#Is it possible to pass a specific field of each struct in a struct array as a 2D array parameter?

12 messages · Page 1 of 1 (latest)

tender prawn
#

Hey guys. I am trying to display grade info for students, and I am getting a warning message that I am passing argument 1 of "FindMax" from incompatible pointer type.

In my code, I pass an array of pointers to the test_scores field, which I am hoping is equivalent to a 2D array (5 students with 4 test scores each). I may have the wrong idea though. Please let me know how I can approach this issue. I could try looping the call to the Max func, but I am curious if it is possible to call the Max func only once.

#include<stdlib.h>
#include<string.h>
#define MAX 100

struct StudentInfo{
        char* name;
        int* test_scores;
        int highest;
        float average;
        char grade;
};

void InputData(char** Names, int** Scores);
int* FindMax(int** Scores);
float* FindAve(int** Scores, int* Max);
char* FindGrade(float* Ave);
void Table(char** Names, char** Scores, int* Max);
void Summary(char** Names, float* Ave, char* Grades);



int main(void){
        struct StudentInfo* Class = (struct StudentInfo*)malloc(sizeof(struct StudentInfo*)*MAX);
        for(i=0; i<MAX; i++){
                Class[i]->name = (char*)malloc(sizeof(char)*MAX);
                Class[i]->test_scores = (int*)malloc(sizeof(int)*MAX);
        }

        /* Initialization of Max Score, Ave Score, and Final Grade for each Student */
        int* Max;
        float* Ave;
        char* Grade;

        /* Calling below functions */
        Max = FindMax(Class->test_scores);
vapid waveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tender prawn
#

PS, there is more code, but I only sent what I assume is necessary. Please let me know if you would like me to send the FindMax func. It has a parameter of a 2D integer array.

junior grail
#

Yes they're incompatible FindMax expects int** Scores and you're passing int* test_scores

#

You'd have to make test_scores into int ** and allocate memory for each one of those, 4 test scores as you mentioned

#

Well better yet, you already allocated MAX number of test_scores, so just make FindMax take in int* Scores plus the number of test scores

tender prawn
#

I actually tried that and I got a segfault

#

Anyways I now looped the calling of FindMax in main

junior grail
tender prawn
junior grail
#

Yes the types match now, but it points to the pointers address not the data you want to use