0
typedef struct stnode {
    unsigned number;
    char * name;
    unsigned section;
    struct stnode * next;
} StudentNode; 

void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students) {

        if(!num_students) return ;
        StudentNode * aux=NULL;

        for(int i=0;i<num_students;i++){

            aux=sections[students[i].section];
            (**(sections+students[i].section)).next=*(students+i);


            }   

    }

When I try do execute this code I have this error:

incompatible types when assigning to type ‘struct stnode *’ from type ‘StudentNode’

What is the problem with the code, I already tried lots of things, but non have worked. I just want to refer the next to the "student" that i'm analyzing

3 Answers 3

2

Read the compiler error (you are not executing this code yet - it hasn't compiled...)

‘struct stnode *’ from type ‘StudentNode’

Basically you are trying to assign a structure to a pointer, this doesn't work. Look at the following line:

(**(sections+students[i].section)).next=*(students+i);

The problem lies in the de-reference of (students + 1).

Sign up to request clarification or add additional context in comments.

4 Comments

Now im receiving the error line 7: 1961 Segmentation fault Why?
We cannot answer this, you need to debug - get the program to generate a core file and then fire up your debugger and look at the core.
Without seeing how this is called, there is no way to know exactly how the fault is occurring.
int main() { StudentNode student = { 11111, "Afonso Henriques", 1, NULL }; StudentNode * sections[3] = { NULL, NULL, NULL }; buildStudentSections(sections, &student, 1); return 0; }
1

The problem is that *(students+i) is de-referencing the element students+i. It should be:

(**(sections+students[i].section)).next=students+i;

1 Comment

Now im receiving the error line 7: 1961 Segmentation fault Why?
0

Change this:

void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students) {

        if(!num_students) return ;
        StudentNode * aux=NULL;

        for(int i=0;i<num_students;i++){

            aux=sections[students[i].section];       // = *(x) you are dereferencing students+1
            (**(sections+students[i].section)).next=*(students+i);


            }   

    }

To this:

void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students) 
{

        if(!num_students) return ;
        StudentNode * aux=NULL;

        for(int i=0;i<num_students;i++)
        {

            aux=sections[students[i].section];      //removed '*`
            (**(sections+students[i].section)).next = (students+1); 


        }   

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.