Reputation: 911
I implemented a linked list with a contains function. It uses strcmp like so:
void contains(NODE *head, char data)
{
NODE *this = head;
while(this != NULL && strcmp(this->data, data) != 0)
{
if(strcmp(this->data, data) == 0){
printf("Found data %s\n", this->data);
}
this = this->next;
}
}
in main I have (I use contains on the last line):
NODE *head;
head = malloc(sizeof(NODE));
bool headNode = true;
char userID[1000];
char test[180] = "monkey"; // for testing contains function
while((fgets(userID,1000,stdin) != NULL)){
if(headNode == true)
{
head = insert(NULL, userID);
headNode = false;
}
else
{
head = insert(head, userID);
}
}
contains(head, test);
I am still learning C, pointers are still a bit confusing. I have a feeling im making a basic mistake. I want to let the user input some string on their own and check if the list contains that string, but I cant even get contains to work with this test string. I know what I am comparing the test string to is indeed in the list, I have a printList function which I used to verify.
Upvotes: 0
Views: 461
Reputation: 12658
test
is an pointer to an array which you are passing in function contains(head, test);
, So formal argument in the caller function should be a pointer as, contains(NODE *head, char * data) { ...
.
Upvotes: 1
Reputation: 1734
Pass in the pointer "test" into function "contains()":
It should be:
void contains(NODE *head, char * data)
Upvotes: 1