Thread: Pointers

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's completely different types.
    var is float*, so if you dereference is, you get float. Yet radius is float, so if you take its address, you get float*, and assigning float* to float doesn't work, unless you do a cast.

    However, an array (1D ONLY!) is automatically converted to a pointer when you pass it to a function requiring a pointer.
    So array becomes int* and the function wants int*, so it's fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by Elysia View Post
    However, an array (1D ONLY!) is automatically converted to a pointer when you pass it to a function requiring a pointer.
    So array becomes int* and the function wants int*, so it's fine.
    this makes sense ...
    i dld the Dev C++ and i uninstalled the VC++.
    for what you said about the %p and that the main should start with int i checked the whole book and didnt find a main starting with int and the %p while printing a pointer.
    by the way im using the teach yourself C in 21 days for sams.should i switch to another book ?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I know little about books, but books can be wrong too, you know. Always return int from main and pointers aren't supposed to be printed with %d. For C++, it's %p, I don't know if it holds for C, however. But there are resources where you can check.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in the second sample there is also error in the function prototype
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    ill think about that.
    anyways thank you for all your help

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, as I mentioned before, all function definitions and declarations should match, no exceptions. The compiler takes a look at the prototype when compiling, so if you type int there it will think it accepts int instead of int* as you have it in the definition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BlaX View Post
    yeah i know, i knew that its done by
    var = &radius.

    but the answer in the book confused me.
    plus it is working when trying to pass an address as an argument when the function's argument is *x. its like *x = &array[0].
    ?
    I think you're getting confused between initializing and assignment. The variable declaration
    Code:
    float *var = &radius;
    is the same as the code
    Code:
    float *var;
    var = &radius;
    The variable is var, not *var; the type is float * (i.e., pointer-to-float), not float.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    The variable is var, not *var; the type is float * (i.e., pointer-to-float), not float.
    Which is why I always do type* var instead of type *var!
    Makes it easier to distinguish type from variable. Type is type* and variable name is var!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    how should i know all these if they arent mentioned in the book !... this book sucks

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BlaX View Post
    how should i know all these if they arent mentioned in the book !... this book sucks
    I would be really surprised if a statement such as "the variable declaration float *var defines a variable named var, which holds the address of a float variable" does not appear anywhere in the chapter.

  11. #26
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by tabstop View Post
    I would be really surprised if a statement such as "the variable declaration float *var defines a variable named var, which holds the address of a float variable" does not appear anywhere in the chapter.
    i read the whole chapter,but it doesnt say anywhere that there is a type float *.i thought the type was just float till Alysia said that the type is float *.

    anyway.i was wondering whats the difference of starting a main() with int or without it ?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the type is a pointer to float, hence why I say the type is float*. Because it's a pointer that points to a value of float.
    However, there's nothing that says the type is float*. You can write it as float* var, float * var, float *var, as you wish. It's the mindset of which is easier. I find float* var easiest as it tells me that var is a variable of float*, a pointer to float.

    For the second question... In C, all functions that lack a return type are automatically considered to return int. This is frowned upon by many upon the boards who like to explicitly specify the return type.
    It can also be confusing because you may think the function returns nothing. But this is not the case. It implicitly returns int. Which is even worse, because in C you can omit a return for functions and then the return type will be undefined.
    So you may think the function returns nothing, and you omit the return, and thus the return is undefined.
    In C99, main is required to return int. In C++, all functions must have a return type and return something, so just omitting return type is, at the very least, not a good practice.
    Last edited by Elysia; 02-21-2008 at 01:37 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BlaX View Post
    i read the whole chapter,but it doesnt say anywhere that there is a type float *.i thought the type was just float till Alysia said that the type is float *.

    anyway.i was wondering whats the difference of starting a main() with int or without it ?
    I can't find the C version in the library, just the C++ version, but it says this:
    Storing a Variable's Address in a Pointer
    Every variable has an address. Even without knowing the specific address, you can store a variable's address in a pointer.

    Suppose, for example, that howOld is an integer. To declare a pointer called pAge to hold its address, you write

    int *pAge = 0;

    This declares pAge to be a pointer to an int. That is, pAge is declared to hold the address of an integer.

    Note that pAge is a variable. When you declare an integer variable (type int), the compiler sets aside enough memory to hold an integer. When you declare a pointer variable such as pAge, the compiler sets aside enough memory to hold an address (on most computers, four bytes). A pointer, and thus pAge, is just a different type of variable.
    As to the other, every program returns an exit code to the operating system, so your main must return an int.

  14. #29
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Elysia View Post
    Which is why I always do type* var instead of type *var!
    Makes it easier to distinguish type from variable. Type is type* and variable name is var!
    Which is why I always do type *var instead of type* var!
    Makes it easier to distinguish type of the dereferenced variable: *var is a type!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh... That makes no sense. Why would you want to have more clarity on the dereferenced type? Knowing that the type is type*, then the dereferenced type must be the type - *, so type.
    type *var makes you think *var is the name of the variable!
    Or not. Hehehe. It depends on the mindset of each individual. That's why there's always a huge, heated debate whenever the standard wishes to discuss which syntax to use, and I'm guessing, that's why all 3 are in the language
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM