Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being:
1) the sizeof operator
o sizeof(array) returns the amount of memory used by all elements in array
o sizeof(pointer) only returns the amount of memory used by the pointer variable itself
2) the & operator
o &array is an alias for &array and returns the address of the first element in array
o &pointer returns the address of pointer
3) a string literal initialization of a character array
o char array = “abc” sets the first four elements in array to ‘a’, ‘b’, ‘c’, and ‘\0’
o char *pointer = “abc” sets pointer to the address of the “abc” string (which may be stored in read-only memory and thus unchangeable)
4) Pointer variable can be assigned a value whereas array variable cannot be.
int a; int *p; p=a; /*legal*/ a=p; /*illegal*/
5) Arithmetic on pointer variable is allowed.
p++; /*Legal*/ a++; /*illegal*/
Please refer Difference between pointer and array in C? for more details.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Double Pointer (Pointer to Pointer) in C
- Pointer to an Array | Array Pointer
- What is a Pointer to a Null pointer
- Difference between pointer to an array and array of pointers
- Sum of array using pointer arithmetic
- Difference between pointer and array in C?
- How to declare a pointer to a function?
- 'this' pointer in C++
- Multidimensional Pointer Arithmetic in C/C++
- When do we pass arguments by reference or pointer?
- How to write C functions that modify head pointer of a Linked List?
- Function Pointer in C
- Opaque Pointer
- Passing by pointer Vs Passing by Reference in C++
- void pointer in C / C++
- NULL pointer in C
- C | Pointer Basics | Question 1
- C | Pointer Basics | Question 2
- C | Pointer Basics | Question 3
- C | Pointer Basics | Question 4