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.

🌐
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)
February 9, 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?

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί array-of-pointers-to-strings-in-c
Array of Pointers to Strings in C - GeeksforGeeks
November 14, 2025 - In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings in C.
🌐
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.
🌐
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
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]); } }
🌐
RIT
se.rit.edu β€Ί ~swen-250 β€Ί activities β€Ί MicroActivities β€Ί C β€Ί mu_string_ptr_update β€Ί distrib β€Ί index.html
C Strings with Arrays and Pointers
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....
🌐
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 the array of strings, you can define a pointer to access the string from the array.
🌐
Cprogramming
cboard.cprogramming.com β€Ί c-programming β€Ί 129552-pointer-array-strings.html
pointer to array of strings
what do you think of this memory allocation and free, using function: I have problem with sizeof function, it always return 4 byte, i guess because pc is 32 bit, how to get the size dinamically of these array and pointer?? ... #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> char **arr( char *s[], char *p[] ); int main () { char **pointer; int size = sizeof(pointer); pointer = malloc(size * sizeof(char *)); char *p[] = { "cane", "dog", "gatto", "cat" }; int size1 = sizeof(p)/sizeof(size1); char **c; c = arr( p, pointer); int size2 = sizeof(c); printf("size=%d\nsize
Find elsewhere
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 ++;
    }
  }
}
🌐
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 ...
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. :)

🌐
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 ...
🌐
Villanova
csc.villanova.edu β€Ί ~mdamian β€Ί Past β€Ί csc8400fa15 β€Ί notes β€Ί 05_Pointers.pdf pdf
1 CSC 8400: Computer Systems Pointers, Arrays and Strings in C Overview
ADDRESS-OF operator. ... Logically, there is no a[6]! ... Arrays: C vs. Java ... C Does Not Do Bounds Checking! ... Stack vs. Data ... Arrays vs. Pointers ... All these declarations are equivalent! Try them out. ... C vs. Java Strings
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί array-of-strings-in-c
Array of Strings in C - GeeksforGeeks
July 23, 2025 - In C we can use an Array of pointers instead of having a 2-Dimensional character array. It is a single-dimensional array of Pointers where pointer to the first character of the string literal is stored.
🌐
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.
🌐
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 16
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 16
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 *.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί array-of-pointers-in-c
Array of Pointers in C - GeeksforGeeks
July 23, 2025 - After that, we can assign a string of any length to these pointers. ... Note: Here, each string will take different amount of space so offset will not be the same and does not follow any particular order. This method of storing strings has the advantage of the traditional array of strings.
🌐
W3Schools
w3schools.com β€Ί c β€Ί c_pointers_arrays.php
C Pointers and Arrays
This way of working with arrays might seem a bit excessive. Especially with simple arrays like in the examples above. However, for large arrays, it can be much more efficient to access and manipulate arrays with pointers. It is also considered faster and easier to access two-dimensional arrays with pointers. And since strings are actually arrays, you can also use pointers to access strings.