C में Array और Pointer में अंतर (Table के साथ)
Arrays और pointers similar दिखते हैं लेकिन sizeof, assignment, memory और behaviour में अलग हैं. पूरी comparison table और famous sizeof proof program.
Why the confusion exists
arr becomes &arr[0], and arr[i] equals *(arr + i). But an array is not a pointer — and 4 differences prove it.Difference 1: sizeof (the famous proof)
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("sizeof(arr) = %zu\n", sizeof(arr)); /* 20: whole array */
printf("sizeof(p) = %zu\n", sizeof(p)); /* 8: just a pointer */
return 0;
}
sizeof sees the truth: arr is a 20-byte block of 5 ints; p is an 8-byte address holder. If arrays were pointers, both would print 8.
Difference 2: Assignment
int arr[5], brr[5];
int *p, *q;
p = q; /* OK: pointers can be reassigned */
p = arr; /* OK: array decays to pointer */
arr = brr; /* ERROR: array name is not modifiable */
arr++; /* ERROR: cannot move an array */
A pointer is a variable — it can point anywhere, any time. An array name is a fixed label for a fixed block of memory; it can never be reassigned or incremented.
Difference 3: Memory
- Array: one allocation — the elements themselves. There is no separate "arr variable" storing an address.
- Pointer: its own memory (8 bytes) that stores an address, separate from whatever it points to.
Difference 4: Behaviour with strings
char arr[] = "hello"; /* modifiable copy in the array */
char *p = "hello"; /* points to read-only literal */
arr[0] = 'H'; /* OK */
p[0] = 'H'; /* CRASH: undefined behaviour */
Complete comparison table
| Point | Array | Pointer |
|---|---|---|
| sizeof | Total bytes of all elements | Size of an address (4/8 bytes) |
| Reassignment | Impossible | Any time |
| arr++ / p++ | Error | Valid |
| Memory | Block of elements only | Separate 8-byte address holder |
| &name | Address of whole array (int (*)[5]) | Address of the pointer variable |
| String literal init | Writable copy | Read-only reference |
Confusion क्यों होता है
arr बन जाता है &arr[0], और arr[i] = *(arr + i). लेकिन array pointer नहीं है — और 4 अंतर इसे prove करते हैं.अंतर 1: sizeof (famous proof)
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("sizeof(arr) = %zu\n", sizeof(arr)); /* 20: पूरा array */
printf("sizeof(p) = %zu\n", sizeof(p)); /* 8: सिर्फ pointer */
return 0;
}
sizeof सच दिखाता है: arr पांच ints का 20-byte block है; p आठ-byte का address holder. अगर arrays pointers होते, तो दोनों 8 print करते.
अंतर 2: Assignment
int arr[5], brr[5];
int *p, *q;
p = q; /* OK: pointers reassign हो सकते हैं */
p = arr; /* OK: array pointer में decay होता है */
arr = brr; /* ERROR: array name modifiable नहीं है */
arr++; /* ERROR: array को move नहीं कर सकते */
Pointer एक variable है — कभी भी, कहीं भी point कर सकता है. Array name memory के fixed block का fixed label है; कभी reassign या increment नहीं हो सकता.
अंतर 3: Memory
- Array: एक allocation — खुद elements. कोई अलग "arr variable" नहीं जो address store करे.
- Pointer: अपनी memory (8 bytes) जो address store करती है, उससे अलग जिसे वह point करता है.
अंतर 4: Strings के साथ behaviour
char arr[] = "hello"; /* array में modifiable copy */
char *p = "hello"; /* read-only literal को point करता */
arr[0] = 'H'; /* OK */
p[0] = 'H'; /* CRASH: undefined behaviour */
पूरी comparison table
| Point | Array | Pointer |
|---|---|---|
| sizeof | सभी elements के total bytes | Address का size (4/8 bytes) |
| Reassignment | Impossible | कभी भी |
| arr++ / p++ | Error | Valid |
| Memory | सिर्फ elements का block | अलग 8-byte address holder |
| &name | पूरे array का address (int (*)[5]) | Pointer variable का address |
| String literal init | Writable copy | Read-only reference |
Frequently Asked Questions
क्या C में array name pointer है?
नहीं. Array name ज़्यादातर expressions में पहले element के pointer में decay होता है, लेकिन sizeof, & और reassign न हो पाना साबित करते हैं कि यह अलग array type है.
sizeof(arr) और sizeof(p) अलग क्यों होते हैं?
sizeof(arr) सभी elements के total bytes देता है (int[5] के लिए 20), जबकि sizeof(p) सिर्फ address का size देता है (64-bit पर 8 bytes).
char *p = "hello"; p[0]='H'; crash क्यों करता है?
String literal read-only memory में रहता है; pointer से उस पर लिखना undefined behaviour है. Literal से initialize हुआ char array इसके बजाय writable copy रखता है.