Like Tree2Likes
  • 1 Post By MK27
  • 1 Post By anduril462

pointers basic question

This is a discussion on pointers basic question within the C Programming forums, part of the General Programming Boards category; Hi, i wanted clarification about pointers. 1) if i declare something like Code: int *p ; it creates pointer variable, ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    44

    pointers basic question

    Hi,
    i wanted clarification about pointers.

    1) if i declare something like
    Code:
     int *p ;
    it creates pointer variable, which is not pointing to a variable yet, but would point to integer variable. so, if i access &p, is it a valid address or junk?
    2) when i declare a void pointer like
    Code:
     void *p1;
    p1 then holds address of another variable, whose type is not known. Is this address
    a valid one ? if so, does it mean that is points to a variable that could be 1 byte, 2 byte etc depending upon the the type?
    3) Null pointer points to reserved space, which could be 0 . so, does NULL pointer have a type?
    4)IN C, void to pointer type T is implicit. does it hold for void to NULL pointer too?
    for example, can i have a function like this
    Code:
      void * f(void)
                {
                 some code
    
                 return NULL;
                }
    thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sed_y View Post
    1) if i declare something like
    Code:
     int *p ;
    it creates pointer variable, which is not pointing to a variable yet, but would point to integer variable. so, if i access &p, is it a valid address or junk?
    "&p" would be the address of p, which has to be a valid address or p would not exist. Pointers themselves -- even if they have no memory allocated to point to -- require some memory just to exist, because they have to exist somewhere.

    "p" would be the address p points to, which if it is uninitialized, would be a junk value. This is the same as:

    Code:
    int x;
    "&x" would be the address of x, a real value. "x" would be an integer value, which if it is uninitialized, is random junk.

    2) when i declare a void pointer like
    Code:
     void *p1;
    p1 then holds address of another variable, whose type is not known. Is this address
    a valid one ?
    Not while p1 is uninitialized, since only by (very unlikely) coincidence would p1 actually hold the address of another variable.

    if so, does it mean that is points to a variable that could be 1 byte, 2 byte etc depending upon the the type?
    If it were assigned to something, yes.

    Code:
    char str[256];
    p1 = str;  // p1 points to 256 byte char block
    int x;
    p1 = &x; // p1 points to an int
    3) Null pointer points to reserved space, which could be 0 . so, does NULL pointer have a type?
    A null pointer does not point to anything. And it's value not just "could be 0" -- it is by definition 0.
    Any type of pointer can be NULL -- NULL is not a type:

    Code:
    int *x = NULL;
    char *s = NULL;
    void *p = NULL;
    //etc
    can i have a function like this
    Code:
      void * f(void)
                {
                 some code
    
                 return NULL;
                }
    Yes. Again, any type of pointer can be set to NULL.

    4)IN C, void to pointer type T is implicit.
    Conversion is implicit, but if you have a void pointer and do not know what it points to, there is no way to find out, either. So type checking is not implicit, it's impossible, and "implicit conversion" does not mean "correct conversion". The compiler will allow you to convert a void pointer to anything, even if it is the wrong type.
    Last edited by MK27; 06-23-2011 at 12:45 AM.
    mistu4u likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    44
    Hi Mk,
    thanks for such detailed answer.

    one last thing.
    Not while p1 is uninitialized, since only by (very unlikely) coincidence would p1 actually hold the address of another variable.
    so,this means, if pi was uninitialized, it would hold an address say 0x0002, which may not be assigned to a variable, and now, casting this pointer to NULL, would make this 0x0000?

    Thank you once again
    sedy

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    p1 holds an address, say 0x0002. That is a real memory address, but it (most likely) does not belong to you (the data there probably belongs to someone else).

    You cannot cast a pointer to NULL.

    If you assign p1 = NULL, then now the value of p1 is 0x0000. This does not affect the value of the memory at 0x0002.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,440
    Just a little more info/clarification:
    Quote Originally Posted by sed_y View Post
    1) if i declare something like
    Code:
     int *p ;
    it creates pointer variable, which is not pointing to a variable yet, but would point to integer variable. so, if i access &p, is it a valid address or junk?
    If you're trying to "access" what p points to, then you want *p. Since p is uninitialized, you may find junk or you may crash your application by accessing memory you don't own. Technically, it's undefined behavior, which means anything or nothing is allowed to happen. It's bad juju, so don't do it.

    2) when i declare a void pointer like
    Code:
     void *p1;
    p1 then holds address of another variable, whose type is not known. Is this address
    a valid one ? if so, does it mean that is points to a variable that could be 1 byte, 2 byte etc depending upon the the type?
    A void * can point to anything, but the compiler can never be sure of the type of thing it points to. To extend MK27's example:
    Code:
    char str[256];
    p1 = str;  // p1 points to 256 byte char block
    int x;
    p1 = &x; // p1 points to an int
    int y = *p1 + 3;  // The compiler doesn't know how to dereference a void * (i.e., it can't be sure of what's there)
    y = *((int *) p1) + 3;  // This works, because you forced the compiler (by casting) to treat p1 as a pointer to an int, thus *p1 refers to an int
    Note that casting will keep the compiler from producing warnings. This is fine if you're 100% sure of what you're doing, but if you mis-cast something, you can wreak havoc in your program's memory space. Use sparingly and be careful.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    44
    yes,
    i meant p1=NULL, not casting to NULL, as casting would be to specify the type, and NULL does not have a type.
    thanks

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    44
    extending anduril's example
    y = *((int *) p1) + 3;
    y = *((char *) p1) + 3; // y is char
    in first case, now, compiler knows to go forward 3 steps , but 3*4 bytes, for 4 byte int
    and in second case, it goes 3 steps,=3 bytes. w/o casting, it would not know this.

    Thank you all guys

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,440
    Quote Originally Posted by sed_y View Post
    extending anduril's example
    Code:
    y = *((int *) p1) + 3; 
    y = *((int *) p1) + 3; // y is char
    in first case, now, compiler knows to go forward 3 steps , but 3*4 bytes, for 4 byte int
    and in second case, it goes 3 steps,=3 bytes. w/o casting, it would not know this.

    Thank you all guys
    You have a couple problems here. First, *((int *) p1) is not a pointer, it's an int pointed to by p1, so *((int *) p1) + 3 is integer arithmetic, not pointer arithmetic. You need to do *((int *)p1 + 3) for pointer arithmetic (notice the difference in parentheses). The addition happens before the dereference.

    Second, both your examples are the same and thus will behave exactly the same. The compiler does not do pointer arithmetic based on the type of where you're putting the result, but the type of the pointer. E.g.
    Code:
    int x, y;
    void *p1 = &x;
    
    y = *((int *)p1 + 3);  // increment p1 by 3*sizeof(int)=3*4=12 bytes, then retrieve the integer value there and store it in y
    y = *((char *)p1 + 3);  // increment p1 by 3*sizeof(char)=3*1=3 bytes, then retrieve the char value there and store it in y
    It's the casting of p1 that affects the pointer arithmetic, not the type of y. Also note that sizeof(int) will be 4 on a 32-bit system, and 8 on a 64-bit.
    Last edited by anduril462; 06-23-2011 at 12:26 PM.
    mistu4u likes this.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think the OP is confused about the meaning of NULL and void.

    NULL might as well be zero for all practical purposes. A NULL pointer is one that contains address of zero (although some would argue there is nothing that mandates the exact value as long as the native system understands it as a special reserved value and acts accordingly). A pointer which holds such an address is to be interpreted as pointing to nothing. On purpose. The value is a place-holder. For example, the returned value from a failed file-open, or the failed allocation of a memory area. The expected value is an address that points to a file descriptor or memory area. A special value of zero indicates the function failed.

    A "nul" is used as the last byte in a string. Sometimes said to be NULL terminated. Note different spelling: "nul" more accurately refers to ASCII character representation of all bits zero. Sure it's still a zero, mathematically speaking.

    "void" is used for different things depending on context. In a function header, "void" instead of a formal parameter or return type means NO parameter and/or NO return value. On the other hand a void pointer is one that has no type. That is, no pointer arithmetic can be performed on it... there is no implicit size of elements.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    44
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. some basic questions about pointers and structs...
    By brack in forum C Programming
    Replies: 4
    Last Post: 10-16-2010, 09:18 AM
  3. Basic pointers
    By cnewbie1 in forum C Programming
    Replies: 2
    Last Post: 12-03-2009, 06:44 PM
  4. Help if basic question
    By Sshakey6791 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2009, 03:00 PM
  5. A basic question
    By Antigloss in forum C Programming
    Replies: 6
    Last Post: 11-13-2005, 11:42 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21