0

I'm in the late stages of a c program that is a dynamic word search. I get the "warning: assignment makes integer from pointer without a cast [enabled by default]" when I compile these lines:

**grid = gridReal;
**items = itemsReal;
**solution = solutionReal;

My main up to that point looks like this:

int main()
{
char gridReal[50][50];/*Array to hold puzzle*/
char itemsReal[100][50];/*Array to hold words*/
char solutionReal[50][50];/*Array to hold solution*/
int dimension;/*Width and height*/
int x, y;/*Counters for printing*/

char** grid = (char**)malloc(sizeof(char)*100*50);
char** items = (char**)malloc(sizeof(char)*100*50);
char** solution = (char**)malloc(sizeof(char)*100*50);

**grid = gridReal;
**items = itemsReal;
**solution = solutionReal;

Any ideas on how to sort this out?

5
  • **grid is of type char, you are trying to assign a char (*)[50] (the type to which gridReal decays). Obviously, it won't compile. What are you trying to achieve with these assignments? Commented Feb 22, 2014 at 21:37
  • You can't copy arrays using the = operator in C, and you haven't initialized them anyway. If you're just trying to assign the address of the array to your pointers, why are you doing all those malloc()s and throwing your references away? Commented Feb 22, 2014 at 21:37
  • Why are you allocating those three areas twice? The declaration char gridReal[50][50] creates the space. Calling malloc again and then an assignment to grid is odd.... Commented Feb 22, 2014 at 21:38
  • 1
    Don't use casts, when you obviously not know what you are doing. Compiler diagnostics are there for a reason. Commented Feb 23, 2014 at 0:21
  • Ok. Any suggestion then? Commented Feb 23, 2014 at 19:59

2 Answers 2

1

It seems that you want to copy the memory from e.g. gridReal to grid. First of all, you have to remember that a pointer to pointer to some type is not the same as an array or arrays of the same type. The memory layout is simply not compatible. Secondly, you can't simply assign an array to a pointer like that, and expect the data to be copied.

Instead you allocate the outer "dimension" first, then in a loop allocate (and copy) the second "dimension":

char **grid = malloc(50));

for (int i = 0; i < 50; ++i)
{
    grid[i] = malloc(sizeof(gridReal[0]);
    memcpy(grid[i], gridReal[i], sizeof(gridReal[i]));
}
Sign up to request clarification or add additional context in comments.

Comments

1

**grid is of type char. You can't assign gridReal, which is of type char (*)[50] after decay , to **grid. Always remember that arrays are not pointers. A pointer to pointer doesn't mean a 2D array.

5 Comments

Actually, after decaying, gridReal is of type char (*)[50].
@FilipeGonçalves; Correct. I missed that.
Thanks so far guys. Was able to type cast to get rid of the warnings: grid = (char**)gridReal; items = (char**)itemsReal; solution = (char**)solutionReal; So now it's on to clearing up a seg fault..
So your solution is to use an ugly cast to cheat on the compiler? If this is how you fix your code, I'm not surprised you're having a segfault.
Yeah, I'm not very good sir. Any ideas instead of the casting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.