0

I don't know the array size when compiling so I declared a variable count and created an array arr[count][count], I will incrase this count variable while program is running and I will reallocate it's memory before doing that. But I couldn't create a function that takes this arr[count][count]. How can i do that ? When i did this like:

void add_friend(int friends[][*count], int p1, int p2)
{

}

compiler gives an error: count undeclared here.

  • 3
    The common pattern is passing the dimensions as parameters to the function. – millimoose Jan 07 '14 at 01:17
  • Actually, you only need the number of columns (unless you want to do bounds checking, in which case you also need rows). – Scott Hunter Jan 07 '14 at 01:22
  • You might want to have a look at [this](http://stackoverflow.com/questions/20899515/trouble-understanding-what-elements-are-passed-when-passing-multidimensional-arr/20918811#20918811). – kuroi neko Jan 07 '14 at 01:46

5 Answers5

0

You can't do what you are asking directly at least not in C. But as @millimoose suggests, if you know the dimensions (which can be passed as ints), you can do a little arithmetic to figure out where friends[p1][p2] is. If there are C elements per row, then you know that friends[r][0] is C*r ints from the start of friends, and can treat that row as a simple int array.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

The count should be accessible by friends, to do that you can either declare it as a global variable or as an argument of the function.

void add_friend(int count, int friends[][count], int p1, int p2)
{
    // ...
}

int main(void)
{
    int a[25][10];
    count = 10;
    add_friend(10, a);

    // ...        

    return 0;
}
rullof
  • 7,124
  • 6
  • 27
  • 36
  • `count` does not need to be global; it just needs to be in scope when it is used. It suffices to declare it as a parameter before the array parameter that uses it. – Eric Postpischil Jan 07 '14 at 01:42
  • @EricPostpischil i just mensioned why he gets the undeclared variable error! – rullof Jan 07 '14 at 01:45
0

Well, here is a way to do what you want with a 1 dimensional array in c/c++:

void ff(int *arr_2d, int s1, int s2)
{
    for(int i=0;i<s1;i++ )
    {
        for(int j=0;j<s2;j++ )
            printf("f[%d][%d] = %d\t",i,j, *(arr_2d+i*s2+j));

        printf("\n");
    }
}

And you can use this function as:

int *f = new int[100];
int s1,s2;
s1 = s2 = 10;
for(int i=0; i<s1; i++)
    for(int j=0; j<s2; j++)
        *(f+i*s2+j) = (i*s2+j);

ff(f,10,10);
asadat
  • 1
0

In C 1999 and in C 2011 implementations that support variable-length arrays, you can declare a function this way:

void add_friend(int count, int friends[][count], int p1, int p2) …
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

you can do it like:

1.void add_friend(int count, int friends[count][count], int p1, int p2)
2.void add_friend(int count, int friends[][count], int p1, int p2)
3.void add_friend(int count, int (*friends)[count], int p1, int p2)
Nibnat
  • 119
  • 7