1
votes

My function searchlist asks the user enter a student ID and lists that student ID and name. Here is my struct:

struct student {
    int ID;
    char name[40];
    struct student *next;
};
typedef struct student Student;

Here is my function:

void searchlist(Student *SLIST){
    Student *currentstudent = SLIST;
    char str[10], str2[10];

    printf("Enter a student ID: ");
    while(currentstudent != NULL){
        scanf("%d", &str);
        if(strcmp(str, (char)currentstudent->ID) == 0){
            printf("ID#: %d Name: %s", currentstudent->ID, currentstudent->name);
        }
    }
}

However, when I try compiling, it gives me a warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast

2
Please ignore str2[10]. forgot to take that part out. - The_Questioner
What are you trying to compare? Because it looks like you're trying to compare the value of a string (char[]) with a char, not another string ... - AntonH

2 Answers

4
votes

You're not passing the right types of variable to these functions

    scanf("%d", &str);

This is expecting str to be an int but it's a string.

    if(strcmp(str, (char)currentstudent->ID) == 0){

This is expecting two strings (either char * or char[]) but the second parameter is an int and you're casting it to a char.

Since you're reading in an int and wanting to compare it to an int why not write it like this:

int in_id;
scanf("%d",&in_id);
if(in_id == currentstudent->ID) {
4
votes

The strcmp signature looks like this:

int strcmp(const char *s1, const char *s2);

I.e. the second parameter must be of type const char*. But you're giving it a char. Hence the error message you're getting (char is an "integer" type).


Also, scanf("%d", &str); requests scanf to read an integer and store it to str. But str is not an integer type. (This would've been caught by the compiler if you had compilation warnings enabled.)


You need something like this:

printf("Enter a student ID: ");
int givenID;
scanf("%d", &givenID); // read integer input to integer variable

while(currentstudent != NULL) {
    if(currentstudent->ID == givenID) { // check whether this user has the ID entered by the user
        printf("ID#: %d Name: %s", currentstudent->ID, currentstudent->name);
        break; // we found what we were looking for, stop the loop
    }
    currentstudent = currentstudent->next; // move on to the next student in the list
}