pointer question..

This is a discussion on pointer question.. within the C Programming forums, part of the General Programming Boards category; when i declare Code: int* ptr_a, ptr_b; why the type of ptr_b is int and not int* ???...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    pointer question..

    when i declare

    Code:
    int* ptr_a, ptr_b;
    why the type of ptr_b is int
    and not int*

    ???

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    If you spaced it out like this it may become clearer...
    Code:
    int *ptr_a, ptr_b;
    (there is no asterisk on the ptr_b)

    Perhaps you wanted:
    Code:
    int *ptr_a, *ptr_b;

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Because of a language pitfall. Better define each pointer on a separate row:
    Code:
    int* ptr_a;
    int* ptr_b;
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    What Elysia said!

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so its illegal to write??

    Code:
    int* ptr_a, ptr_b;
    my goal was to declare 2 variables as pointer in one line

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    No it's not illegal. It just won't do what you wanted. You need to put the asterisk with each variable you wish to be a pointer.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    so its illegal to write??

    Code:
    int* ptr_a, ptr_b;
    my goal was to declare 2 variables as pointer in one line
    In that case

    Code:
    int *ptr_a, *ptr_b;
    is what you are looking for.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    whats the result of
    Code:
    int* ptr_a, ptr_b;
    the astrix near the int

    what will be the result of such line?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    But if you don't want to place the * before the name, it's better to put each pointer on a separate row. Consistency is the argued most important thing. So pick the one you feel most comfortable with and stick with it.
    Quote Originally Posted by transgalactic2 View Post
    whats the result of
    Code:
    int* ptr_a, ptr_b;
    the astrix near the int

    what will be the result of such line?
    ptr_a will be of type int*, ptr_b will be of type int.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by transgalactic2 View Post
    whats the result of
    Code:
    int* ptr_a, ptr_b;
    the astrix near the int

    what will be the result of such line?
    The asterisk near the int is not the issue.
    You could write
    Code:
    int* ptr
    or
    Code:
    int * ptr
    or
    Code:
    int *ptr
    The fact is, it precedes the variable name. Somewhere. With or without misdirecting spaces. If you want a second pointer, it too needs to be preceded by an asterisk. You'd never realize that using Elysia's misinterpretation of pointer declaration.
    Last edited by nonoob; 10-13-2008 at 01:46 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Quote Originally Posted by nonoob View Post
    ...You'd never realize that using Elysia's misinterpretation of pointer declaration.
    Misinterpretation?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Although it was written with C++ in mind, I suggest reading Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Thank you, laserlight... It helped me understand where int* p comes from. I'm not a C++ person. I was about to post the following in response to Elysia:

    The pitfall, if any, appears to be that you've settled on a syntactical foible that implies some associativity of basic C data type with the pointer indicator. This causes you to make strange recommendations about separate lines.

    But now I realize there is a reason. But I took a long time to compose the above so I thought I'd voice it anyhow for posterity.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Quote Originally Posted by nonoob View Post
    Thank you, laserlight... It helped me understand where int* p comes from. I'm not a C++ person.
    Nevertheless, this is not associated with C++. This style is equally valid in C and any good book should line out both alternatives, leaving the programmer to choose their preferred style.
    But it's true that I heavily prefer emphasis on pointers binding to the types due to my C++ background.
    Nevertheless it's a good advice, really, since defining pointers on separate lines avoids confusion and people thinking that you've made a (probable) error (as Bjarne clearly lines out in his faq).

    And actually, I was looking for that article when replying earlier, but couldn't actually find it due to only bjarne's last name was in the title, so Firefox wouldn't find it. I bookmarked it now, so no problems now!
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21