1

I apologize for the simplicity of my question and its similarity to questions already asked. I've looked at questions like it and researched for hours now, and I haven't been able to straighten out whatever misconceptions I have that prevent me from understanding it.

In the following excerpt of code, the warning 'assignment from incompatible pointer type [enabled by default]' occurs at the equals sign on the line commented 'Warning'.

char * readFile(int width, int height)
{
    char mapArray[width][height];
    char *p;
    p = mapArray;     // Warning
    return p;
}

Why is that?

What I currently understand is that 'mapArray' contains the address of the first element of a 2D array of chars. 'p' is defined such that it will, in future, contain the address of another place in memory where a char exists. The value of 'p' is then set to the current value of 'mapArray', so it now contains the address of the first element of said array. The function returns the value of p, which is an address, i.e. a pointer, to a char in memory.

I'd greatly appreciate if someone could tell me where my understanding falls down, and where the incompatible pointer type enters into this.

userManyNumbers
  • 904
  • 2
  • 10
  • 26
  • `p = mapArray;` --> `p = *mapArray;` but It must not return a local address in a function. – BLUEPIXY Aug 17 '14 at 05:11
  • @BLUEPIXY This also made the warning disappear. Thank you very much as well! Does this modification mean that 'p' now contains the address of an address of the first element? Why is the address *in* mapArray local, but the address *of* mapArray not? – userManyNumbers Aug 17 '14 at 05:33
  • 1
    yes p now point to `&mapArray[0][0]`. _Why is the address in mapArray local_ : `mapArray` pointing area will be release at end of scope of the function. – BLUEPIXY Aug 17 '14 at 05:48

2 Answers2

0

mapArray is of return type char ()[height], while the pointer p that you have declared is of type char. So p can only take pointer to one 1-D array. If you write as below then you wouldnt get warning since mapArray[0] returns pointer to 1-D array of height chars:

p = mapArray [0];
Manu
  • 103
  • 6
-1

You cannot use char mapArray[width][height]

The expression must have constant value.

This is also true for char (*p)[height]

You should allocate memory for your array when you don't know its size.

Something like this:

char * readFile(int width, int height)
{
    char **mapArray;
    mapArray = (char **)malloc(sizeof(char *)* width);
    for (int i = 0; i < height; i++)
        mapArray[i] = (char *)malloc(sizeof(char)* width);
    char *p;
    p = *mapArray;
    return p;
}
  • 1
    You have `c++` in mind? Regardless, won't this lead to a memory leak for anything beyond `mapArray[0]`? – Fiddling Bits Aug 17 '14 at 05:40
  • Note that [you should not cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and also note that `sizeof(char)` is redundant, since it is always equal to 1, by definition. – Paul R Aug 17 '14 at 08:12
  • Also note that C has supported VLAs since C99, so the above solution is somewhat obsolete. – Paul R Aug 17 '14 at 08:18