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 :(
       }
  }
}

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.

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 4 Years Ago by nullptr

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 4 Years Ago by Kashaku

 #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 4 Years Ago by jayarajtk

This article has been dead for over six months. Start a new discussion instead.