Thread: Difference between Int *p and int *p = &a

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    Difference between Int *p and int *p = &a

    Hello,

    Some way the pointers are used confuses me.

    When use as below

    Code:
    int a = 5;
    int *p = &a;
    
    printf("address of p = %d\n", &p);
    printf("Address pointed by p = %d\n", p);
    printf("Address of a = %d\n", &a);
    printf("Value pointed by p = 5d\n", *p);
    Code:
    OUTPUT: address of p = 1987328632                                                         Address pointed by p = 1987328628                                               
    Address of a = 1987328628                                               
    Value pointed by p = 5
    here p is pointed to the memory address of a

    But what does this mean
    Code:
    int *z = 7;
    To print this

    Code:
    printf("%d\n", z);
    1. Is z pointer here?? If yes, then why
    Code:
    printf("%d\n", z)
    instead of
    Code:
    printf("%d\n", *z)
    2. Is z pointed to 7 or the address of 7 ?
    3. What is the address of z ?
    4. Why can't I use
    Code:
    int *p = &45
    ??
    Last edited by Athul; 09-28-2018 at 03:32 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Athul
    But what does this mean
    Code:
    int *z = 7;
    It means that you are initialising the pointer z with the value of 7. This is usually a mistake, but sometimes it is well documented for the particular system that you're coding for such that either the address 7 has some designated meaning, or the compiler knows to transform the special value to a valid address.

    Quote Originally Posted by Athul
    1. Is z pointer here?
    Yes. You declared z to be a pointer, so it is a pointer.

    Quote Originally Posted by Athul
    If yes, then why
    Code:
    printf("%d\n", z)
    instead of
    Code:
    printf("%d\n", *z)
    If you want to print the pointer z, you should write:
    Code:
    printf("%p\n", (void*)z);
    If you want to print what z points to, then the second example that you showed is correct, except that because you initialised z to 7, it probably doesn't point to something valid so this would also be an error.

    Quote Originally Posted by Athul
    2. Is z pointed to the address of 7 or 7 ?
    The value of z is 7, i.e., if it were a valid address, it would be the address 7. Again, because z probably contains an invalid address, it doesn't actually point to anything valid.

    Quote Originally Posted by Athul
    3. What is the address of z ?
    The address of z is &z

    Quote Originally Posted by Athul
    4. Why can't I use
    Code:
    int *p = &45
    ??
    You cannot take the address of an integer literal as it might not even have an address.
    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

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    Thanks for the reply

    It means that you are initialising the pointer z with the value of 7. This is usually a mistake, but sometimes it is well documented for the particular system that you're coding for such that either the address 7 has some designated meaning, or the compiler knows to transform the special value to a valid address.
    Does this mean,

    Code:
    Int *p = 7
    here 7 is treated as address, not as value??

    If Yes then I guess below question is not valid.

    If No

    1. Is z pointer here?? If yes, then whyCode:
    [COLOR=white !important]?[/COLOR]
    1
    printf("%d\n", z)



    instead ofCode:
    [COLOR=white !important]?[/COLOR]
    1
    printf("%d\n", *z)


    What I meant by this is,

    For example

    Code:
    int a = 5;
    int *p = &5;
    Here p is a pointer, then to print value pointed by p,

    Code:
    printf("%d", *p);
    In the case of
    Code:
    int *z = 7;
    If z is also pointer, then as per my first example what comes first would be following to print value pointed by pointer z

    Code:
    printf("%d", *z);
    but that wouldn't be correct, the correct code is

    Code:
    printf("%d", *z);
    I don't understand why??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Athul
    here 7 is treated as address, not as value??
    Both! p is a variable which is initialised by a value. However, because p is specifically a pointer, this value is treated as an address.

    Quote Originally Posted by Athul
    For example
    Code:
    int a = 5;
    int *p = &5;
    As you noted in post #1, this will not compile. As I noted in post #2, this is because you cannot take the address of an integer literal. You probably intended this example:
    Code:
    int a = 5;
    int *p = &a;
    Quote Originally Posted by Athul
    Here p is a pointer, then to print value pointed by p,
    Code:
    printf("%d", *p);
    Yes, that is correct.

    Quote Originally Posted by Athul
    In the case of
    Code:
    int *z = 7;
    If z is also pointer, then as per my first example what comes first would be following to print value pointed by pointer z
    Code:
    printf("%d", *z);
    but that wouldn't be correct
    z is also a pointer, and hence you are wrong to say that "that wouldn't be correct". The correct code to print what z points to is indeed:
    Code:
    printf("%d", *z);
    This is because *z is what the pointer z points to. The issue that you are facing is not that the syntax of printing what z points to is wrong, it is that z doesn't actually point to something valid.

    Quote Originally Posted by Athul
    the correct code is
    Code:
    printf("%d", *z);
    I don't understand why??
    It looks like you copied and pasted without changing your code snippet. You probably wanted to say that the correct code is:
    Code:
    printf("%d", z);
    But this is wrong. It prints z, not what z points to (and does so with the wrong format specifier).

    If you're still having trouble with this concept, then I have two suggestions:
    • Keep in mind that if z is the pointer, *z is what the pointer points no. As long as the pointer z is valid and not a null pointer, this is true. If the pointer z is invalid or a null pointer, then *z is wrong. Absolutely wrong. Completely forbidden. Never ever dereference a pointer that you don't know for sure to be valid and not a null pointer.
    • Never initialise a pointer with an integer literal other than 0 (which means setting the pointer to be a null pointer), e.g., never write:
      Code:
      int *z = 7;
      Forget that you have seen this, and just never do it. It is wrong. Wrong. WRONG. The resulting pointer will be invalid. (One day you might do it anyway and it might be the right thing to do, but by then this won't be an issue, so just treat it as absolutely wrong and hence forbidden for now.)
    Last edited by laserlight; 09-29-2018 at 07:47 PM.
    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

  5. #5
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    So to sum up

    When do as follows

    Code:
    int *p = 5;
    here value 5 is treated as an address

    Code:
    printf("%d\n", p);
    
    //OUTPUT : 5
    But when do as follows
    Code:
    printf("%d\n", *p);
    It crashes program because p is not pointing to something,

    Right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between EOF and EOL
    By chinthaka in forum C Programming
    Replies: 2
    Last Post: 11-17-2012, 09:48 AM
  2. Difference between C++ and VC++?
    By #include Jack in forum C++ Programming
    Replies: 55
    Last Post: 03-31-2010, 04:31 PM
  3. Difference between ROM and RAM..
    By shwetha_siddu in forum C Programming
    Replies: 18
    Last Post: 06-23-2008, 03:23 AM
  4. difference
    By srinu in forum Windows Programming
    Replies: 2
    Last Post: 02-13-2003, 04:19 PM
  5. Difference between %f and %d
    By Johnny1 in forum C Programming
    Replies: 12
    Last Post: 09-07-2002, 04:03 PM

Tags for this Thread