0

What am I doing wrong here? The first one works like a charm the second one crashes.

char *t[3][4] = {
  {"one", "two", "three", "Four"},
  {"Five", "Six", "Seven", "Eight"},
  {"Nine", "Ten", "Eleven", "Twelve"}
};
int i,j;
for (i=0;i<3;i++) {
  for (j=0;j<4;j++) {
    printf("%s\n",t[i][j]);
  }
}

Same array just want to use in function:

foo(t, 3, 4);

char **foo(char **t, int row, int col) {
  int i,j;
  for (i=0;i<row;i++) {
    for (j=0;j<col;j++) {
      printf("%s\n",t[i][j]);
    }
  }
}

Process terminated with status -1073741510 (0 minute(s), 8 second(s));

5
  • char **foo(char **t, int row, int col) --> void foo(int row, int col, char *t[row][col]) (call foo(3, 4, t);) Commented Jul 3, 2016 at 2:49
  • 1
    @BLUEPIXY: You don't even have to specify 'row' there since C's row major indexing will automatically take care of the first dimension, if the second is passed to it. Commented Jul 3, 2016 at 3:12
  • I'm not sure if you meant to pass char *** (which would be incorrect as a 2D array of char * is not a pointer-to-pointer-to-pointer-to-char) or just thought a char ** would refer to an array of char * regardless of the number of dimensions (also not right, but a different misunderstanding)... Commented Jul 3, 2016 at 3:21
  • @Shiva It is not necessary, but I think that descriptive. Commented Jul 3, 2016 at 3:55
  • 1
    Did the compiler not warn you about type mismatches? It should have done. Fundamentally, the type of t in the argument list of foo does not match the type of t, the array. When I compiled your code, I got errors (because I convert warnings to errors with -Werror) like: asp19.c:22:9: error: passing argument 1 of ‘foo’ from incompatible pointer type [-Werror=incompatible-pointer-types] identifying the line foo(t, 3, 4); and observing asp19.c:3:6: note: expected ‘char **’ but argument is of type ‘char * (*)[4]’ referring to void foo(char **t, int row, int col); Commented Jul 3, 2016 at 4:19

2 Answers 2

2

Dealing with pointers is cumbersome, especially with string matrices. It's best to write your function in such a way that the compiler clearly knows what's being passed to it.

Change your function signature to void foo(int row, int col, char* t[][col]) { ... }, and correspondingly, call your function as foo(3, 4, t);. You're clearly telling the compiler what's being passed.

Sign up to request clarification or add additional context in comments.

Comments

0

This is my code witch worked with char **foo(char **t, int row, int col). The problem was that i had a ** array with a pointer to its beginning, and you cant traverse it like a simple 2d array like t[i][j]. But you can use i*col to reach the row you want, and the j is the locating the j-th element in i-th row. Extra note that the size of the containing strings doesnt matter because you have addresses in the array . Thanks for the helpful comments witch led me to the correct answer.

for (i=0;i<row;i++)
    {
        for (j=0;j<col;j++)
        {
        printf("%s",*(t+i*col+j)); 
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.