← Back to C Course | Chapter 7: Pointers | Lesson 4 of 9

C Pointers & Arrays

An array's name acts like a pointer to its first box, like a street's starting address. You can walk through the array by moving the pointer.
Syntax
c
data_type array_name[] = {value1, value2, value3};
data_type *pointer_name = array_name;

*(pointer_name + index);  // same as array_name[index]

Array Name as a Pointer

This is why sizeof(arr) and sizeof(arr[0]) behave differently even though arr itself 'looks like' a pointer in expressions -- the array name only decays into an actual pointer value when it's used in most expressions, but sizeof still sees the full array type.

Example: Array Name as a Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Print formatted text to the screen
	printf("%d", *arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Elements using Pointers

Both notations compile down to the exact same machine instructions, so choosing between arr[i] and *(arr + i) is purely a matter of readability -- most C code favors bracket notation for clarity except in performance-sensitive pointer-walking code.

Example: Accessing Elements using Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	// Print formatted text to the screen
	printf("%d", *(arr + 1));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Scanning Arrays with Pointer Increments

This pattern (ptr = arr; while (...) { ...; ptr++; }) avoids recomputing an address from scratch on every access the way arr[i] technically does, which mattered more on older compilers that didn't optimize indexing as aggressively as modern ones do.

Example: Scanning Arrays with Pointer Increments

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	int *ptr = arr;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("%d ", *ptr);
		ptr++;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Array Elements via Pointers

*(ptr + i) = newValue and arr[i] = newValue produce identical results -- both ultimately compute an address and write to it, so which style you use is again a matter of preference and context.

Example: Modifying Array Elements via Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	int *ptr = arr;
	*(ptr + 1) = 99;
	// Print formatted text to the screen
	printf("%d", arr[1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Comparing Pointers

Comparing pointers from two different arrays (or two unrelated variables) is technically undefined behavior in C, even though it will often work by accident -- only compare pointers that both point somewhere within the same array or one past its end.

Example: Comparing Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	int *p1 = &arr[0];
	int *p2 = &arr[2];
	// Print formatted text to the screen
	printf("%d", p1 < p2);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assigning to an array name such as arr = p;, which is not allowed because an array name is not a modifiable pointer.
  2. Using sizeof on a pointer to find the array length, which gives the pointer size instead.
  3. Writing *p++ when you mean (*p)++, which moves the pointer instead of incrementing the value.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.