Question

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?

Was it helpful?

Solution

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]));
}

OTHER TIPS

**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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top