-2

My Code is below:

#include <stdio.h>

void print_pointer(char **str);
void print_array(char *str[20]);
void print_array2(char str[20][20]);
void print_array3(char str[][20]);

int main(int argc, char *argv[])
{
    char str[20][20] = {"test1", "test2", "test3"};

    print_pointer(str);
    print_array(str);
    print_array2(str);
    print_array3(str);

    return 0;
}

void print_pointer(char **str)
{
    int i = 0;
    for(; i < 20; i++)
    {
        printf("%s", str[i]);
    }
}
void print_array(char *str[20])
{
    int i = 0;
    for(; i < 20; i++)
    {
        printf("%s", str[i]);
    }
}
void print_array2(char str[20][20])
{
    int i = 0;
    for(; i < 20; i++)
    {
        printf("%s", str[i]);
    }
}

void print_array3(char str[][20])
{
    int i = 0;
    for(; i < 20; i++)
    {
        printf("%s", str[i]);
    }
}

When I compile this code, there are two compile errors encountered:

  1. error C2664: 'print_pointer' : cannot convert parameter 1 from 'char [20][20]' to 'char ** '

  2. error C2664: 'print_array' : cannot convert parameter 1 from 'char [20][20]' to 'char *[]'

My question is what's the actually difference between these 4 functions?

Why print_array and print_pointer function could not work while print_array2 and print_array3 could work properly?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
Charles0429
  • 1,406
  • 5
  • 15
  • 31

2 Answers2

7

Yeah, this is where the idea that an array can be treated like a pointer falls apart.

"char[20][20]" denotes an array of 400 characters, laid out in a 20x20 fashion. It is not an array of 20 pointers to arrays of 20 characters each. Thus it would be incorrect to cast a char[20][20] to a char** (and if you did so explicitly, you'd get garbage results).

For the same reason (char[20][20] is not an array of pointers), you can't cast to char *[20].

It is an array of arrays, which is what you've declared for print_array2 and print_array3.

Mike Woolf
  • 1,210
  • 7
  • 11
4

char** is a pointer to a pointer.

You want a pointer to an array (hint: it's not char* [] either as this is an array of pointers equivalent to the above).

You need char (*)[size] (notice the brackets). This will happily take an input of the type char[20][20]. For the sake of completeness char [][size] is also equivalent (in both cases you need to specify the size of the second array).

Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • so why does void print_array(char *str[20]) not work? it's char(*)[] format as you mentioned above. – Charles0429 Jun 14 '13 at 06:08
  • It's not, `(char str[20])` might as well be `(char* str)`. However `(char (*str)[20])` will work for `str[20][20]` – Nobilis Jun 14 '13 at 06:10
  • Can I direct you to this link - http://stackoverflow.com/questions/16724368/how-to-pass-a-2d-array-by-pointer-in-c – Nobilis Jun 14 '13 at 06:12
  • why one dimension array could interchange with pointer, while two dimension array could not? – Charles0429 Jun 14 '13 at 06:13
  • 1
    Because there are differences between pointers and arrays :) You want a pointer to an array, not an array of pointers. – Nobilis Jun 14 '13 at 06:15
  • Stack Overflow has some excellent resources on pointers and arrays in C, have a browse around :) – Nobilis Jun 14 '13 at 06:29