Fenraehl Fenraehl - 2 months ago 7
C Question

filling structures with a pointer C

We started programming in School and need to fill a structure via a pointer but we get this error:


'Schueler' has no member named 'Schueler'


We are using eclipse Indigo with the MinGW Compiler.

#include <stdio.h>

int main()
{
typedef struct Schueler{
char Vorname[10];
char Nachname[10];
}Schueler;

Schueler* vpName;
char cSchuelerVName[10]="Hans";

vpName->Schueler.Vorname=cSchuelerVName;

return 0;
}

Answer
  1. Firstly you need to allocate memory to the structure pointer vpName
  2. The way the structure element Vorname is accessed is wrong. It should be pointername->Structure_element_name
  3. Vorname is a character array. = cannot be used to assign the value

modified code:

#include <stdio.h>

int main()
{
    typedef struct Schueler{
        char Vorname[10];
        char Nachname[10];
    }Schueler;

    Schueler* vpName=(Schueler*)malloc(sizeof(Schueler));
    char cSchuelerVName[10]="Hans";
   // vpName->Schueler.Vorname=cSchuelerVName;
    strcpy(vpName->Vorname,cSchuelerVName);
    printf("vpName->Vorname=%s\n",vpName->Vorname);

    return 0;
}