Pointer to a two Dimensional array of pointers

I have a program

#include <stdio.h>
int main()
{
    int i , j;
    char *str[2][3] = {{"john", "alice", "bobby"},
                       {"peter", "mark", "anthony"}};
    char (*ptr)[3] = str;  //Compiler throws Warning here 
    for ( i = 0 ; i < 2 ; i++)
    {
        for ( j = 0 ; j < 3; j++)
        {
            printf("%s ", str[i][j]); //This works fine.
            printf("%s ", ptr[i][j]); //segmentation fault here
        }
    }
}

My intension is to use a pointer to a two dimensional array of pointers. However the compiler throws error as follows;

test.c: In function ‘main’:
test.c:7:19: warning: initialization from incompatible pointer type [enabled by default]
 char (*ptr)[3] = str;
                   ^

Also, I am getting segmentation fault while running. My question is how would I have a pointer which points to a two dimensional array of pointers?



Read more here: https://stackoverflow.com/questions/64944287/pointer-to-a-two-dimensional-array-of-pointers

Content Attribution

This content was originally published by Franc M at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: