2

I have read somewhere that if you create user defined data type like structure, the compiler will "give" the address of that structure (It the first element) to the name of the structure. struct person p1, p1 will also hold it's address.

If this is true, why did this code not work ?

main(void) {
  typedef struct personne{
    char nom [20] ;
    char prenom [15] ;
    int age ;
    char tel[12] ;
  } personne ;
  personne p;

  p->age=10;
  printf("%d",p->age);
}

ERROR MESSAGE: base operand of '->' has non-pointer type 'personne {aka main()::personne}'

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Marwane
  • 333
  • 3
  • 13
  • 1
    `p->age=10;` won't compile because `p` is not a pointer. It should be `p.age = 10`. If you want a pointer: `personne *p2 = &p;`. – 001 Jun 05 '21 at 14:57
  • @JohnnyMopp I know, that not what I asked – Marwane Jun 05 '21 at 15:00
  • 2
    It's unclear what you are asking. – 001 Jun 05 '21 at 15:01
  • ", the compiler will "give" the adresse of that structure (It the first element) to the name of the structure" --> Not true. – chux - Reinstate Monica Jun 05 '21 at 15:02
  • Are you asking this: [Is a struct's address the same as its first member's address?](https://stackoverflow.com/a/9254621) ? – 001 Jun 05 '21 at 15:04
  • @chux-ReinstateMonica Ok thanks, i don't know why my teacher claimed that it is – Marwane Jun 05 '21 at 15:05
  • @JohnnyMopp yes ! – Marwane Jun 05 '21 at 15:06
  • @JohnnyMopp In that page they said it is, am a bit confused, thanks for the help i just need to search more – Marwane Jun 05 '21 at 15:10
  • 2
    The answers in that post are correct. But your code is not because you are trying to use `p` as a pointer - and it isn't. I think you want `personne p; personne *pp = (personne *)(p.nom);` Note that I did not use the address of `p.nom` since it is an array. If it were an `int`, for example, you would need the address: `personne *pp = (personne *)(&p.nom);` – 001 Jun 05 '21 at 15:13
  • Marwane, "why my teacher claimed that it is " is likely that "it" refers to a pointer to a `struct` and not the `struct`. `p` here is a `struct`, not a pointer. – chux - Reinstate Monica Jun 05 '21 at 17:08

1 Answers1

1

In general, all variables in your program occupy some memory space. The space is allocated by the compiler at a known address of the virtual memory.

So, personne p; asks compiler to allocate space in memory for variable p sufficient to keep the data for all fields of the struct. The compiler knows the address of the variable p. You can always ask the program to give you that address as &p. The address is essensially the pointer to the struct.

The variables in 'c' can contain data (as your p) or a a pointer. A pointer variable keeps an address which is associated with another variable. A pointer varialbe also occupies space in memory and you can get its address as well.

But in your case you can use something like the following:

  personne pstruct;        // delcare your struct
  personne *p = &pstruct;  // delare a pointer to the struct and intialize it with the address of 'pstruct'

  p->age=10;
  printf("%d",p->age);

This should work now.

Serge
  • 11,616
  • 3
  • 18
  • 28