200===Dev Language/C

Pointers and Arrays in C

블로글러 2024. 6. 9. 14:08

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:

  1. Character Arrays – How strings are stored in C.
  2. Pointers – How to navigate memory addresses.
  3. 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

  1. 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.

  2. 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.

  3. String Handling
    In C, functions like printf 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;
}
  1. 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, and t, plus an empty one ('\0').
  2. Pointer Declaration and Assignment

    char* p = NULL;
    p = a;
    • Declares a pointer p and initially sets it to NULL (no address).
    • Then assigns p to point to the first element of a.
    • Now p is like the master key card for the a array.
  3. Printing the String

    printf("%s\n", a);
    • Prints the entire string stored in a, which is "Art".
  4. Printing the First Character via Pointer

    printf("%c\n", *p);
    • Uses the * operator to get the character at the address pointed to by p, which is 'A'.
  5. 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'.
  6. Printing the String via Pointer

    printf("%s\n", p);
    • Since p points to the start of a, this again prints "Art".
  7. 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".

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; means p starts at a+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

  1. What is the value stored in p after p = a;?
    It’s the address of the first element of a.

  2. What would happen if we replaced char a[] = "Art"; with char *a = "Art";?
    Then a 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.

  3. How would the output change if we initialized p directly with char *p = a; instead of using a separate assignment?
    The result is the same—p would still point to the start of a, 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.
728x90