What is the difference between an array and a pointer in C?
| Array | Pointer |
|---|---|
| Fixed block of memory with its own size | A variable which stores an address |
sizeof(arr) gives the total bytes | sizeof(p) gives the pointer size |
| Name cannot be changed to point elsewhere | Can point to any place |
| Memory is allocated automatically | Memory is given by the programmer, or it points to an existing one |
#include <stdio.h>
int main(void) {
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%zu %zu\n", sizeof(arr), sizeof(p));
printf("%d %d\n", arr[2], p[2]);
return 0;
}
Output:
Output of the above program is
20 8
3 3
The array name acts like a pointer in most expressions (it “decays”), and that is why they look alike. See Pointers and Arrays.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |