0

I can´t read from the char array
This is how I pass the string into my array for each test case and that works fine but passing the array is a problem. I looked it up here:
Passing arrays and matrices to functions as pointers and pointers to pointers in C
I still get the warning that I compare between a pointer and an integer.

  char klammern[MAX][STRING];
    int i, test;
    int ergebnis;

    printf(" Test cases?:");
    scanf("%d",&test);
    getchar();                      //catch Enter


    for(i=0;i<test;i++)
    {
        fgets(klammern[i],30,stdin);
    }

Here is how I pass the argument:

    for(i=0;i<test;i++)
    {
      ergebnis = matching_brackets( klammern );
      printf("%d ",ergebnis);
    }

My function should count the numbers of brackets and return 1 if not all brackets are closed and 0 if everything is correct.

int matching_brackets(char (*klammern)[STRING])
{
    int ergebnis, i;
    int runde_klammern = 0;

    for(i=0; *klammern[i] != '\n';i++)
       {
           if( *klammern[i] == '(')
            {
            runde_klammern++;
            }

            else if( *klammern[i] == ')')
            {
            runde_klammern--;
            }
   ergebnis = runde_klammern;

   if ( ergebnis != 0)
   {
       return 1;
   }
   else
   {
       return 0 ;
   }

While testing I saw that my for loop in the function read my array like this:
array [1][0]
array [2][0]
... I want to loop the array like:
array[0][0]
array[0][1]
...

Edit: I do not get the compiler warning anymore after I fixed a typo in my function.

Community
  • 1
  • 1
7Orion7
  • 57
  • 1
  • 7

1 Answers1

2

You have two problems, one is that your loop is wrong and the other is an operator precedence problem.

You should loop like e.g.

for (size_t i = 0; i < test; ++i)
{
    for (size_t j = 0; klamern[i][j] != '\n'; ++j)
    {
        // Here `klamern[i][j]` is the current character
    }
}

Note that you need to pass the test variable to the function as well.

The above loops also removes the second problem. (that *klamern[i] is seen by the compiler as *(klamern[i]) and not (*klamern)[i]).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Your style is exactly like mine! Hard to find someone that does *exactly* what I do! (using `for` like a keyword, e.g., `for (expr)` vs `for(expr)`, putting opening brace on a new line (as opposed to K&R-style which only does this for functions; old arguments *for* this style are due to limited terminal size, but I think it's just much *much* easier to see when it's on it's own line, and looks very nice and neat since they line up), and using prefix operator unless postfix is explicitly needed (because of the wasted return value) (i.e., `++j` vs `j++` in a for loop). We should be friends! :) – RastaJedi Feb 22 '16 at 05:26
  • Oh, and always spaces before *and* after operators (i.e., `arr[i - 5 * 3]` vs. `arr[i - 5*3]` even when the latter would hint at the operator precedence); it's just *so* much easier to see this way (I adopted my style from Linux kernel style and added some of my own--everything I do is for a very specific reason (Google style has some good ideas but not all are amazing). Also, to make sure you do `sizeof var` vs. `sizeof (var)` (or even `sizeof(var)`), and `return expr;` and not something silly like `return (0)` or return(0)`. – RastaJedi Feb 22 '16 at 05:29