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;
}
Answer from Sangeeth Saravanaraj on Stack Overflow
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-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.
Discussions

Pass pointer to array of strings? (Stumped)
There are several ways of doing this C doesn't know the dimensions of an array, so when trying to pass it, that information would be lost and so it isn't possible. The very hardcoded way would be void loadFile(FILE* file, char buffer[ROWS][LENGTH]){ // load the file } I assume this is not what you want to do A different way of doing it would be: void loadFile(FILE* file, size_t size, void* buffer){} loadFile(theFile, sizeof(file), file); Or you could just do it like fread will work as well (which eventually you have to call anyway) void loadFile(FILE* file, size_t elementSze, size_t count, void* buffer) { } loadFile(theFile, sizeof(char), ROWS * LENGTH, file) More on reddit.com
🌐 r/C_Programming
18
1
June 2, 2024
Creating a pointer to an array of strings in C - Stack Overflow
I would like to know if the method of creating a pointer to an array of strings is the same with a one-dimensional. Most examples i found don't really answer my question so here it is: Suppose we ... More on stackoverflow.com
🌐 stackoverflow.com
How to make a pointer to an array of string in c? - Stack Overflow
Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want to make a pointer to an array of string(matrix[10][10]), but I get "initialization from incompatible pointer type" problem so how to fix it? 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
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 is unknown at start). For example an array of 2 More on stackoverflow.com
🌐 stackoverflow.com
🌐
OverIQ
overiq.com › c-programming-101 › array-of-pointers-to-strings-in-c
Array of Pointers to Strings in C - C Programming Tutorial - OverIQ.com
It is important to note that each element of the sports array is a string literal and since a string literal points to the base address of the first character, the base type of each element of the sports array is a pointer to char or (char*).
🌐
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 - Basically, this array is an array of character pointers where each pointer points to the string’s first character.
🌐
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....
🌐
Reddit
reddit.com › r/c_programming › pass pointer to array of strings? (stumped)
r/C_Programming on Reddit: Pass pointer to array of strings? (Stumped)
June 2, 2024 -

I've tried everything! I've never been this stuck!

static char file[ROWS][LENGTH];
void loadFile(FILE* file, char** buffer){
      // load the file
}
void saveFile(FILEI* file, char** buffer){
      // save the file
}

int main(){
      FILE* theFile = fopen("the path.txt", "r");
      loadFile(theFile, file);
      fclose(theFile);
      return 0;
}

I've tried using char** buffer, char* buffer[], char* buffer[][] in the function prototype

and I've tried passing file, &file[0], file[], &file into the function call.

but I get "warning: passing argument 2 of 'loadFile' from incompatible pointer type"

I need a way to make file[ROWS][LENGTH]; work. How do I do this?

🌐
Stack Overflow
stackoverflow.com › questions › 73292290 › creating-a-pointer-to-an-array-of-strings-in-c
Creating a pointer to an array of strings in C - Stack Overflow
The correct pointer declaration is char (*suits)[9] = a; which says that suits is a pointer to an array of characters of length 9. ... void printString(size_t cols, char (*strings)[cols], size_t pos) { printf("Printing in function: String at ...
Find elsewhere
🌐
CodinGeek
codingeek.com › home › array of pointers to string - c programming language
Array of pointers to string – C Programming Language
February 24, 2021 - The array contains the base address of every String element in the array. ... This array stores the array base address of “tree” in arr[0]. Similarly the base address of “bowl” in arr[1] and so on. There are many advantages to using a string pointer array over a string array.
🌐
DEV Community
dev.to › missmati › pointers-arrays-strings-in-c-52h3
Pointers , Arrays & Strings in C - DEV Community
October 11, 2022 - Based on how you want to represent ... code, ... ** To access the string array, we need to create a pointer to the array and initialize the pointer with the array....
Top answer
1 of 3
2

matrix is of type char (*)[10] (pointer to array of 10 char).

pMatrix is of type char * (pointer to char).

There is a type mismatch when you use:

char *pMatrix = matrix;

You try to assign a pointer to an array of 10 char to a pointer to char.

This is why you get the warning:

warning: initialization of 'char *' from incompatible pointer type 'char (*)[10]'


You need to dereference matrix

char *pMatrix = *matrix;

to get a pointer to char.

Could you say why my version (char * pMatrix = matrix;) works for 1D array, but stops working for 2d array?

matrix' type is different when declared as char matrix[10] and parameter of a function. Then matrix is actual equivalent to char *. The assignment from char * to char * is correct.

Take a look at:

  • Difference between passing array and array pointer into function in C

  • Passing an array as an argument to a function in C

  • C pointer notation compared to array notation: When passing to function

Note that if you provide an amount of elements like in your case doesn't matter. char matrix[10] is equal to char matrix[] which is furthermore equal to char *matrix.


No guarantee if your algorithm works beside that. If you got problems with that, please ask a different question.

2 of 3
1

There are so many ways but I prefer this one:

void rotate(char matrix[10][10]){
  
  char *pMatrix = matrix[0];
  for(int j = 0; j < 10; j ++){
    for(int i = 9; i >= 0; i --){
      *pMatrix = matrix[i][j];
      pMatrix ++;
    }
  }
}

Or you could use this for better understanding

void rotate(char matrix[10][10]){
  
  char * pMatrix = &matrix[0][0];
  for(int j = 0; j < 10; j ++){
    for(int i = 9; i >= 0; i --){
      *pMatrix = matrix[i][j];
      pMatrix ++;
    }
  }
}
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]
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. :)

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 129552-pointer-array-strings.html
pointer to array of strings
August 27, 2010 - Therefore, the strcpy is copying the strings from array "strings" into never never land. Mainframe assembler programmer by trade. C coder when I can. ... #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> char **arr( char *s[], char *p[] ); int main () { char *pointer[2]; char *strings[2] = { "hello\n", "world\n" }; printf("%s", arr( strings, pointer )[1] ); } char **arr( char *s[], char *p[] ) { int i; for( i = 0; i < 3; ++i ) p[i] = s[i]; return p; }
🌐
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 ...
🌐
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 - First, let us understand what are the arrays of pointers in C programming language. It is an array whose elements are ptrs to the base add of the string.
🌐
Stack Overflow
stackoverflow.com › questions › 20179177 › array-of-pointer-pointing-to-arrays-of-strings
c - array of pointer pointing to arrays of strings - Stack Overflow
Temp=malloc(Count(' ',Sentences[Note])*sizeof(char *)) only overrides the content each time,and when i assign the pointers,all of them point to the last value(the value overrides).what can i do? ... For a sentence you only need a char pointer or a char pointer array to store multiple sentences.
🌐
Dyclassroom
dyclassroom.com › c › c-pointers-and-strings
C - Pointers and Strings - C Programming - dyclassroom | Have fun learning :-)
The pointer variable strPtr is at memory location 8000 and is pointing at the string address 5000. The temporary variable is also assigned the address of the string so, it too holds the value 5000 and points at the starting memory location of the string "Hello". We can create a two dimensional array and save multiple strings in it.