0

I am trying to pass a double-dimensional array to a function, but it throws this error all the time. My code is below:

void initialize_centroids(int *,int,int);(Initialization)

initialize_centroids(centroid[0],noofcentroids,newnoofvar);(inside my main)

void initialize_centroids(int *carray,int p,int q)
{
    int i,j;
    for(i=0;i<p;i++)
    {
        for(j=0;j<q;j++)
        {
           carray[i][j]=i;
        }
    }

    return;
}
1
  • 'throws this error all the time'? The first thing you need to do (especially as someone doing a Masters in CS) is understand the difference between compile-time errors -- which are not "thrown", and of course happen "all the time" if you haven't eliminated the error -- and runtime errors. And of course int * is not in any way "a double-dimensional array". Commented Sep 14, 2012 at 4:27

2 Answers 2

1

You're passing a pointer to an int (int*), but you're trying to use two subscripts. Is it a 2D array? Then the parameter needs to be a pointer to a pointer to an int: int**

void initialize_centroids(int**, int, int);

int centroid[][] = ....  // wherever this comes from
initialize_centroids(centroid, noofcentroids, newnoofvar);(inside my main)



void initialize_centroids(int** carray, int p, int q)
{
    int i,j;
    for(i=0;i<p;i++)
    {
        for(j=0;j<q;j++)
        {
            carray[i][j]=i;
        }
    }

    return;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Didn't get you.. So should I initialize in my function as int .Tried this but I get one more error. cannot convert âintâ to âintâ for argument â1â to âvoid initialize_centroids(int*, int, int)â
cannot convert int ()[100] to int* for argument â1â to âvoid initialize_centroids(int**, int, int)
I don't see any memory allocated for centroid, and you're passing a two-dimensional array to a function expecting a pointer to an array of int *'s. I suggest that you test your answer before offering it.
I just had that in there for the signature. Edited.
1

Don't de-reference centroid when passing in. Doing that will only pass your first row. Do this:

initialize_centroids( centroid, noofcentroids, newnoofvar );

Then you need to use the correct type here:

void initialize_centroids( int **carray, int p, int q )

Your array is presumably an int**. That means it's a pointer to a location in memory that contains an array of int* pointers. Each of those pointers (I assume) references memory that has been allocated one row of your array.

Now, once you obtain one of these pointers by doing carray[i] you have selected row i. You now have an int* which means it points to an array of int.

Finally, carray[i][j] selects a specific integer from column j on row i.

[edit]

Theory about what might be going wrong... You may not be allocating a 2D array in a dynamic sense, or indeed it might be a 1D array and you are supposed to use stride-lengths etc. Here is a solution that you can use for allocating a 2D array dynamically, and indexing it as carray[i][j].

Note: Some people have gotten upset at me for this method in the past due to alignment concerns, but I have never had issues. I think they were just having a grumpy day. For those who are worried about alignment, it's easy to adjust for that, but is too complicated for this example.

int** allocate_array2d( int rows, int cols )
{
    int i, **arr, *data;
    arr = (int**)malloc( rows * sizeof(int*) + rows*cols*sizeof(int) );
    data = (int*)(arr + rows);
    for( i = 0; i < rows; i++ ) {
        rows[i] = data;
        data += cols;
    }
    return arr;
}

void free_array2d( int** arr ) {
    free((void*)arr);
}

Now, in your main:

int** centroid = allocate_array2d( noofcentroids, newnoofvar );
initialize_centroids( centroid, noofcentroids, newnoofvar );

[more...]

Okay.Yes I allocated memory like this centroid[100][100] – Bobby 11 mins ago

In that case, you should declare:

void initialize_centroids( int carray[100][100], int p, int q )
{
    //...
}

No need to use my dynamic array example

4 Comments

Oh, it appears you have declared the array on the stack and are using it as if it was declared on the heap. Does the array have fixed-size (ie hard-coded) dimensions? I will post code for dynamic allocation.
Okay.Yes I allocated memory like this centroid[100][100]
@Bobby Well in that case, you should declare initialize_centroids( int carray[100][100], int p, int q ). No need to use my dynamic array example.
Yes, that's because int centroid[100][100] is not an array of int*. You must either use my dynamic 2D array example (allocate_array2d(100,100)), or pass the array as specified in my previous comment.

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.