Question

I am attempting to edit an array of structs.the user has the ability to enter 3 numbers each of which performes a different action. for example you can add by typing:

1
1234 marvin

all the code for this is below

    void input_interpreter()
{   
    int input;
    char inputc1[106];
    scanf("%d", &input);

    switch(input)
    {
        case 0 :
            /*pretty self explanatory*/
            exit(0); 
            break;
        case 1 :
            /*add a student to the array*/
            scanf("%s", inputc1);
            new_student((string_split_string(inputc1),(string_split_int(inputc1)));/*<----the warning points here*/
            break;
        case 2 :
            /*Remove a specified student, (implicitly unenrolling them from all their units, if any), O(S + U).*/
            remove();
            break;
        case 3 :
            /*Print, in ascending numerical order of ID number, the ID numbers and names of the students in
            the database, O(S).*/
            print_array();
            break;
    }
     input_interpreter();
     return;
}

'

here is what I use to seperate out the id's and names

    int string_split_int(char input_string[])
{
    char * ptr;
    int ID = 1;
    int  ch = " ";
    int i;
    int name_start;
    int array_length = sizeof(input_string);
    ptr =  strchr(input_string, ch);
    name_start = array_length - sizeof(ptr); /*may have to change this if names                  are including namespaces*/

    for(i = name_start; i >= array_length; i--)
    {
            ID=ID/10;
            ID=ID+input_string[i];
    }
    return ID;
}

char string_split_string(char input_string[])
{
    char * ptr;
    char name[100];
    int  ch = ' ';
    int i;
    int name_start;
    int array_length = sizeof(input_string);

    ptr =  strchr(input_string, ch);
    name_start = array_length - sizeof(ptr); /*may have to change this if names are including namespaces*/
    for(i = name_start; i <= array_length; i++)
    {
        name[i] = input_string[i];
    }
    return *name;
}


    void new_student(char *name, int ID)
{
    struct student s;       
    s.ID=ID;
    s.name=name;
    insert_array(s);
    return;
}

unfortunatly this throws a passing argument 1 of 'new_student' makes pointer from integer without a cast warning.

Was it helpful?

Solution

Problem Analysis

Per the signature

void new_student(char *name, int ID)

the 1st argument must be char *.

However according to the call

new_student((string_split_string(inputc1),(string_split_int(inputc1)));
/*<----the warning points here*/

and the per signature

char string_split_string(char input_string[]);

the type returned by string_split_string() and thus the 1st argument to new_student() is char.

In short, the caller is providing char where callee requires char *.

Solution Sketch

Splitting a string is a fairly common task, please take some to (re)search on that, before rolling out a complex solution.

C

C++

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top