1

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);
}
Air
  • 8,274
  • 2
  • 53
  • 88

1 Answers1

2

This doesn't work the way you expect:

studenten[i] = {s[i][0],s[i][1],s[i][2]};

Member initializer lists are only valid in a declaration, regular assignments are not allowed to use initializer lists. You will have to do it manually, and allocate memory yourself:

for (i=0;i<MAXSTUDENT;i++) {
    studenten[i] = malloc(sizeof(*studenten[i]));
    if (studenten[i] == NULL) {
        /* Handle malloc error... */
    }
    studenten[i]->firstname = s[i][0];
    studenten[i]->pre = s[i][1];
    studenten[i]->lastname = s[i][2];
}

In either case, note that s[i][j] is 0 for any i greater than or equal to 2 - you only initialized s[0] and s[1].

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • I no longer get a compile err but I get a bus error(10) at runtime –  Jan 12 '14 at 21:06
  • Given that `swap` is accepting the `student**` type, passing the addresses of the studenten pointers should still work right? – A Person Jan 12 '14 at 21:10
  • it seems I do something wrong with my pointers, if I define the character array as * it means is a pointer right? this means if I later use it in the for loop I pass the pointers of s[i][x] to the student struct and not the actual chars, right?? –  Jan 12 '14 at 21:10
  • it works with "studenten[i] = malloc(sizeof(*studenten[i]));", you have my thanks! But I would like to know why, do I always have to allocate memory like that with a struct? –  Jan 12 '14 at 21:18
  • You made `studenten` an array of pointers. That's why you have to do the malloc yourself. Declare studenten as an array of student structs and then pass the addresses of its elements to avoid manual malloc. – A Person Jan 12 '14 at 21:20
  • @user3188237 Not necessarily. You have to allocate memory every time you declared a pointer and want to make it point to a valid location where you can write. The declaration `student *studenten[MAXSTUDENT];` will only give you space to hold `MAXSTUDENT` pointers, but the locations pointed to by each pointer are not ready to write. You must allocate each one separately. If you declare an array of `studenten` then you don't have to allocate memory for each position. – Filipe Gonçalves Jan 12 '14 at 21:21