-5

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;
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
Fenraehl
  • 13
  • 3
  • 2
    `vpName->Schueler.Vorname=cSchuelerVName;` --> `vpName->Vorname=cSchuelerVName;`, you don't want the name of the type before the member name, and you need to reserve room for this pointer, i.e: `Schueler* vpName = malloc(sizeof *vpName);` – David Ranieri Sep 20 '16 at 07:01
  • Why don't you try a debugger, it's easier and also you can learn from mistakes better. – 23ars Sep 20 '16 at 07:29
  • 1
    @23ars A debugger is pretty useless on code that doesn't compile. – unwind Sep 20 '16 at 07:38
  • @unwind I agree with you, but, I think that even a beginner knows if a code doesn't compile, then something is wrong and should identify the problem or read again. I'm saying because this example of questions are pretty useless and are a lot. – 23ars Sep 20 '16 at 08:21

1 Answers1

2
  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;
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
hashdefine
  • 346
  • 2
  • 5
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Sep 20 '16 at 08:22