3

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

1
  • personPtr=malloc(sizeof(struct person)); and personPtr->name[0]=getchar(); Commented Jan 19, 2017 at 8:17

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I still get an error even if I use scanf : [Error] format '%s' expects argument of type 'char ', but argument 2 has type 'char ()[18]' .. On the other hand if I remove & from the scanf the program runs.. but it gives me an error after it ends
@NeacsuMihai - You are correct. I mistyped in my haste. Now fixed. name is an array, and will decay to the correct pointer type. No & required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.