简体   繁体   中英

How to pass a (struct) pointer of an array of pointers to a function?

I have a struct of pointers and a global pointer to use in functions declared after main. Now declaring the functions with those same name of pointers is fine. But when I call it within another function (because its like a menu type program), I kept getting different types of errors.. Like expression is needed, unexpected type, etc. My question is simply how to I call the parameters for the function to work. I haven't used C in years so the solution might seem simpler than it sounds like. The code below will show you what I mean.

StudentPtr studentArray StudentPtr** studentArray struct StudentPtr *studentArray *StudentPtr studentArray[] (Pretty much moving the pointers around and using struct as prefix)

typedef struct Student {
    char *firstName;
    char *lastName;
    char *id;
    char *email;
} Student, *StudentPtr;

//Prototypes:
int fillData(StudentPtr studentArray,char* f, char* l, char* id, char* e,int n);
int displayData(StudentPtr studentArray, int n);
int displayDataAll(StudentPtr studentArray);

int main()
{
return 0;
}

int command(char line[])
{
//other code here
//some more code..
//......

//error below
if(lineSize==0)    /* If the command is empty, asks again for a command */
    {
        return 0;
    }

    else
    {
        if(strncmp(line,"1",lineSize)==0)
        {reset();}

        else if(strncmp(line,"2",lineSize)==0)
        {fillData(StudentPtr studentArray,char* f, char* l, char* id, char* e,int n);} //the first parameter here

        else if (strncmp(line,"3",lineSize)==0)
        {modify(StudentPtr studentArray,char* f, char* l, char* id, char* e,int n);} //here as well

        else if(strncmp(line,"4",lineSize)==0)
        {displayDataAll(StudentPtr studentArray);} //here too


        else if(strncmp(line,"5",lineSize)==0)
        {return 1;}
        else
        {noComm();}
    }

    return 0;
}

//example of the functions supposed to be used
int fillData(StudentPtr studentArray,char* f, char* l, char* id, char* e,int n)
{
    //get the start of the nth record
    //Ptr arithmetic
    StudentPtr currentStudentptr = studentArray+(n-1);

    //allocate memory for the character pointers
    currentStudentptr->firstName =malloc(sizeof(char)*20);
    strcpy(currentStudentptr->firstName,f);

    //... same for others

    return 0;

}

The calling of the function here should properly call the functions that are further down.

You are mixing syntax for function declaration and definition with syntax for calling a function:

    {fillData(StudentPtr studentArray,char* f, char* l, char* id, char* e,int n);} //the first parameter here

In a function call you mustn't specify the type. You only provide the arguments:

    {fillData(studentArray, f, l, id, e, n);}

You do not show any variable definiton. Therefore I cannot tell if the variables have correct types or if you need to add some & operators here and there... That is the reason why a minimum complete verifyable example is mandatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM