Thread: Pointers: & and * when scanning

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    7

    Pointers: & and * when scanning

    Hello, I have a quick question about using & and * when it comes to pointers and scanf.

    If I have

    Code:
    int *ptr;
    int a = 3, b;
    What happens if I have

    Code:
    ptr = &b;
    scanf("%d", &ptr);
    where there's a & in front of the pointer

    or


    Code:
    ptr = &b;
    scanf("%d", *ptr);
    where there's a * in front of the pointer?

    I know that when you scan something into an array, you don't need the & because it's a pointer. So that means that when you scan something into a pointer, you don't need it. But what happens if you have a & or a * in front of ptr? Does * mean that the value that is scanned in s put into b?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Answer to your first question:
    Pointers are variables too, with their own address in memory. In this case, you print the address of "ptr".

    Answer to your second question:
    "ptr" is... a pointer, so you can dereference it to get they value it's pointing at. In this case garbage, because "b" was left undefined.

    And about your final question:
    You can think of all pointers as arrays and vice-versa. Whether that array has size 0(NULL), 1, 10 or 42 is up to the programmer. "*ptr" is equivalent to "ptr[0]", "*(ptr+1)" to "ptr[1]" etc. When you do "*ptr = 53;" you're changing a value inside that array. When you do "ptr = &b" you are actually changing the array itself.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    7
    Thank so much for your answers!

    So in the case of scanf, which is what I have up there, where am I scanning in values when I use &ptr or *ptr.
    I'm not printing in this case.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by blh View Post
    So in the case of scanf, which is what I have up there, where am I scanning in values when I use &ptr or *ptr.
    Let's assume that "ptr" exists at address 0x100000, "a" at address 0x100008 and "b" at address 0x10000C. I will denote what exists at each address using an arrow.

    Before assignment:
    0x100000 -> garbage
    0x100008 -> 3
    0x10000C -> garbage

    After assignment:
    0x100000 -> 0x100008
    0x100008 -> 3
    0x10000C -> garbage

    "&ptr" gives the leftmost value
    "ptr" gives the rightmost value
    "*ptr" gives the rightmost value of the address that exists at its own rightmost value( it points to the value ). You can visualize it as:
    0x100000 -> 0x100008 -> 3
    Last edited by GReaper; 05-04-2016 at 03:52 PM.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    It's actually undefined what will happen, because your object types, *ptr and &ptr, do not match your conversion specifier, %d.

    But to answer your question, suppose scanf() does actually attempt to store the converted input, and you entered 55:

    With the correct statement,
    Code:
    scanf("%d", ptr);
    the value 55 would be stored in b.

    With &ptr as an argument, it will try to store the value 55 in ptr.
    Now ptr, in all likelyhood, no longer points to b.

    With *ptr as an argument, it will try to store the value 55 in whatever b is pointing to.
    This is where the real havoc can be wrought.

    -

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    7
    Quote Originally Posted by GReaper View Post
    "&ptr" gives the leftmost value
    "ptr" gives the rightmost value
    "*ptr" gives the rightmost value of the address that exists at its own rightmost value( it points to the value ). You can visualize it as:
    0x100000 -> 0x100008 -> 3
    Okay I see. That helps, thank you!

  7. #7
    Registered User
    Join Date
    Apr 2016
    Posts
    7
    Quote Originally Posted by megafiddle View Post
    With &ptr as an argument, it will try to store the value 55 in ptr.
    Now ptr, in all likelyhood, no longer points to b.

    With *ptr as an argument, it will try to store the value 55 in whatever b is pointing to.
    This is where the real havoc can be wrought.

    -
    Yeah I didn't expect for these statements to do anything specific, it was just a to get me thinking, and I have little experience with pointers. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning the string
    By Nino Meh in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2012, 02:38 PM
  2. EOF while scanning
    By dpp in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2009, 10:14 PM
  3. Scanning ...
    By whammy in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2005, 06:25 PM
  4. Scanning a directory
    By ColdFire in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2003, 04:39 AM
  5. Better scanning method
    By pdstatha in forum C Programming
    Replies: 17
    Last Post: 03-30-2002, 03:34 PM

Tags for this Thread