Pointers and Arrays in C
In this post, we’ll explore a short C program that shows how pointers and arrays work together—like a tour guide helping you navigate a city map. First, we’ll look at the big picture, then dive into each line of code.
The Big Picture
Our code snippet demonstrates three fundamental things in C programming:
- Character Arrays – How strings are stored in C.
- Pointers – How to navigate memory addresses.
- String Handling – How to print and manipulate strings.
A Quick Analogy
Think of a character array as a series of hotel rooms, each containing a single character. A pointer is like the hotel’s master key card: it can point to (or “unlock”) the room you want. By moving this pointer to different positions, you can access different characters in the array.
Core Concepts
Character Arrays
In C, strings are essentially arrays of characters ending with a special character called the null terminator ('\0'
). It’s as if the hotel rooms line up in a row, and the last room is always empty ('\0'
) to mark the end of the string.Pointers
A pointer holds the address of a variable or array element. When dealing with arrays, pointers let you move from one element to another by incrementing the pointer—like moving your master key card to the next room in the hallway.String Handling
In C, functions likeprintf
can print entire strings (%s
) or a single character (%c
).
Detailed Walkthrough of the Code
#include <stdio.h>
int main() {
char a[] = "Art";
char* p = NULL;
p = a;
printf("%s\n", a);
printf("%c\n", *p);
printf("%c\n", *a);
printf("%s\n", p);
for(int i = 0; a[i] != '\0'; i++)
printf("%c", a[i]);
return 0;
}
Initialization of Character Array
char a[] = "Art";
- Creates a character array
a
that holds"Art"
followed by a hidden'\0'
terminator. - Think of it as three hotel rooms labeled
A
,r
, andt
, plus an empty one ('\0'
).
- Creates a character array
Pointer Declaration and Assignment
char* p = NULL; p = a;
- Declares a pointer
p
and initially sets it toNULL
(no address). - Then assigns
p
to point to the first element ofa
. - Now
p
is like the master key card for thea
array.
- Declares a pointer
Printing the String
printf("%s\n", a);
- Prints the entire string stored in
a
, which is"Art"
.
- Prints the entire string stored in
Printing the First Character via Pointer
printf("%c\n", *p);
- Uses the
*
operator to get the character at the address pointed to byp
, which is'A'
.
- Uses the
Printing the First Character via Array Name
printf("%c\n", *a);
- The array name
a
can be treated like a pointer to its first element, so this also prints'A'
.
- The array name
Printing the String via Pointer
printf("%s\n", p);
- Since
p
points to the start ofa
, this again prints"Art"
.
- Since
Printing Characters Using a Loop
for(int i = 0; a[i] != '\0'; i++) printf("%c", a[i]);
- This loop continues until it reaches the null terminator
'\0'
. - It prints each character one by one, resulting in
"Art"
.
- This loop continues until it reaches the null terminator
Putting It All Together
If we visualize the array a
:
Index: 0 1 2 3
Value: 'A' 'r' 't' '\0'
Address: a+0 a+1 a+2 a+3
p = a;
meansp
starts ata+0
(the address of'A'
).*p
→'A'
.*a
→'A'
.- Printing
p
as a string prints from'A'
until'\0'
, so"Art"
.
What the Output Looks Like
Art
A
A
Art
Art
Test Your Understanding
What is the value stored in
p
afterp = a;
?
It’s the address of the first element ofa
.What would happen if we replaced
char a[] = "Art";
withchar *a = "Art";
?
Thena
points to a string literal stored in read-only memory (depending on the compiler/implementation). You can still print it, but modifying it may cause a crash or undefined behavior.How would the output change if we initialized
p
directly withchar *p = a;
instead of using a separate assignment?
The result is the same—p
would still point to the start ofa
, and the program’s output wouldn’t change.
Conclusion and Summary
This code snippet is a handy demonstration of how pointers and character arrays work in C. By thinking of arrays as a row of rooms and pointers as a master key, we see how easy it is to navigate and print each character. Understanding pointers is crucial for any C programmer, as it opens the door to more advanced memory management and data structures.
Reference
For a deeper dive into pointers and arrays in C, refer to:
- The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie (2nd Edition), particularly Chapter 5 (pointers and arrays). See especially pages 96–99 on pointers and arrays.