1

compiler gives me the folowing error:

[Warning] assignment from incompatible pointer type

This was my try;

char (*array[10])[20],string[20] = "AEIOU"; array[2] = string;

Array of pointers to an array of fixed size i already find this other question but wasn't useful.

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 3
    Does this answer your question? [Array of pointers to an array of fixed size](https://stackoverflow.com/questions/37348741/array-of-pointers-to-an-array-of-fixed-size) and several other answers found by searching SO – Rob Sep 01 '23 at 17:55
  • please post code one can compile... – arfneto Sep 01 '23 at 20:02
  • The last statement should be `array[2] = &string;`. Strictly speaking, this is an assignment, not initialization. – nielsen Sep 02 '23 at 09:01

4 Answers4

2

The most "natural" way to define an array of pointers to strings:

char *array[10];

This defines array as an array of 10 pointers to char.

Then

array[i] = string;

to make element i point to the first character of string.

From this assignment, array[i] is basically an alias for string.


Or if you really want a pointer to an array:

char (*array[10])[20];
array[i] = &string;

Note the use of the pointer-to operator & here, to get a pointer to the array itself. Also note that this is very different from the more "natural" way shown above. It all depends on what you actually want to accomplish, what your actual use-case and problem is.


And what happens with your currently shown code is that string decay to a pointer to its first element, so string is the same as &string[0]. Which has the type char *.

You currently define array as an array of pointers to arrays, so each element of array have the type char (*)[20]. That's the type you get with &string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You are defining char (*array[10])[20], it is declaring an array of 10 pointers, and each pointer points to an array of 20 characters (correct).

But in the following line:

array[2] = string;

it will show the warning "assignment from incompatible pointer type", bcz this is not allowed without proper typecast because the types are incompatible in C/C++. In your example, you are trying to assign a array of character string to an element of an array of pointers to arrays of characters. So it will cause warning.

You can change to use this to ignore the warning:

array[2] = &string;

array[2] will hold the address of of the string array, it is point to an array of characters. Therefore, this assignment is compatible.

ldn
  • 82
  • 3
1

C compilers do not charge by line used.

Declare one thing per line.

  • it is easier to read
  • it is easier to locate a name or type a value
  • it is easier to change
  • in case of an error you just get the precise line number from the compiler

compare

char (*array[10])[20],string[20] = "AEIOU"; array[2] = string

with

#include <stdio.h>
int main(void)
{
  char string[20] = "AEIOU";
  char(*array[10])[20];

  array[2] = string;
}

line by line

    char string[20] = "AEIOU";

Is is ok but you should always use const as a reminder to yourself. If you do that an by mistake try to assign some value to string compiler will warn you, not someone at the phone when your code crashes.

    char array[2] = string;

Each array is a pointer to a char[20] so an & is missing. Write:

    char array[2] = &string;

not an initializer

    array[2] = &string;

This is an assignment. Not an initializer.

This is an initilizer:

    char(*array[10])[20]  = {
    NULL,
    NULL,
    NULL,
    "A Test",
    string,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};

This program

#include <stdio.h>
int main(void)
{
    char string[20] = "AEIOU";
    char(*array[10])[20]  = {
    NULL,
    NULL,
    NULL,
    "A Test",
    string,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};

    array[2] = &string;
    printf("\
    #%d\t\"%s\"\n\
    #%d\t\"%s\"\n\
    #%d\t\"%s\"\n",
        2, array[2],
        3, array[3],
        4, array[4]);

    return 0;
}

prints

    #2  "AEIOU"
    #3  "A Test"
    #4  "AEIOU"

Note that string is const char[20]

arfneto
  • 1,227
  • 1
  • 6
  • 13
0

To the OP: you're over-thinking the array declaration, each array[N] is already an array pointer, e.g., array[2] is already an array pointer:

char array[10][20] = 
{
  "Test",   // array[0]
  "Test2",  // array[1]
  "AEIOU"   // array[2]
};

After declaration, e.g. :

strcpy(array[2], "AEIOU");

or

char string[20] = "AEIOU";
strcpy(array[2], string);
Coder
  • 133
  • 7
  • This is not an array of pointers. – nielsen Sep 02 '23 at 11:48
  • @Coder No, it is not. `array[N]` is `char(*)[20]` as declared. It is a pointer to an array of precisely 20 `char`. And you code has no relation to the question. Mind the `*` in the original code. – arfneto Sep 02 '23 at 19:44