0

never help with pointer and functions. i want to creat a function what check if "char *test" is inside "char array[1]"

int main()
{
    char array[1] = {"one", "two"};
    char *test = "one";

    check(test, array);
}



void check(char test, char array[])
{
    int i;
  for(i = 0; i <= 1; i++)
  {
      if(strcmp(array[i], test) == 0)
      {
          //It never comes in this if statment :(
      }
      else
       {
           //It never comes in this else statment :(
       }
  }
}
4
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by jayarajtk
0

ops made a mistake.

it should be

char *array[2] ={"one", "two"};

and prototype is

void test(char*[], char*);                //not sure if this is right.
0

try this:

int main()
{
    char* array[4] = {
        "one", 
        "two",
        "four",
        "nine"
    };

    char* test = "four";

    check(test, array);
    getchar();

    return 0;
}



void check(char* test, char* array[])
{
    int i;

    for (i = 0; i < 4; i++)
    {
        if(strcmp(array[i], test) == 0)
        {
            printf("same string: %s\n", array[i]);
        }
        else
        {
            // whatever
        }
    }
}

Edited by nullptr

0

Where does void test(char*[], char*); come from? I assume you meant void check(...).

The reason why it doesn't reach your function, is because your main() function is unaware of the fact that the check function exists. This can be fixed by either placing the check(...) function above the main() function or putting void check(...) (with the same parameters as your check(...) of course) as one of your first lines in your code, after the include, mixing a sort of header + code file in one.

Hence if you ever use a call to a function, then the program must already have read the function or a reference to that function. I assume it is basic knowledge that it read from the top to the bottom.

Edited by Kashaku

0
 #include <stdio.h>
 #include <string.h>
 int main()
 {
     void check(char*, char *[]);
     char *array[] = {"one", "two"};
     char *test = "one";
     check(test, array);
 }
 void check(char* test, char* array[])
 {
     int i;
     for(i = 0; i <= 1; i++)
     {
         if(strcmp(array[i], test) == 0)
         {
                //Do whatever
         }
         else
         {
                //Do Whatever
         }
     }
 }

Edited by jayarajtk

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.