Thread: related to pointers

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    related to pointers

    I'm not newbie in C programming but there's something still making me "hesitated" in my programming what that's the term "pointers" .

    So, I've three questions thats conclude whole things and I hope I get a full understandable answers.

    1. if I have a null pointer like int *p=NULL, I cant write *p = 5 (any value) because the pointer p is NULL and if I do * (NULL) I will get an error right? I'm still missing the concept of using the pointer "NULL" and when defining a pointer as NULL and when not.

    2. lets assume I have a struct named"KID" and have a variable d, so when I make (KID ** d) what does that d mean? an array of struct? what is the meaning of ** at all? I'm still misunderstanding the behind concept of using ** .

    3. How can I change a pointer as long as we know that a pointer is something constant in memory, so by **p for example I can change a pointer that I want?


    thanks in advance and I appreciate your assistance beforehand.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by RyanC View Post
    I'm not newbie in C programming but there's something still making me "hesitated" in my programming what that's the term "pointers" .

    So, I've three questions thats conclude whole things and I hope I get a full understandable answers.

    1. if I have a null pointer like int *p=NULL, I cant write *p = 5 (any value) because the pointer p is NULL and if I do * (NULL) I will get an error right? I'm still missing the concept of using the pointer "NULL" and when defining a pointer as NULL and when not.
    Well, importantly, NULL is a placeholder address for use when you aren't pointing at anything. Normally, you would take the address from some existing integer, like n, and do p = &n; or get memory from malloc(). Then the address that the pointer holds won't be NULL any more and dereferencing makes more sense. One way to think of what dereferencing is to think of an analogy. For example, I have a real name, but you don't know what it is, and you still would like to contact me. Fortunately, I have ways to get in touch with me, like my phone number. Well, my phone number is like a memory address that leads to me, so when you call, you contact me. The act of dereferencing is a lot like placing a call. The actual memory you want to access may not have a name like I do, or in computer terms, like a normal variable, but you can look it up by address.

    What is so special about NULL is that it is an address that will lead to a detectable error, or a crash if it is dereferenced. Using GDB, in this program, I dereferenced a NULL pointer for you.
    Code:
    (gdb) r
    Starting program: C:\Users\jk\Desktop\a.exe
    [New Thread 11172.0x2cc8]
    [New Thread 11172.0x2c88]
    
    Program received signal SIGSEGV, Segmentation fault.
    0x00000000004015c9 in main () at sandbox.c:4
    4          printf("%d\n", *p);
    (gdb)
    Why would someone want this? It's important to know that all pointers are used correctly. Pointers can be easily used to leverage security attacks against a system. Finding and eliminating pointer dereferences where there shouldn't be any is important. The good advice goes that once you are done using the pointer, you should make it NULL. This means that the old address will be cleared out and no one can access the old memory.

    2. lets assume I have a struct named"KID" and have a variable d, so when I make (KID ** d) what does that d mean? an array of struct? what is the meaning of ** at all? I'm still misunderstanding the behind concept of using ** .
    It would be a pointer to a struct KID pointer. Whether you use that pointer to simulate an array is up to you. Importantly, it is impossible to tell what one it actually is, you just need to read documentation or try to glean it from code. This is because making an array from a call like d = malloc(sizeof(KID*) * bignumber); is valid and so is something a lot simpler than that:
    Code:
    KID k;
    KID *p = &k;
    KID **d = &p;
    Normally, it's a lot more useful. For example, most memory you will actually use should be on the heap, so you end up using a bunch of KID pointers. Once you need an array of them, you will see KID**, but it is just a dynamic, or flexible version of something like KID *classroom[16];

    3. How can I change a pointer as long as we know that a pointer is something constant in memory, so by **p for example I can change a pointer that I want?
    Personally, I'm not sure I understand your question, so if you want to try asking again, or waiting for other answers, that would be good.
    Last edited by whiteflags; 11-21-2017 at 07:37 PM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    A pointer represents a memory address/location, the value of a pointer is a memory-address. So for example an ampersand in front of "int i" also represents a pointer:
    int i;
    &i is thus a pointer but as a pointer it represents a pointer-constant since you can not change it's address (only the integer value i can be changed). Obviously you can assign any integer
    value to i but you can NOT change the address of i. The following is NOT possible: &i = someOtherValue;

    Pointer variables can point to different addresses like for example:
    int *iPtr, i, j;

    iPtr = &i; // iPtr points to the memory location of the integer variable i
    iPtr = &j; // Now iPtr points to the memory location of the integer variable j

    If you only declare a variable without assigning it with a value, like for example "int i;" the value of i is undefined. Although at that stage, i is not yet provided with a value, it does have a value (which can be any integer value), therefore it's value is undefined.
    The same holds for pointers, like for example you declare "int *iPtr;" It's value can be anything, in other words, it can point to any memory location, So if you assign a value to such an un-initialized pointer, it is very well possible that you try to store a value in a memory location to which your program has no access. In that case your operating system will signal your program that something bad has happened.

    The statement iPtr = NULL explicitly means that iPtr is NOT pointing to any valid/allocated memory and thus de-referencing it is illegal.

    int *iPtr is a pointer variable which like any variable has a specific address.
    As mentioned above: "A pointer represents a memory address/location" .... so it is also possible to declare a pointer which points to the memory address of a pointer variable, like in:
    int i, j;
    int *iPtr, *jPtr; // pointers to integers
    int **iPptr; // pointer to a pointer to integer (you can make the list as long as you want, for example int ***complexPtr is a pointer to a pointer to a pointer to an integer)

    iPtr = &i; // iPtr now gets as value the address of i, in other words iPtr points to the memory location of integer variable i
    *iPtr = 5; // the memory location to which iPtr points now gets the value 5 and since iPtr points to the memory location of i, this results in assigning the value 5 to i --> i ==5

    iPptr = &iPtr; // iPptr now gets as value the address of iPtr, in other words iPptr points to the memory location of pointer variable iPtr

    jPtr = *iPptr; // jPtr now gets as value the value of iPptr, in other words jPtr==iPtr and they now both point to the same address (which is integer variable i)
    // now i == *iPtr == *jPtr == **iPptr


    ........, I'm not sure whether this makes it a bit clearer for you

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    Why would someone want this? It's important to know that all pointers are used correctly. Pointers can be easily used to leverage security attacks against a system. Finding and eliminating pointer dereferences where there shouldn't be any is important. The good advice goes that once you are done using the pointer, you should make it NULL. This means that the old address will be cleared out and no one can access the old memory.


    It would be a pointer to a struct KID pointer. Whether you use that pointer to simulate an array is up to you. Importantly, it is impossible to tell what one it actually is, you just need to read documentation or try to glean it from code. This is because making an array from a call like d = malloc(sizeof(KID*) * bignumber); is valid and so is something a lot simpler than that:
    Code:
    KID k;
    KID *p = &k;
    KID **d = &p;
    Normally, it's a lot more useful. For example, most memory you will actually use should be on the heap, so you end up using a bunch of KID pointers. Once you need an array of them, you will see KID**, but it is just a dynamic, or flexible version of something like KID *classroom[16];
    I got you Mr; so if I understand you; so I can use a NULL pointer for instance for checking if I arrived the final of "linked list" or not ? meaning with that; lets assume I have a *L pointer to a linked list, writing an statement like
    Code:
     if (L==NULL) : printf("final of linked lists)
    and not writing in the condition
    Code:
     if (*L==NULL)
    because if L==NULL then *L isn't defined! ... right?
    Another thing, if I given a pointer as input to a specific function
    (not main) and have done some manipulation with that pointer for instance I have changed the value of that pointer so after I finish from that function will the value gone? meaning will it be locally or the value still saved in that address? as we know in C programming if I defined a variable in a function its defined in the function itself and will gone once I get out from it ..

    Well, I don't know how to thank you Mr! ; I have understood 99% of using NULL pointer and pointers issue itself!

    thanks alot

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by ddutch View Post
    A pointer represents a memory address/location, the value of a pointer is a memory-address. So for example an ampersand in front of "int i" also represents a pointer:
    int i;
    &i is thus a pointer but as a pointer it represents a pointer-constant since you can not change it's address (only the integer value i can be changed). Obviously you can assign any integer
    value to i but you can NOT change the address of i. The following is NOT possible: &i = someOtherValue;

    Pointer variables can point to different addresses like for example:
    int *iPtr, i, j;

    iPtr = &i; // iPtr points to the memory location of the integer variable i
    iPtr = &j; // Now iPtr points to the memory location of the integer variable j

    If you only declare a variable without assigning it with a value, like for example "int i;" the value of i is undefined. Although at that stage, i is not yet provided with a value, it does have a value (which can be any integer value), therefore it's value is undefined.
    The same holds for pointers, like for example you declare "int *iPtr;" It's value can be anything, in other words, it can point to any memory location, So if you assign a value to such an un-initialized pointer, it is very well possible that you try to store a value in a memory location to which your program has no access. In that case your operating system will signal your program that something bad has happened.

    The statement iPtr = NULL explicitly means that iPtr is NOT pointing to any valid/allocated memory and thus de-referencing it is illegal.

    int *iPtr is a pointer variable which like any variable has a specific address.
    As mentioned above: "A pointer represents a memory address/location" .... so it is also possible to declare a pointer which points to the memory address of a pointer variable, like in:
    int i, j;
    int *iPtr, *jPtr; // pointers to integers
    int **iPptr; // pointer to a pointer to integer (you can make the list as long as you want, for example int ***complexPtr is a pointer to a pointer to a pointer to an integer)

    iPtr = &i; // iPtr now gets as value the address of i, in other words iPtr points to the memory location of integer variable i
    *iPtr = 5; // the memory location to which iPtr points now gets the value 5 and since iPtr points to the memory location of i, this results in assigning the value 5 to i --> i ==5

    iPptr = &iPtr; // iPptr now gets as value the address of iPtr, in other words iPptr points to the memory location of pointer variable iPtr

    jPtr = *iPptr; // jPtr now gets as value the value of iPptr, in other words jPtr==iPtr and they now both point to the same address (which is integer variable i)
    // now i == *iPtr == *jPtr == **iPptr


    ........, I'm not sure whether this makes it a bit clearer for you
    thanks anyway

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Your question:
    Another thing, if I given a pointer as input to a specific function
    (not main) and have done some manipulation with that pointer for instance I have changed the value of that pointer so after I finish from that function will the value gone? meaning will it be locally or the value still saved in that address? as we know in C programming if I defined a variable in a function its defined in the function itself and will gone once I get out from it ..
    Answer:
    Code:
    #include <stdio.h>
    
    int globalInt = 1;
    
    void someFunc3(int **pt)
    {
        *pt = &globalInt;
    }
    
    void someFunc2(int *pt)
    {
        pt = &globalInt;
        printf("Inside someFunc2, *pt=%d\n", *pt);
    }
    
    void someFunc1(int *pt)
    {
        *pt = 8;
    }
    
    void main()
    {
        int i;
        int *iPtr;
    
        iPtr = &i;
        *iPtr = 5;
    
        printf("Initial value of i=%d\n", i); // i = 5
    
        someFunc1(iPtr);
        printf("after call to someFunc1() i=%d\n", i); // Will show: i=8
    
        someFunc2(iPtr);
        printf("after call to someFunc2() *iPtr=%d .... iPtr keeps its value (which is &i)\n", *iPtr); // Will still show: *iPtr=8
    
        someFunc3(&iPtr);
        printf("after call to someOtherFunc() *iPtr=%d\n", *iPtr); // Will show: *iPtr=1
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by RyanC View Post
    I got you Mr; so if I understand you; so I can use a NULL pointer for instance for checking if I arrived the final of "linked list" or not ? meaning with that; lets assume I have a *L pointer to a linked list, writing an statement like
    Code:
     if (L==NULL) : printf("final of linked lists)
    Right, a linked list can be NULL at the end. That is one way of terminating your lists. There are other ways.

    and not writing in the condition
    Code:
     if (*L==NULL)
    because if L==NULL then *L isn't defined! ... right?

    thanks alot
    Actually if you compile with a mistake like that, the compiler should catch it. Because L does not have the same resulting type as the dereference, *L.

    See here:
    Code:
    C:\Users\jk\Desktop>gcc -ansi -c sandbox.c
    sandbox.c: In function 'main':
    sandbox.c:11:9: error: invalid operands to binary == (have 'intlist {aka struct list}' and 'void *')
      if (*L == NULL)
             ^
    
    C:\Users\jk\Desktop>more sandbox.c
    #include <stdio.h>
    
    int main(void) {
            typedef struct list {
                    struct list *next;
                    int data;
            } intlist;
    
            intlist *L = NULL;
    
            if (*L == NULL)
                    puts("if you see this, then the compiler missed an error.");
    
            return 0;
    }
    Last edited by whiteflags; 11-23-2017 at 01:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A difference between 2 statements related to pointers
    By Muhammad Yahya in forum C Programming
    Replies: 2
    Last Post: 04-05-2016, 10:38 AM
  2. Another pointers / struct/ array related question
    By patishi in forum C Programming
    Replies: 3
    Last Post: 09-11-2013, 05:25 PM
  3. Pointers related.
    By Helix in forum C++ Programming
    Replies: 4
    Last Post: 08-02-2003, 02:23 PM
  4. Related to Pointers
    By Extol in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 08:41 PM
  5. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 12:54 PM

Tags for this Thread