0

Suppose I have a function, int function(int N, int c[N]){...}, taking as parameters an integer and an array. Suppose now I have a double array **c of size 'N times 2' and suppose, I want to apply the function function to one column of the double arrow, c[i][0], i varying from to N-1. How am I supposed to use this function. Does it looks like something like function(N,*c[0]) ?

Does anyone can help ?

5
  • When you write "double array" I think you mean "two dimensional array". A double array is something entirely different. Commented Aug 25, 2012 at 16:10
  • Yes sorry, I tought it was the right term. I meant two dimensional array Commented Aug 25, 2012 at 16:12
  • You should definitively read up on pointers and arrays in C. A two dimensional array and a pointer to pointer (your **c) completely different beasts and not compatible. Commented Aug 25, 2012 at 16:19
  • possible duplicate of Passing multidimensional arrays as function arguments in C Commented Sep 22, 2012 at 5:31
  • This is far too simple not to look up in a tutorial. And it also has been asked many times here. Voting to close. Commented Sep 22, 2012 at 5:32

2 Answers 2

2

An array in C always decays to a pointer to its first element when passed into a function. This is good to know in some situations.

As an example, you could write

int list[10];
func (int *x) {
    int i;
    for (i = 0; i < 10; i++) {
         printf("%d", x[i]);
    }
 }

x[i] is really just syntactic sugar. In C, when you use bracket notation to access an element of an array, it gets converted to *(x + i), where x is the name of the array.

This works because of pointer arithmetic. If x is the name for an array of 10 integers, then the value of x in an expression is the address of the first integer of the array.

x + i will always point to the i-th element after x (C takes into account the size of the element type stored in the array, and increments the pointer accordingly).

Thus, when passing 2d arrays, the array decays to a pointer to its first element - which is an array.

A function signature taking a 2d array can be written as

func(int x[][columns] {
      ...
} 
// but could also be written as

func(int (*x)[columns]) {
    ...
}

which indicates that x is a pointer to an array of integers.

Sometimes you need to write a function to accept a 2 dimensional array where the width is not known until run time. In this case, you can pass a pointer to the [0][0] element and also pass the two dimensions, and then use pointer arithmetic to get the right element. For example,

print_2d_array (int *x, height, width) {
    int i, j;
       for (i = 0; i < height; i++) {
         for (j = 0; j < width; j++) {
            printf("%d", x[i * width + j]);
         }
     }
}

int list[10][10];
print_2d_array (&list[0][0], 10, 10);

would work for a dynamically allocated 2d array.

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

Comments

2

In c langauge, for single dimentional array you no need to mention the size of the array in the function arguments while passing an array to that function.

If it is a single dimentional array

...
int a[10];
func(10, a);
...
void func(int size, int x[]) //no need to mention like int x[10]
{
    //here x is not an array. Its equivalent to int *
    printf("%d", sizeof(x)); // this will print 4 or 8 not 40
}

If it is 2D array

...
int a[10][5];
func (10, a);
...

void (int rows, int x[][5])   //here int x[][] is invalid
{

}

3 Comments

You are correct that you don't have to mention the N in the first dimension, but it can be considered being good practice to show the intention of the code. And also for fixed sizes there is the notation int x[static 10] that requires that the array that is passed through the pointer has at least 10 elements.
OK, in fact whta I did is not working. if I take the example below abd suppose I call the function 'function' : void function(int rows, int x[][5]), suppose now I call this function later on my code, in I put function(rows,x), where x is the 2D arry you mentionned, it doesn't work....
@user1611830 : 2D array is working for me. codepad.org/uNij4dkf Check this link i have executed my program.

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.