Thread: pointer question..

  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,834
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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,834
    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,059
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,834
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

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

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, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM