Sheedy75
Sheedy75

Reputation: 83

C: Incompatible pointer type initializing

I'm trying to learn C. I'm currently on pointers so I've decided to write some code to see how it works. However the code works as intended (i.e. it adds chars a-j in an array and prints it on the console) but I'm get warnings about incompatible pointer assignment. I've added the warnings as comments on the lines were the warnings are given.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, const char * argv[]) {

    #define MAX 10

    char* c[MAX]; // pointer to the 1st element in the array
    char* pointer = &c; // Warning: Incompatible pointer types initializing 'char *' with an expression of type 'char *(*)[10]'
    for (int j = 0; j < 10; j++)
    {
        *pointer = 'a' + j;
        pointer++;
    }

    pointer = &c; // Warning: Incompatible pointer types assigning to 'char *' from 'char *(*)[10]'
    for (int j = 0; j < 10; ++j)
    {
        printf("i[%d] = %c\n", j, *pointer);
        ++pointer;
    }

    return 0;
}

Can someone please explain why I'm getting these warnings?

char* pointer = &c; & pointer = &c;

I understand the code I'm writing as follows declare a char pointer called pointer and assign the address of the 1st element in the array to the pointer.

PS! please don't comment on how to achieve the same result with better written code as I'm trying to learn here about pointers and arrays. So while this may be a verbose way to achieve this result, I feel it is syntactically correct, so if I've got this wrong please help me understand.

Upvotes: 5

Views: 14455

Answers (3)

juanchopanza
juanchopanza

Reputation: 227390

c is an array of pointers to char:

char* c[MAX];

The type of this expression

&c

is "pointer to a length-MAX array of pointers to char", or

char *(*)[MAX]

The compiler is warning you that you're initializing a pointer of one type from one of another type.

It isn't clear what you intended to do with your code, but a valid initialization of a char* with no conversions would be

char* pointer = c[0];

A valid initialization from &c (pointer to an array of pointers), would be

char *(*pointer)[MAX] = &c;

Alternatively, you can let the array "decay" to a pointer to its first element by omitting the address-of operator &", but this yields a pointer to pointer:

char** pointer = c;

Upvotes: 3

ouah
ouah

Reputation: 145829

Change:

char* pointer = &c

to

char* pointer = c[0];

c elements type is char * but &c type is a pointer to c type.

EDIT: This will fix your warning but the problem is you are dealing with the wrong types first.

Instead of:

char* c[MAX];
char* pointer = &c;

use:

char c[MAX];
char *pointer = c;

In your program you are storing characters, so you need an array of char and not an array of char * elements.

Same for pointer = &c; which then has to be pointer = c;.

Upvotes: 5

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

From the code it seems that you declared the array the wrong way, it should be

char c[MAX];

which is an array of char, not an array of char pointers, then

char *pointer;
pointer = c;

where pointer is a pointer to the first element of c, and the rest of the code will work as you expected.

Also, I am a bit curious why did you define the MAX macro inside main(), just in case you didn't know, it's not being declared it's substituted by the prepropcessor before compilation, so where you put it in the code is completely irrelevant.

Upvotes: 2

Related Questions