Thread: Pointers

  1. #31
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    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.
    Yes, books tend to make some mistakes repeatedly.

    void main() is one of them.

    using %d to print pointers is another.

    technically, %p is for void pointers, so you should use this:

    printf("%p", (void *) pointer ) ;

  2. #32
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    uhh ... does this mean that

    Code:
    int *x,y;
    will create 2 pointers ?
    since the declared type is int * ?

  3. #33
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    uhh ... does this mean that

    Code:
    int *x,y;
    will create 2 pointers ?
    since the declared type is int *...
    so x and y are 2 pointers ... true ?

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, x is a pointer, but y is not. It's a pitfall in the language.
    I always define each pointer on a separate line to avoid such pitfalls.
    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.

  5. #35
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I define each var on separate line, initializing it and when required - giving small commant
    Code:
    int messageNumber = 0; /* will store the message Number to process */
    comment is optional, meaningful name and initialization are avoided in very rear circumstances.
    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

  6. #36
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Elysia View Post
    I always define each pointer on a separate line to avoid such pitfalls.
    I generally don't; and this is part of the reason a drifted toward my preference in #29.


    Quote Originally Posted by Elysia View Post
    No, x is a pointer, but y is not. It's a pitfall in the language.
    I don't consider it a pitfall of the language. And my preference aside, your comment with the mention of the case...
    Code:
    int *x,y;
    ... brings to mind the further confusion available with the use of a typedef:
    Code:
    typedef int *ip;
    ip a,b;
    Note that in this case, BlaX, that both a and b would be pointers to int. FWIW
    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.*

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave_Sinkula View Post
    I generally don't; and this is part of the reason a drifted toward my preference in #29.
    Yeah, but then again, I consider that wrong because you are basically "naming" the variable *var, and not var.
    A pointer is not a special variable... it's just another variable that you can dereference.
    Therefore this is wrong: type *var
    And this is right: type* var
    This is all IMHO, of course.

    I don't consider it a pitfall of the language. And my preference aside, your comment with the mention of the case...
    I clearly see it as a pitfall simply because
    type *a, *b works, yet type* a, b does not.
    If I can define a pointer as
    type* var
    and
    type *var
    then I should be able to create pointers using both
    type* a, b
    and
    type *a, *b.
    Clearly this isn't a case, I see it as a pitfall and it also introduces confusion.

    Why they did it like this I can only guess...
    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.

  8. #38
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I consider your case to be programmer's block.
    Code:
    type *a, b, **c;
    The type of a, b, and c are quite obvious to me.
    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.*

  9. #39
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Why they did it like this I can only guess...

    Can I try?

    Since pointers can be declared from any type, including the keyword void, it makes sense to me that things are the way they are. The asterisk is mostly syntactic sugar, which would have been necessary anyway in order to make a distinction between what's being declared. You could have introduced a more explicit keyword like pointer, but I don't see how the same problem wouldn't arise in that case. It's just as easy for new programmers to be confused and assume that only variables of a certain type can be declared on a line, or forget to add the keyword at the end.
    Code:
    int p pointer, q; /** oops, q is an int! **/
    Not to mention that use of another keyword would have reserved another variable name for language purposes. Expression through repression! Yay!

    You could make sure that you declare the right thing on the same line through using typedefs, like Dave demonstrated. Perhaps the people who made the language decided if they wanted that, they had it, and if they didn't, those people were satisfied. Win-win.

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not so much as confusion, but I do consider it a pitfall since my preferred way of defining pointers just doesn't work.
    Anyway, while we're still at the subject of typedefs... I like to always explicitly have a * at variables that are pointers, so I prefer:

    Code:
    void (myptr)();
    myptr* p;
    Instead of
    Code:
    void (*mypstr)();
    myptr p;
    Of the horrors if you need a pointer to pointer:
    Code:
    void (*mypstr)();
    myptr* p;
    Good gracious! That looks confusing. I'd rather it be:
    Code:
    void (myptr)();
    myptr** p;
    Which is where another pitfall to me appears... sometimes you've no choice but to embed the darn * into the actual typedef... Ah, sigh.

    Things can never be 100% as you want them, huh?
    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.

  11. #41
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    That's a discussion on personal style though, and i admit i was using the asterisk fixed to the type pointer notation for a long time, but switched to the fixed to the variable after some bugs with multi variable declarations.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

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