ARRAY & POINTERS IN C
Exam-friendly notes based on the uploaded handwritten pages
1. ARRAY — QUICK NOTES
• Array: A collection of elements of the same data type stored in contiguous memory locations.
• Declaration: int arr[5]; creates space for 5 integers.
• Initialization: int arr[5] = {10, 20, 30, 40, 50};
• Array indexing starts from 0. For 5 elements, indexes are 0 to 4.
• arr[0] is the first element; arr[4] is the last element.
• Array elements are stored one after another in memory. If an int occupies 4 bytes, addresses increase by 4 bytes.
• Example: if arr[0] is at address 100, then the next elements may be at 104, 108, 112, 116.
• The array name usually represents the address of its first element in expressions: arr behaves like &arr[0].
• Important: an array name is not a modifiable pointer. You cannot do arr++.
2. ARRAY AND POINTER RELATIONSHIP
int arr[5] = {10,20,30,40,50};
int *p = arr; → p stores the address of the first element.
int *q = &arr[2]; → q stores the address of the third element.
Expression Meaning
arr Address of first element in most expressions = &arr[0]
&arr[0] Address of first element
*arr Value of first element = arr[0]
p Address currently stored in p
*p Value stored at the address held by p
&arr[i] Address of arr[i]
arr[i] Value of arr[i]
3. POINTER — BASIC CONCEPT
• Pointer: A variable that stores the memory address of another variable.
• Declaration: int *p; means p is a pointer to an int.
• & is the address-of operator: &x gives the address of x.
• * is the dereference operator: *p accesses the value stored at the address in p.
• Example: int x=10; int *p=&x; → p stores x's address and *p gives 10.
• Changing *p changes the value of x: *p = 20; makes x equal to 20.
4. ADDRESS SPACE & PROCESS (FROM YOUR NOTES)
• When a program is executed, the operating system treats it as a process.
• The OS gives each process its own virtual address space (the memory addresses that the process can use).
• A process cannot normally access arbitrary memory belonging to another process.
• If a program attempts an invalid memory access, the OS may stop it and report an error such as a segmentation fault.
Page 1
5. NULL POINTER
• NULL pointer: A pointer that is intentionally made to point to no valid object.
• Example: int *p = NULL;
• It is good practice to initialize a pointer to NULL when it does not yet have a valid address.
• Never dereference NULL: *p when p is NULL is invalid and can cause a runtime fault.
• Important correction: NULL is a null pointer constant used to represent an invalid/no-object pointer; it is not simply
'the address 0' in every conceptual sense, even though it commonly maps to a zero address representation.
6. POINTER ARITHMETIC
Pointer arithmetic is different from ordinary integer arithmetic. When a pointer moves by 1, it moves by the size of the
type it points to.
int arr[] = {11, 21, 51, 101, 111, 121};
int *p = &arr[0];
If sizeof(int) = 4 bytes and p is at address 100:
p + 1 → 104 p + 2 → 108 p + 3 → 112
Operation Allowed? Meaning
p+n Yes Move n elements forward
p-n Yes Move n elements backward
p++ / ++p Yes Move to next element
p-- / --p Yes Move to previous element
q-p Yes* Number of elements between pointers in the same array
p+q No Adding two pointers is invalid
p*q No Multiplying pointers is invalid
p/q No Dividing pointers is invalid
* Pointer subtraction is defined when both pointers point into the same array object (or one past its end). The result is measured in
elements, not bytes.
7. WHY p + 2 MOVES 8 BYTES
For an int *, the compiler uses sizeof(int) when performing pointer arithmetic.
p = p + 2;
p = p + 2 * sizeof(int);
if sizeof(int) = 4 → p = p + 8 bytes.
Key idea: p + 2 means “move 2 int elements,” not “add the number 2 to the raw address.”
8. POINTER SUBTRACTION — EXAMPLE
int arr[] = {11,21,51,101,111,121};
int *p = &arr[0];
int *q = &arr[4];
Here p points to index 0 and q points to index 4. Therefore q - p gives 4, because four array elements separate them. It
does not give the byte difference 16.
printf("%d\n", q-p); // 4 (conceptually; %td is the portable format for ptrdiff_t)
printf("%d\n", *p); // 11
printf("%d\n", *q); // 111
Page 2
9. ARRAY INDEXING USING POINTERS
• arr[i] is equivalent to *(arr + i).
• &arr[i] is equivalent to arr + i.
• Therefore, array indexing and pointer arithmetic are closely connected.
int arr[] = {10,20,30};
printf("%d", arr[1]);
printf("%d", *(arr + 1));
Both print 20.
10. POINTER DEREFERENCE vs ADDRESS
Expression What it gives
x Value of variable x
&x Address of x
p Address stored in pointer p
*p Value at the address stored in p
&p Address of the pointer variable p itself
11. COMPLETE SIMPLE PROGRAM
#include <stdio.h>
int main()
{
int arr[] = {11, 21, 51, 101, 111, 121};
int *p = &arr[0];
int *q = &arr[4];
printf("%d\n", q - p);
printf("%d\n", *p);
printf("%d\n", *q);
p = p + 2;
printf("%d\n", *p);
q = q - 2;
printf("%d\n", *q);
return 0;
}
Output: 4, 11, 111, 51, 51. After p = p + 2, p points to arr[2] = 51. After q = q - 2, q points to arr[2] = 51.
12. COMMON MISTAKES TO AVOID
• Do not confuse p (address) with *p (value).
• Do not write p + q, p * q or p / q.
• Do not assume p + 1 means one byte. It means one element of the pointed-to type.
• Do not subtract pointers that refer to unrelated arrays.
• Do not dereference an uninitialized pointer or a NULL pointer.
• Do not change an array name with arr++ or arr = ....
• Use a pointer only with a valid, appropriately typed object when dereferencing it.
Page 3
13. VERY SHORT EXAM REVISION
• Array → same-type elements stored contiguously.
• Index → starts from 0.
• arr → usually acts as address of arr[0] in expressions.
• Pointer → stores an address.
• & → gets an address.
• * → dereferences an address to get the value.
• p + n → moves n elements forward.
• p - n → moves n elements backward.
• q - p → number of elements between pointers in the same array.
• p + q, p*q, p/q → invalid pointer arithmetic.
• NULL pointer → intentionally points to no valid object; do not dereference it.
• Segmentation fault → common runtime result of an invalid memory access.
One-line memory trick: arr[i] = *(arr+i) and &arr[i] = arr+i.
Page 4