C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Pointers in C

String array with pointers

C Program: String array with pointers

C

#include <stdio.h>

 

int main() {

    // Array of pointers to strings

    char *names[] = {"Alice", "Bob", "Charlie", "David", "Emma"};

    int i;

 

    printf("List of Names:\n");

 

    // Loop to display each string

    for (i = 0; i < 5; i++) {

        printf("%s\n", names[i]);

    }

 

    return 0;

}

Output

 
OUTPUT :
List of Names:
Alice
Bob
Charlie
David
Emma

Explanation

  • char *names[] is an array of character pointers, where each pointer points to the first character of a string literal.
  • Each element (names[i]) is a string (or more precisely, a pointer to a character array).
  • The program loops through the array and prints all the strings.