Thread: Question about pointers

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    Question about pointers

    Hi,

    I get the basics of pointers, but as soo as they get more complex I'm stuck. For example, even the printf function:
    int printf ( const char * format [ , argument , ...] );
    What do they mean by const char *, so a pointer to a string? I allways have problems with things like this.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you pass an array to a function, it degrades into a pointer to its first element. The first argument for printf, the format string, is treated like an array of char.
    printf could have been implemented like this:

    printf( const char format[] [, variable... ] );
    printf( const char *format [, variable... ] );

    To the compiler it all looks the same.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    11
    Yeh but I mean if I just do
    printf("Whatever");
    'Whatever' itself isnt stored as a variable, so its not stored in memory (is it?), so how can you "point" to it?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When you pass an array to a function, it degrades into a pointer to its first element.
    That's mean. What makes an array so special that it "degrades" into a pointer? Is the array slumming? Are you suggesting that pointers are gutter trash? You'd better apologize, I think you hurt the pointer's feelings.



    >'Whatever' itself isnt stored as a variable, so its not stored in memory
    Of course it's stored in memory, otherwise it wouldn't exist at all.

    >so how can you "point" to it?
    One of the biggest benefits of pointers is the ability to refer to anonymous memory.
    My best code is written with the delete key.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > You'd better apologize, I think you hurt the pointer's feelings.
    I do apologize... I need to look up some less loaded words in English. >.>

    > 'Whatever' itself isnt stored as a variable, so its not stored in memory (is it?)
    It is stored in memory. The printf function (like other functions) will allocate enough stack space for its arguments once it is called, since a function's arguments are a part of its stack frame. So even if you write something like

    printf( "hello world\n" );

    the string is still stored somewhere. The C compiler is pretty smart in that regard. It's part of all the little things that happens thanks to machine code generated by MinGW (or whatever you use).
    Last edited by whiteflags; 07-31-2006 at 03:02 PM.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    11
    Ok thanks I tink I understand it more now... but I still have problems with some functions that have complicated pointers. Cant think of any right now but Il get back to you

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quzah isn't adverse to saying that pointers degrade: http://cboard.cprogramming.com/showp...6&postcount=10

    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Boozers and tramps.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Quzah isn't adverse to saying that pointers degrade:
    Yet quzah is an uncivilized brute. Speaking of, how does one pronounce quzah?
    My best code is written with the delete key.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,533
    kuh - zaw? I have never wondered about the name, but that avatar always made me wonder. What is it?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    OMG! It's Cobra Commander from GI Joe! His old sig was a dead giveaway, given that it actually showed that it was a Cobra Commander quote
    If you understand what you're doing, you're not learning anything.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,533
    lol - I would not know GI Joe stuff if it hit me in the face.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think we can officially call this a hijack.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >lol - I would not know GI Joe stuff if it hit me in the face.
    I still have no idea what GI Joe is. I know that it was some kind of army thing, and the boys in my neighborhood couldn't get enough of the little action figures. Apparently there was a lame cartoon too, but that's the extent of my interest.
    My best code is written with the delete key.

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Apparently there was a lame cartoon too
    The best part was the 5 minute "Now you know" segment at the end of each episode.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM