Cobalt
Cobalt

Reputation: 33

Passing a 2d-array to a function in C

Note I'm using C not C++. I'm working on a program that will take a 2d-array and count the numbers of non-spaces in a subarray. Such as lines[0][i] <-- iterating over line[0][0] - line[0][n]. However, I'm having difficulty getting the function to accept the 2d-array.

Here is my code:

pScore[0]=letters(pLines, 0);

This is the actual function. pScore[] is another array, but a 1d one. pLines is a 4 by 200 2d-array.

 int letters(char line[][], int row)
 {
     int i = 0;
     int n = n;
     int results = 0;

     for( i = 0; i < (sizeof(line)/sizeof(line[0])); i++ )
     {
         if( !isspace(line[row][i]) )
             results++;
     }

     return results;
 }

When I do this it gives me "formal parameter number 1 is not complete". If I remove the second [] from it, the code runs but gives the worng number of non-space characters.

Upvotes: 1

Views: 118

Answers (4)

David C. Rankin
David C. Rankin

Reputation: 84642

Taking from the comments above, and your function parameter list:

int letters(char line[][], int row)

You violate one primary tenant of passing a 2D array to a function. Specifically, you must always provide the number of columns contained in the array. e.g.

int letters(char line[][5], int row)

char line[][5] is an appropriate parameter. However, whenever you pass an array as a function parameter, the first level of indirection is converted to a pointer. (you will often here this referred to as "pointer-decay", though that is a bit of a misnomer). Therefore a proper declaration that makes this clear is:

int letters(char (*line)[5], int row)

line, after conversion, is a pointer-to-an-array of 5-int. While you can pass the array as line[][5], that is not nearly as informative as (*line)[5]. Let me know if you have any questions.


Numbers Instead of Characters

It is hard to tell what is going on without seeing the remainder of your code. However, I suspect that you are confusing the numerical value and the ASCII character value for the contents of your array. (e.g. character '0' = decimal 48 (0x30 (hex), '1' = 49, 'a' = 97, etc..). See ASCIItable.com

Upvotes: 3

Awad A. Bekhet
Awad A. Bekhet

Reputation: 113

I am not sure if the following statement works with you

for( i = 0; i < (sizeof(line)/sizeof(line[0])); i++ )

where sizeof(line) will be 4 or something like that depends on your platform because "line" is a pointer and you get the size of pointer itself. Correct me if I am wrong. In this case, you should pass column number as row's.

Upvotes: 1

YtRen
YtRen

Reputation: 13

You need tell function the number of columns of 2d array. Here may help you.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409442

You you pass an array to a function it decays to a pointer. That means char line[][] should really by char (*line)[SIZE_OF_SECOND_ARRAY].

It also means the sizeof trick will not work, as doing sizeof on the pointer line will just return the size of the pointer and not what it points to, you need to explicitly pass the size as an argument to the function.

Upvotes: 2

Related Questions