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.

3
  • 3
    Does this answer your question? Array of pointers to an array of fixed size and several other answers found by searching SO Commented Sep 1, 2023 at 17:55
  • please post code one can compile... Commented Sep 1, 2023 at 20:02
  • The last statement should be array[2] = &string;. Strictly speaking, this is an assignment, not initialization. Commented Sep 2, 2023 at 9:01

4 Answers 4

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.

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

Comments

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.

Comments

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]

Comments

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 string[20] = "AEIOU";

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

// Or, after declaration, e.g.:

strcpy(array[2], string);

or

char string[20] = "AEIOU";

char* array[10] =
{
    "Test 0",
    "Test 1",
    string
};

// Or
array[2] = string;

5 Comments

This is not an array of pointers.
@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 For array[2], at the machine-level, a CPU register is loaded with the address of the char array, equal to &array[2][0], and that address is used as a pointer to characters. Nit-picking about what "kind" of char pointer at the compiler-level, misses the much more important point that I am making here, and that is to either use a 2D array of characters or use a 1D array of pointers.
@Coder I see your point. But the compiler and the specification are always nit-picking
@arfneto compiler nit-picking, yes, try c++ :-) Although, I'd rather have the issues at compile-time rather than run-time :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.