How to access position of an array of struct using pointer

Oliver

Answer (see below): When accessing a struct with a pointer, one needs to use the arrow operator "myStruct->structVariable" which is the equivalent to "(*myStruct).structVariable". When accessing the struct directly one uses "myStruct.structVariable".

I'm pretty new to C and pointers and am trying to do the following: I'd like to access an array of structs which is part of another struct. The struct is a pointer which gets passed to a function, in which i would like to access it.

Student has an array of structs which 10 Lectures (also a struct). To simplify the process of adding lectures to a student, i would like to be able to call the function "addLectureToStudent()" and passing the necessary arguments and assign the values to the array of lectures.

typedef struct Lectures {
  char name[20];
 
} Lecture;


typedef struct Students {
  char name[20];
  Lecture lectures[10];

} Student;     

void addLectureToStudent(Student * stud, int position, char lecture_name[20]){
    strcpy(stud->lectures[position]->name, lecture_name); //This line doesn't work as expected
}
int main(void) {

    Student markus;
    strcpy(markus.name, "Markus");
    markus.matrikelnummer = 12089548;
    addLectureToStudent(&markus, 0, "Programming");
}

My problem is, that when wanting to access strcpy(stud->lectures[postition]->name, lecture_name) it tells me that stud should be a pointer but isn't (thats the best translation i can come up with. It is originally in german..). How do i access the array of lectures properly in this case?

MikeCAT

stud->lectures[position] is Lecture, so you should use ., not ->, to access that.

strcpy(stud->lectures[position].name, lecture_name);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to access a pointer of struct in a struct using a function?

How to access a struct array through a pointer?

Access a Array by Struct (with a pointer)

How to access a field in a struct contained in an array of structs using a pointer to that array? [C++]

How to initialize an array of pointer inside a struct by using struct constructor?

Define pointer to array of int pointers in struct, how to access those ints?

How to access nested structure using pointer and array?

Accessing an array using a pointer in a struct

C how to pass pointer to struct array pointer

void pointer to struct, element access using macros

How to set a value in a multi-dimensional struct array using a struct pointer?

How to access a pointer struct's member variables?

How to access struct members from pointer

Struct's pointer and how to access elements

How to pass/access a double pointer to/from a struct?

print array item of struct using pointer in c

How to access 2 dimensional Array Pointer in a typedef-struct over a function in c

How to access the members of a struct within a struct via a double pointer?

How do I access the members of a pointer to pointer to struct?

How to access a member of a struct which is passed into a function as pointer to pointer parameter?

How to access the content of a void pointer inside a struct pointer?

How to access the array position using SwiftUI's onDelete() function

How to set value array of struct with pointer in Go

How to fill array of struct containing pointer arrays

How to correctly typecast char array to struct pointer?

How to return pointer to array of struct in C

How to match a varible in a struct array, then the position

How to get struct array (or pointer) that is part of another struct? in C

How to free a tree struct using double pointer?