Neacsu Mihai
Neacsu Mihai

Reputation: 137

Using malloc with struct in C

So, this is my code:

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

struct person{
    char name[18];
   int age;
   float weight;
};

int main()
{
    struct person *personPtr=NULL, person1, person2;
    personPtr=(struct person*)malloc(sizeof*(struct person));
    assert (personPtr!=NULL);
    personPtr = &person1;                   // Referencing pointer to memory address of person1

    strcpy (person2.name, "Antony");                //chose a name for the second person
    person2.age=22;                                 //The age of the second person
    person2.weight=21.5;                                //The weight of the second person


    printf("Enter a name: ");               
    personPtr.name=getchar();                   //Here we chose a name for the first person

    printf("Enter integer: ");              
    scanf("%d",&(*personPtr).age);          //Here we chose the age of the first person

    printf("Enter number: ");               
    scanf("%f",&(*personPtr).weight);       //Here we chose the weithgt of the first person

    printf("Displaying: ");                                             //Display the list of persons
    printf("\n %s, %d , %.2f ", (*personPtr).name, (*personPtr).age,(*personPtr).weight);           //first person displayed
    printf("\n %s, %d , %.2f",person2.name, person2.age, person2.weight);                       //second person displayed
    free(personPtr);
    return 0;
}

I get two errors and I don't know why. Firstly, I don't think that I allocated the memory right, the first error is on the next line:

personPtr=(struct person*)malloc(sizeof*(struct person));

It says that:

[Error] expected expression before ')' token

The second error that I get is on the line

personPtr.name=getchar();

Why cant I assign a name using getchar for a structure? The error is:

[Error] request for member 'name' in something not a structure or union

Upvotes: 3

Views: 1679

Answers (1)

sizeof*(struct person) is a syntax error. It is seen by the compiler as an attempt to apply the sizeof operator to *(struct person). Since you can't dereference a type, the compiler complains. I think you meant to write the following:

personPtr = malloc(sizeof *personPtr);

It's the idiomatic way to allocate whatever personPtr is pointing to. Now the type is specified only where the pointer is defined, and that's a good thing. You also don't need to cast the result of malloc, since void* is implicitly convertible to any pointer type.

The second error is two-fold:

  1. name is a fixed sized array. You cannot assign to an array using the assignment operator. You can only assign to each individual element.

  2. getchar returns a single character, not a string as you seem to expect. To read a string, you can use scanf("%17s", personPtr->name). The 17 is the size of your buffer - 1, to protect against buffer overflow when scanf adds a NUL terminator to the string.

Upvotes: 7

Related Questions