0

I am trying to assign values to a 2d int array in C.

int **worldMap;

I want to assign each row to the array, so I do this in a loop:

worldMap[0][sprCount] = split(tmp.c_str(), delim);
sprCount++;

The problem is I get an error the above line saying can not convert int* to int.

Here is the code to create the 2D array:

int** Array2D(int arraySizeX, int arraySizeY)
{
    int** theArray;
    theArray = (int**) malloc(arraySizeX*sizeof(int*));
    for (int i = 0; i < arraySizeX; i++)
        theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
    return theArray;
}

I want to take the return pointer of this function (shown above) and put it into the Y dimension.

int* split(const char* str, const char* delim)
{
    char* tok;
    int* result;
    int count = 0;
    tok = strtok((char*)str, delim);
    while (tok != NULL)
    {
        count++;
        tok = strtok(NULL, delim);
    }

    result = (int*)malloc(sizeof(int) * count);

    count = 0;
    tok = strtok((char*)str, delim);
    while (tok != NULL)
    {
        result[count] = atoi(tok);
        count++;
        tok = strtok(NULL, delim);
    }
    return result;
}
Synaps3
  • 1,597
  • 2
  • 15
  • 23
  • 3
    `int **worldMap;` is not a 2D array, don't try to treat it as one. Also, [do not cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). –  Oct 06 '13 at 06:27
  • Short version: I think you want `worldMap[sprCount]`. Long version: `worldMap` is a pointer-to-pointer, and assigned a dynamic array of pointers. Dereferencing it with `worldMap[0]` allows access to the pointer at that slot, which is a `int*` representing a dynamic array of `int`. Dereferencing *that* with `[sprCount]` results in the `int` lvalue in slot `sprCount`. Thus your error. An `int` is not an `int*`. If you want the `sprCount` "row" in your pointer array, use `worldMap[sprCount]`. (and fair warning, free the old occupant of that slot or you'll leak memory like a sieve leaks water). – WhozCraig Oct 06 '13 at 06:40
  • What's with `tmp.c_str()`? That is normally a part of C++… – Jonathan Leffler Oct 06 '13 at 06:40
  • Your `split()` function should simply return an `int`, not allocate and return an `int *`. You've already allocated the space for an `int` in the array. Alternatively, you don't need the second level of allocation in the `Array2d()` function and the assignment should be `worldMap[sprCount] = split(...)`. – Jonathan Leffler Oct 06 '13 at 06:42
  • @JonathanLeffler That looks like a pretty deliberate row-creation function. I think the *original* row-allocations filled with indeterminate values in `Array2D` may be a better candidate to throw out. Hard saying without some real context. – WhozCraig Oct 06 '13 at 06:45
  • @WhozCraig: Yes, I just wrote that alternative into my comment, shortly before seeing your comment suggesting the same. Sure and certain it is that there is confusion about where data is being assigned. It could also be a 2D array of rows, so it needs to be a triple pointer — hopefully, though, this isn't a [3-Star Programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer) at work. – Jonathan Leffler Oct 06 '13 at 06:47
  • @JonathanLeffler Did you notice the secondary `strtok()` loop that isn't going to work because the first one already filled it with terminators? The OP will get at-most *one* token from this for each row, no matter how many were initially there. – WhozCraig Oct 06 '13 at 06:53
  • @WhozCraig: I didn't look at `split()` harder than the signature and noting some `malloc()`...There's probably a ton of stuff that needs to be fixed. I'm still extremely curious about the `tmp.c_str()`...that smacks of some C++ to me. Overall, I decided it's too late for me to be interested in fixing this question, this time. It clearly isn't an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) and I'm feeling too lazy to create one. – Jonathan Leffler Oct 06 '13 at 06:57
  • There are too many wrongs in this code. You better learn something about multidimensional arrays, ponters, variable scopes. Then try again. – kotlomoy Oct 06 '13 at 13:07
  • I think I may have gotten closer. Can someone just tell me why the split function is not looping on the second while. I noticed it was modifying the string so I did a strcpy to another string at the beginning, but it still doesn't do the second loop. – Synaps3 Oct 06 '13 at 20:52

0 Answers0