Pointer (adress) question.

This is a discussion on Pointer (adress) question. within the C Programming forums, part of the General Programming Boards category; I've been playing a bit with pointers and strings. Code: #include <stdio.h> int main() { char string; //Sets up a ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Pointer (adress) question.

    I've been playing a bit with pointers and strings.
    Code:
    #include <stdio.h>
    
    int main()
    {
      char string;                        //Sets up a character named string.
      char * pString = &string;  //Makes a character pointer and assings the adress of string to it
      puts("Enter: ");
      scanf(" %s", pString);        //I know this could have been gets(), I used scanf().
      printf("%s\n",pString);
      printf("pString has been stored here: %d", pString);
      getchar();
      getchar();
      return 0;
    }
    Why doesn't it matter (at least as far I can see) whether I do:
    Code:
      printf("pString has been stored here: %d", pString);
    or
    Code:
    printf("pString has been stored here: %d", &pString);
    The ampersand is the adress of operator in this case, so why can I get the adress without using it? Did I miss something about pointers perhaps?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    pString is a pointer. The variable itself holds the address of what it points to, so of course it's going to print an address.
    If you use & on pString, you will actually take the address of the pointer variable itself (not what it points to), so you'd actually get char**.
    Oh and you should print pointers using &#37;p and not %d.
    Oh and gets is a very, very bad function. Never use it. And using scanf to read strings is also tricky. You need to be careful. Read my signature about it.
    Last edited by Elysia; 02-25-2008 at 01:55 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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hmm. I expect that crashes if you enter a string that is more than about 3 chars long.

    And I would also expect that if you print pString using %d, you get one value, and if you print &pString using %d, you get a very similar value that is approximately 4 less than the pString value, because I expect the address of string to be 4 less than pString itself.

    Try printing both next to each other, or subtract one from the other to see it properly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Look at the declaration again:
    Code:
    char * pString;
    This means that pString is a variable that can hold an address of a character (to wit, the first character of your string). Perhaps your string lives at address "123 Elm St." (obviously completely fictitious example). So pString = "123 Elm St.". However, pString is a variable, and must be stored somewhere in memory too, so perhaps &pString = "159 Elm St.". The value of the variable is a pointer to a string; the address of the variable is where the variable itself lives.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Oh and you should print pointers using %x and not %d.
    To be perfectly correct, neither %x nor %d will be correct for pointers. If you want to use the printf format that matches exactly to a pointer, you should use %p. It will use hex, and match the size of a pointer (e.g. 64-bit on a 64-bit OS, rather than %d that is 32-bit on both 32- and 64-bit systems).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Wait, oops, yes, I meant &#37;p. A typo.
    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.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Oops I overlooked that I used pString instead of string. My bad

    &#37;p for printing pointers.

    So I would do:
    Code:
    #include <stdio.h> 
                       
    int main()         
    {                  
     int a = 10;       
     int * pA = &a;    
     printf("%p", pA); 
     getchar();
     return 0;        
    }
    This gives me a memory adress in hexadecimal system.
    Last edited by omnificient; 02-25-2008 at 02:24 PM. Reason: I fixed the return 0;

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >printf("%p", pA);
    The %p specifier expects a pointer to void. Matching the expected type is important:
    Code:
    printf("%p", (void*)pA);
    >}
    What's up with saying main will return int but not returning anything? :P
    My best code is written with the delete key.

  9. #9
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    What do you call doing
    Code:
    (void *)pA
    It looks like a sort of typecasting to me.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    >printf("%p", pA);
    The %p specifier expects a pointer to void. Matching the expected type is important:
    Code:
    printf("%p", (void*)pA);
    >}
    What's up with saying main will return int but not returning anything? :P
    And whilst it's likely that there are systems where a void * is different from a char *, I doubt the original poster's problem will be solved by a type-cast.

    And to omnificient:
    It looks like a sort of typecasting to me.
    Yes it is. There's nothing wrong with a typecast, although I very much doubt that it changes anything in your particular case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    scanf(" &#37;s", pString); //I know this could have been gets(), I used scanf().
    you have buffer of 1 char length, you really do expect some input will fit into this buffer?

    and forget abour gets, when you need to read line - use fgets as described in the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well I'm not really familiar with C and gets() is a lot easier thatn fgets(), therefore I will use gets() and thereby ignore your advice, even if it might overwrite memory it doesn't own.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    May you forever be looked on as a day one n00b, then, by your own request.
    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.*

  14. #14
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Right, what I'm saying is that I have enough to figure out already. When I find that the gets() function is no longer suitable for my needs, I'll try and figure out how the fgets() function works.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    If you ignore our advice, then I shall certainly not welcome you here anymore.
    You are openly ignoring advice from experts and you are choosing a function which you should never, ever use PLUS you are inviting malware vendors to hack into your application and infest others' computers.
    Every programmer should be able to use fgets. It is incredibly easy, and many others have been corrected before you and use it correctly. And yes, they were all using gets before.
    Now, I suggest you do the same. If not, then you should not be in the programming world or perhaps not be using C.
    Never use gets!
    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.
    よく聞くがいい!私は天才だからね! ^_^

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