The issue is that you are not allocating any space for those names. You need to initialize each element in the array if you intend to use it with scanf.

char* names[6];
for( int i = 0; i < 6; ++i )
    names[i] = malloc( 256 * sizeof *names[i] ); // or some other max value

scanf( "%s", names[1] );

Otherwise those pointers will be pointing anywhere in your memory, and attempting to read/write those locations will eventually result in a segmentation fault.

Answer from K-ballo on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-pointers-to-strings-in-c
Array of Pointers to Strings in C - GeeksforGeeks
November 14, 2025 - Each array element will act as a pointer to the first character of an individual string.
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ array-of-pointers-to-strings-in-c โ€บ index.html
Array of Pointers to Strings in C - C Programming Tutorial - OverIQ.com
Although the characters of a particular string literal are always stored in contiguous memory location. The following program demonstrates how to access strings literal in the array of pointers to string and in the process prints the address of each string literal.
Discussions

c - array of pointers to strings - Stack Overflow
In your code names is an array of 6 pointers to char. Now each of these pointers can store the starting point (the address of the first character) of a new string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Pointer to string array in C - Stack Overflow
Playing with pointers in C is fun (not really). I have several arrays of strings I want to declare in an easy way, preferably something like: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why can you have an pointer to array of strings in C - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... First one is an array of pointers to strings. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I create (and use) an array of pointers to an array of strings in C? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I need to create an array of pointers that will each point to an array of strings. The base, is a size 2 array of strings (the length of the strings ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ what are array of pointers?
r/C_Programming on Reddit: What are Array of Pointers?
December 17, 2024 -

So i am learning command lines arguments and just came cross char *argv[]. What does this actually do, I understand that this makes every element in the array a pointer to char, but i can't get around as to how all of this is happening. How does it treat every other element as another string? How come because essentialy as of my understanding rn, a simple char would treat as a single contiguous block of memory, how come turning this pointer to another pointer of char point to individual elements of string?

Top answer
1 of 14
79
If you have something like const char *argv[] = {"./a.out", "hello", NULL}; You get this in memory: argv โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ptr โ”œโ”€โ”€โ”€โ–บโ”‚ ./a.out โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ ptr โ”œโ”€โ” โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ NULL โ”‚ โ””โ”€โ–บโ”‚ hello โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ You can see that the "./a.out" and "hello" are stored in other places in memory, not inside the array. Thatโ€™s what a pointer isโ€”a value that can point to another location in memory. Or it can be NULL, which does not point to anything.
2 of 14
13
The only thing thatโ€™s guaranteeing is contiguous are the double pointers. If you dereference the first element, that is a pointer to a char, and there might be more chars further along if you move down that row with pointer arithmetic. Dereferencing your second element would be a pointer to another char which might make up a string along that row too (if you defined it as such). Those derefenced pointers have no reason to be contiguous in memory. Itโ€™s just that the pointers for themselves are. The only contiguity guarantee here from that statement alone is the array of pointers to char *.
What's the use of pointers-to-pointers? May 11, 2023
r/C_Programming
3y ago
How to write array of pointers to arrays Aug 15, 2018
r/cpp_questions
8y ago
Arrays in c using pointers Jan 27, 2023
r/learnprogramming
3y ago
A Basic Guide to Pointers in C Programming Dec 19, 2023
r/programming
2y ago
More results from reddit.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c-program-to-print-array-of-pointers-to-strings-and-their-address
C program to print array of pointers to strings and their address
March 19, 2021 - The C program demonstrating the concept of printing array of pointers to string and the addresses too is given below โˆ’ ยท #include<stdio.h> #include<string.h> void main(){ //Declaring string and pointers, for loop variable// int i; char *a[5]={"One","Two","Three","Four","Five"}; //Printing values within each string location using for loop// printf("The values in every string location are : "); for(i=0;i<5;i++){ printf("%s ",a[i]); } //Printing addresses within each string location using for loop// printf("The address locations of every string values are : "); for(i=0;i<5;i++){ printf("%d ",a[i]); } }
๐ŸŒ
DEV Community
dev.to โ€บ missmati โ€บ pointers-arrays-strings-in-c-52h3
Pointers , Arrays & Strings in C - DEV Community
October 11, 2022 - Similar to the 2D array we can create the string array using the array of pointers to strings. Basically, this array is an array of character pointers where each pointer points to the stringโ€™s first character.
Top answer
1 of 2
11

The issue is that you are not allocating any space for those names. You need to initialize each element in the array if you intend to use it with scanf.

char* names[6];
for( int i = 0; i < 6; ++i )
    names[i] = malloc( 256 * sizeof *names[i] ); // or some other max value

scanf( "%s", names[1] );

Otherwise those pointers will be pointing anywhere in your memory, and attempting to read/write those locations will eventually result in a segmentation fault.

2 of 2
5

In your code names is an array of 6 pointers to char. Now each of these pointers can store the starting point (the address of the first character) of a new string. This means you can store the starting addresses of 6 different strings in your names variable.

But when you use a loop to initialize each of these strings, you need to inform the machine HOW long each string might be, so that it can allocate a continuous block of addresses whose first address can then be stored in your pointer to refer to your string. Thus, you must allocate a certain size you think should be sufficient to store your string (eg: 256 bytes, 1 byte being 1 character). In the absence of this, the machine doesn't know where to store all the bytes of your string and throws a segmentation fault due to illegal memory access.

Thus to do this, each of your 6 pointers must be allocated some space to store a string. This will be done in your loop using malloc(). Based on @K-ballo's code:

char* names[6];
int max_length = 256; // The maximum length you expect
for( int i = 0; i < 6; ++i )
    names[i] = malloc( max_length * sizeof(char) ); // allocates max_length number of bytes

scanf( "%s", names[1] );

So now you basically have a 6 different blocks of max_length continuous char addresses that are each referred to by names[i]. When you do the scanf() it reads the bytes from standard input and puts then into these allocated bytes in memory referred to by names[1].

I had a difficult time at the start understanding all this, so just thought an elaborate explanation would help. :)

๐ŸŒ
CodinGeek
codingeek.com โ€บ home โ€บ array of pointers to string - c programming language
Array of pointers to string in C Language | Codingeek
February 24, 2021 - Pointers contain addresses of the particular variable that we need. An array of pointers stores the addresses of all the elements of the array and an array of string pointers stores the addresses of the strings present in the array.
Find elsewhere
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ pointer to string array in c, you should know
Pointer to string array in C, you should know - Aticleworld
February 25, 2023 - Similar to the 2D array we can create the string array using the array of pointers to strings. Basically, this array is an array of character pointers where each pointer points to the stringโ€™s first character.
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ arrays โ€บ array of pointers to strings
Array of Pointers to Strings
May 15, 2024 - In C and C++, an array of pointers to strings is a common technique used to store multiple strings. Each element of the array is a pointer to a string (char array), allowing for a flexible and efficient way to manage a collection of strings ...
๐ŸŒ
Lynxbee
lynxbee.com โ€บ home โ€บ array of pointers to strings in c: explained with clear code examples
Array of Pointers to Strings in C: Explained with Clear Code Examples - Lynxbee โ€“ Linux, Embedded, Android, WordPress, SEO, Digital Marketing
June 16, 2025 - This is especially useful in ... ... In simple terms, an array of pointers to strings is an array where each element is a pointer to the first character of a string (i.e., char*)....
๐ŸŒ
RIT
se.rit.edu โ€บ ~swen-250 โ€บ activities โ€บ MicroActivities โ€บ C โ€บ mu_string_ptr_update โ€บ distrib โ€บ index.html
C Strings with Arrays and Pointers
February 27, 2025 - Arrays can be declared with an ... = "Hello!" ; // a 7 element array - 6 characters in Hello! + terminating NUL ยท An array name is a constant pointer to the first (0th) array element; thus: mesg == &mesg[0] ; // address of the first character in the message....
Top answer
1 of 2
28

There are two way of working with array of characters (strings) in C. They are as follows:

char a[ROW][COL];
char *b[ROW];

Pictorial representation is available as an inline comment in the code.

Based on how you want to represent the array of characters (strings), you can define pointer to that as follows

    char (*ptr1)[COL] = a;
    char **ptr2 = b;

They are fundamentally different types (in a subtle way) and so the pointers to them is also slightly different.

The following example demonstrates the different ways of working with strings in C and I hope it helps you in better understanding of array of characters (strings) in C.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ROW 5
#define COL 10

int main(void) 
{
    int i, j;
    char a[ROW][COL] = {"string1", "string2", "string3", "string4", "string5"};
    char *b[ROW];

    /*

    a[][]

      0   1   2   3   4   5   6     7    8   9
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 1 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 2 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 3 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 4 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 5 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+

    */  

    /* Now, lets work on b */    
    for (i=0 ; i<5; i++) {
        if ((b[i] = malloc(sizeof(char) * COL)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    }

    strcpy(b[0], "string1");
    strcpy(b[1], "string2");
    strcpy(b[2], "string3");
    strcpy(b[3], "string4");
    strcpy(b[4], "string5");

    /*

       b[]              0   1   2   3   4   5   6    7     8   9
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 1 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 2 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 3 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 4 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 5 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+

    */

    char (*ptr1)[COL] = a;
    printf("Contents of first array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", *ptr1++);


    char **ptr2 = b;
    printf("Contents of second array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", ptr2[i]);

    /* b should be free'd */
    for (i=0 ; i<5; i++)
        free(b[i]);

    return 0;
}
2 of 2
1

What would be the correct way to solve this problem?

Well, the correct way would be to use a library specifically designed for dealing with multilanguage interfaces - for instance gettext.

Another way, though patchier, would be to use a hash table (also known as "dictionary" or "hash map" or "associative map" in other languages/technologies): Looking for a good hash table implementation in C

It's probably not the answer you were looking for, but you've asked the wrong question to the right problem.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-strings-in-c
Array of Strings in C - GeeksforGeeks
July 23, 2025 - So, each string is only take the required amount of memory. Though some extra space is required to store the pointers. arr is an array of pointers, each pointing to a string.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ string pointer in c
String Pointer in C - Scaler Topics
January 16, 2024 - An array is a contiguous block of memory, and when pointers to string in C are used to point them, the pointer stores the starting address of the array. Similarly, when we point a char array to a pointer, we pass the base address of the array to the pointer. The pointer variable can be dereferenced ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-create-a-pointer-for-strings-using-c-language
How to create a pointer for strings using C language?
March 15, 2026 - In C programming, you can create pointers to strings using arrays of pointers. An array of pointers to strings is an array where each element is a pointer that holds the base address of a string literal or character array.
Top answer
1 of 2
3

from cdecl:

declare foo as array of pointer to array 2 of pointer to char

char *(*foo[])[2];

So, foo[0] is a pointer to array 2 of char *

That is the array, but for your use, you want:

declare foo as pointer to array 2 of pointer to char;

char *(*foo)[2];

Now you can do:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *(*foo)[2];

    printf("How many people?\n");
    int n; scanf("%d", &n);

    foo = malloc(sizeof *foo * n);

    for (int i = 0; i < n; i++) {
        char bufFirstName[1024];
        char bufLastName[1024];

        printf("Please insert the #%d first and last name:\n", i+1);

        scanf("%s %s", bufFirstName, bufLastName); 

        char *firstName = malloc(strlen(bufFirstName) + 1);
        char *lastName = malloc(strlen(bufLastName) + 1);

        strcpy(firstName, bufFirstName);
        strcpy(lastName, bufLastName);

        foo[i][0] = firstName;
        foo[i][1] = lastName;
    }

    for (int i = 0; i < n; i++) {
        printf("Name: %s LastName: %s\n", foo[i][0], foo[i][1]);
    }

    return 0;
}

Compile with -std=c99

Note that using scanf, strcpy, strlen like that is unsafe because there can be a buffer overflow.

Also, remember to free your malloc's!

2 of 2
0

Not that your approach is wrong, but have you considered instead using a struct that includes first and last name, and then malloc'ing based on the number of names the user will enter:

typedef struct {
    char* first;
    char* last;
} person;

person* people = malloc(num * sizeof(*person));

This just simplifies pointer interaction. While the way you are doing it is a good exercise in understanding pointers better, it may not be the easiest way to understand.

If you are unable to use structs, you should instead be doing:

char** people;  
people = malloc(2*num*sizeof(char*));  

for (int i = 0; i < 2*num; i++) 
    people[i] = malloc(MAX_NAME_SIZE*sizeof(char));

Now you would need to reference the i th person via:

first name: people[i*2 + 0] 
last name: people[i*2 + 1]
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-pointers-in-c
Array of Pointers in C - GeeksforGeeks
July 23, 2025 - We can access the value of these integers by first selecting the array element and then dereferencing it to get the value. One of the main applications of the array of pointers is to store multiple strings as an array of pointers to characters.