I am quite new to C programming but have some experience with other languages.
I am currently learning about structs and pointers. My assignment is to sort a few names using structs and pointers.
I started by making a struct and a function for swapping 2 names around. I just can't figure out what goes wrong when I want to declare an array with a struct.
This is the error I get when I compile:
pr.c:29:22: error: expected expression
studenten[i]={s[i][0],s[i][1],s[i][2]};
^
1 error generated.
Here is some code:
#include <stdio.h>
#define MAXSTUDENT 2
typedef struct {
char *firstname;
char *pre;
char *lastname;
} student;
void swap(student **a,student **b) {
student *temp;
temp=*a;
*a=*b;
*b=temp;
}
int main () {
int i;
char *s[MAXSTUDENT][3]={{"John"," the ","Way"},{"John"," ","Smith"}};
student *studenten[MAXSTUDENT];
for (i=0;i<MAXSTUDENT;i++) {
studenten[i]={s[i][0],s[i][1],s[i][2]};
}
printf("%s%s%s - %s%s%s\n",studenten[0]->firstname,studenten[0]->pre,studenten[0]->lastname,studenten[1]->firstname,studenten[1]->pre,studenten[1]->lastname);
swap(&studenten[0],&studenten[1]);
printf("%s%s%s - %s%s%s\n",studenten[0]->firstname,studenten[0]->pre,studenten[0]->lastname,studenten[1]->firstname,studenten[1]->pre,studenten[1]->lastname);
}