Pointer problem of C language

requirements:
C language implements producer-consumer model

  • student_queue queue
  • producer thread
  • consumer thread

question:
when getting data from the queue, student is not assigned after the student_queue_get () function is executed

  • student is assigned correctly in the student_queue_get () function
  • status when student is reassigned after student_queue_get () is executed ((Student student declaration)

look at the code:

  • student_queue.c queue
void *thread_run(void *userdata){
    for(;;){
        Student student;
        student_packet_get(&queue, &student);
        
        LOGI("Student: { age: %d, name: %s}\n", student.age, student.name);// : age = 0, name = 
    }
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_run, NULL);
}
C cPP
May.02,2021

this is because the modification to the parameter student in student_queue_get () is not passed out.

void *thread_run(void *userdata){
    for(;;){
        Student student;//A
        student_packet_get(&queue, &student);//A
        //...
    }
}

int student_queue_get(StudentQueue *queue, Student *student){//studentA
    //student = queue->front; studentqueue->frontA
    *student = *(queue->front);//A
    //...
}

is essentially a problem with pass by value


the student pointer is defined outside the function, and the address of the student pointer is passed in the function

Menu