Notes on pointer in C
- A pointer is a memory address.
- Assume size of int and char in the compiler as 4 and 1 Byte respectively.
- & : address of
- * : Content of (de-referencing operator)
- int x; //x occupies 4 byte memory
- int *p; //p is a pointer and address of an integer. It means *p (content of p) will be an integer.
- p= &x; //now, p is address of x. p is pointer to integer x.
- p++; //move pointer to next element. As p is the pointer to integer, it's value will increase by 4.
- int **q; // q is pointer to pointer. (address of address)
- q=&p; //q is now pointer to pointer to (address of) p.
- Size of a pointer is fixed for a compiler irrespective of pointer type.
- p=(char*)p; //now p is address of char, content of p (*p) will be of size 1 Byte.
I recommend to go through this test for checking yourself : https://www.geeksforgeeks.org/c-language-2-gq/pointers-gq/
Comments
Post a Comment