1

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.

tijko
  • 7,599
  • 11
  • 44
  • 64
Cobalt
  • 33
  • 4
  • Why are you trying to iterate by number of rows instead of number of columns? – MikeCAT Mar 05 '16 at 08:26
  • You need to show the declaration of `lines` before anyone can do anything but guess. If you have declared `lines` as say `char lines[maxr][maxc] = {{0}};`, then you probably need to function parameter passing lines to look like `int letters (char (*line)[], int row)`. – David C. Rankin Mar 05 '16 at 08:51
  • @MikeCAT I'm trying to count the number of non-space characters in that subarray. Like: pLines[0][0-199] <-- I want to see how many of those are a non-space character. – Cobalt Mar 05 '16 at 23:11

4 Answers4

3

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

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Thank you, that makes a lot of sense. It is now taking my array, I think, but for some reason it is saying the contents of the array is a bunch of numbers rather than the user specified characters. (Previous printf()s show the array is characters.) – Cobalt Mar 05 '16 at 23:09
  • See the addition to the answer. I suspect that you are confusing the *numerical value* and the *ASCII character value*. – David C. Rankin Mar 06 '16 at 08:33
2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

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

Community
  • 1
  • 1
YtRen
  • 13
  • 3
1

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.

  • Your assumption on `line` being a pointer for which in turn the `sizeof`-operator return the size of a pointer is correct. – alk Mar 06 '16 at 12:13