Thread: Unclear to pointers.

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    Unclear to pointers.

    well, I have some touch question and I really need help, good explanation needed for answering them.

    1)Can anyone explain exactly what do
    Code:
    int *p=10
    &&
    Code:
    int *p=NULL
    stand for? and isn't
    Code:
    NULL
    considered as a value?
    I actually understood pointers, since *p => resemble the value of pointer p.

    2)why can't I write like
    Code:
    int *p;
    p=10;
    ? isn't that equivalent to int *p=10?
    and why if I write like
    Code:
    int *p;
    p=NULL;
    it will work normally??
    Last edited by Romyo2; 09-24-2015 at 07:09 AM.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    130
    Also, is the
    Code:
     NULL
    can be used sometimes as a pointer and sometimes as a value?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a pointer named p of type int* and initialises it to an address of value 10:
    Code:
    int *p = 10;
    Unless you are dealing with an environment where you are supposed to initialise the pointer to an address with value 10, you should not do this.

    This declares a pointer named p of type int* and initialises it to be a null pointer:
    Code:
    int *p = NULL;
    NULL is an implementation defined null pointer constant. For example, your standard library implementation might have:
    Code:
    #define NULL 0
    or
    Code:
    #define NULL ((void*)0)
    or something else such that NULL is a null pointer constant.

    Quote Originally Posted by Romyo2
    2)why can't I write like
    Code:
    int *p;
    p=10;
    ? isn't that equivalent to int *p=10?
    You should be able to do that if you are able to initialise p to 10 in the first place. If not, maybe your compiler is just trying to stop you from doing something stupid... like hard coding an assignment that assigns 10 to the pointer even though the address 10 is not guaranteed to be available for you.
    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

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    This declares a pointer named p of type int* and initialises it to an address of value 10:
    Code:
    int *p = 10;
    Unless you are dealing with an environment where you are supposed to initialise the pointer to an address with value 10, you should not do this.

    This declares a pointer named p of type int* and initialises it to be a null pointer:
    Code:
    int *p = NULL;
    NULL is an implementation defined null pointer constant. For example, your standard library implementation might have:
    Code:
    #define NULL 0
    or
    Code:
    #define NULL ((void*)0)
    or something else such that NULL is a null pointer constant.


    You should be able to do that if you are able to initialise p to 10 in the first place. If not, maybe your compiler is just trying to stop you from doing something stupid... like hard coding an assignment that assigns 10 to the pointer even though the address 10 is not guaranteed to be available for you.

    Got you, so what I've posted in comment #2 is completely correct, right?
    NULL can be used either as a value or a pointer.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    Got you, so what I've posted in comment #2 is completely correct, right?
    NULL can be used either as a value or a pointer.
    No. NULL is not (necessarily) a pointer. It is a null pointer constant.
    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

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    No. NULL is not (necessarily) a pointer. It is a null pointer constant.
    I'm with you!

    then why when I write this syntax writing as :
    Code:
    int *p;
    p=NULL;
    in my compiler(Visual C++ 2010) shows the same result as
    Code:
     *p=NULL
    ?
    Also it's showing an error while writing
    Code:
    int main()
    {
    	int *p=NULL;
    	*p=9;
    	printf("%d", *p); // it should print the number f9//
    	return 0;
    }
    !!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    I'm with you!

    then why when I write this syntax writing as :
    Code:
    int *p;
    p=NULL;
    in my compiler(Visual C++ 2010) shows the same result as
    Code:
     *p=NULL
    ?
    The first code snippet declares p, then assigns NULL to p, causing it to be a null pointer. The second code snippet dereferences p and assigns NULL to the result. This is semantically wrong since p is a pointer to int, so *p is an int, hence you should not be assigning NULL to it since NULL is a null pointer constant.

    That said, my guess is that you are confusing this:
    Code:
    int *p = NULL;
    with:
    Code:
    *p = NULL
    I have already explained the former in post #3.

    Quote Originally Posted by Romyo2
    Also it's showing an error while writing
    Code:
    int main()
    {
    	int *p=NULL;
    	*p=9;
    	printf("%d", *p); // it should print the number f9//
    	return 0;
    }
    !!
    Since you initialised p to be a null pointer, you cannot dereference p since a null pointer does not point to an object.
    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

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    Since you initialised p to be a null pointer, you cannot dereference p since a null pointer does not point to an object.
    I conclude if an pointer has been initialised to be a null pointer, its value(value of the null pointer) can't be changed because the null pointer is constant and has always the value 0/<null> , right?


    thanks

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    I conclude if an pointer has been initialised to be a null pointer, its value(value of the null pointer) can't be changed because the null pointer is constant and has always the value 0/<null> , right?
    No, a null pointer and a pointer that is constant are not necessarily the same. This declares p1 to be a pointer to int that is a null pointer:
    Code:
    int *p1 = NULL;
    In the above, you can modify p1 by say, assigning another pointer to it, but you cannot access, much less modify, what p1 points to because p1 does not point to anything.

    This declares p2 to be a pointer to int that is constant:
    Code:
    int x;
    int * const p2 = &x;
    In the above, you cannot modify p2, but you can modify what p2 points to through p2.

    This declares p3 to be a pointer to int that is constant and a null pointer:
    Code:
    int * const p1 = NULL;
    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

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If any of this is still unclear, I recommend watching Binky: Pointer Fun with Binky - YouTube

    One way to look at the difference between declaration and dereference is to notice that this:
    Code:
    int *p = NULL;
    defines the type of the expression *p (dereferencing), and the way the star is laid out helps show that in this case. I think.
    Last edited by whiteflags; 09-24-2015 at 11:37 AM.

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by whiteflags View Post
    If any of this is still unclear, I recommend watching Binky: Pointer Fun with Binky - YouTube

    One way to look at the difference between declaration and dereference is to notice that this:
    Code:
    int *p = NULL;
    defines the type of the expression *p (dereferencing), and the way the star is laid out helps show that in this case. I think.

    So if I'm not wrong, and from what you're telling about, I can do like this thing of coding:
    Code:
    int *p=10;
    p=15;
    printf("%d", p);
    then it must print 15 without any errors, ye?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    So if I'm not wrong, and from what you're telling about, I can do like this thing of coding:
    Code:
    int *p=10;
    p=15;
    printf("%d", p);
    then it must print 15 without any errors, ye?
    No:
    • As I noted earlier, you should not be initialising pointers with integer constants or assigning integer constants to pointers unless you know that such initialisation/assignment is valid.
    • The correct format specifier for printing a pointer with printf is %p, not %d. Furthermore, if the pointer is not a pointer to void, you should cast it to be a pointer to void.
    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
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    No:
    • As I noted earlier, you should not be initialising pointers with integer constants or assigning integer constants to pointers unless you know that such initialisation/assignment is valid.
    • The correct format specifier for printing a pointer with printf is %p, not %d. Furthermore, if the pointer is not a pointer to void, you should cast it to be a pointer to void.
    To the first note, why shouldn't I? I can tell u that I understood you, but truly didn't!, and I attended to this website for learning/helping.
    I don't want to count on others, but I really still misunderstand that case..

    lets start from here, when I write
    Code:
    int *p
    , what in terms of PC is, occupying a byte which the pointer on it is p, and its kind is int, so once I write
    Code:
    int*p=10
    , actually the byte value that the pointer p is pointing on the byte itself is 10 (specific to this case) ... so what's wrong with putting a value as int to a pointer which its kind is int*?


    thanks very much and if u can throw an example throw explaining would be much appreciated!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    To the first note, why shouldn't I?
    Here's what the C standard has to say about such a conversion:
    Quote Originally Posted by C11 Clause 6.3.2.3 Paragraph 5
    An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
    The "except as previously specified" part has to do with null pointer constants since an integer constant expression with the value 0 is a null pointer constant.
    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

  15. #15
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight;
    The "except as previously specified" part has to do with null pointer constants since an integer constant expression with the value 0 is a null pointer constant.
    So if the matter is just about needing a null pointer constant, then I can do like this and all things must be worked properly:
    Code:
    int *p=NULL;
    p=10; // now the p is considered as a null constant pointer. 
    printf("%d", p);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something unclear for me.
    By RyanC in forum C Programming
    Replies: 3
    Last Post: 08-11-2015, 07:03 AM
  2. Program skips input && Unclear Warnings
    By Sourabh Verma in forum C Programming
    Replies: 1
    Last Post: 08-27-2014, 07:36 AM
  3. Unclear Error Messages
    By mzottola in forum C Programming
    Replies: 2
    Last Post: 10-12-2012, 08:55 PM
  4. Replies: 5
    Last Post: 10-02-2011, 03:21 PM
  5. unclear about typecasting and creating pointers
    By kirani in forum C Programming
    Replies: 11
    Last Post: 06-16-2010, 04:29 PM