-4

How can I get the Size of an struct Pointer..

struct person **angest=NULL;

In this struct are 22 persons registrar. How can I get the Count 22 with sizeof? It is possible?

Fkcm95
  • 29
  • 2
  • No. How would `sizeof` know (at compile time) the size of where it's pointing to(at runtime)? Also, "In this struct are 22 persons registrar" is wrong, it's clearly a null pointer. – Blaze Mar 19 '19 at 11:33
  • No. Especially not when it is a pointer to pointer whose value is NULL. – P.W Mar 19 '19 at 11:33
  • The struct became the data from an text file at compile time. This is the decleration. – Fkcm95 Mar 19 '19 at 11:35
  • 1
    @Blaze The `sizeof` is not compile time. It can get the size of a VLA. – klutt Mar 19 '19 at 11:37
  • 1
    @Broman this isn't a VLA though, so it's done at compile time. `sizeof` is only done at runtime for VLAs. See this answer for details: https://stackoverflow.com/a/47336577/10411602 – Blaze Mar 19 '19 at 11:39
  • @Fkcm95 the compiler knows nothing about the file data, don't confuse the compiler with the IDE which is hosting it. – Weather Vane Mar 19 '19 at 11:47

1 Answers1

4

It seems you are allocating the persons using malloc, eg
angest=malloc(sizeof(struct person *)*22); (you have now allocated 22 pointers to the structs)

Then it is not posible to get this number 22 by using sizeof. The size of dynamically allocated arrays cannot be calculated in compile time. You must maintain this size yourself, e.g. static int nPersons;

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41