0

I can't find any solution to my problem and my brain is almost exploding...

I have defined a struct and a function to create contacts, like a adress book. I'm asking how many contacts want to be created and this totally works fine.

So I end up with an array of Contacts which pointer i give to my menue. In my menue I can choose to create a contact, search and delete a contact.

Currently I'm working on searching a contact. So I start the search function and give it the pointer on my Struct.

I have no clue how to dereference this pointer into an array of structs again. It's probably just a stupid small little error, but I can't find it.

So I first tried to find out how many Contacts I created. When I try to print the number of created contacts like this:

    void searchContact(struct Contact* ContactArray){
      int howManyContacts=sizeof(ContactArray)/sizeof(struct Contact);
      printf("%d",howManyContacts);
     }

I always get 0 printed out. Doesn't matter if I create 1,2,3....Contacts

I hope someone of you can help me...It's driving me crazy

By the way, I'm using Eclipse with GCC compiler

Erneman
  • 11
  • 1
  • `ContactArray` is a pointer, so its size will always be the same. – juanchopanza Jan 07 '15 at 14:13
  • That `sizeof` trick can only be used with proper arrays (i.e. something declared like `struct Contact contacts[X];`), not with pointers, because `sizeof` of a pointer give you the size of the pointer and not what it points to. Every function that needs to know the size of the array need to have an argument that tells the number of entries in the array. – Some programmer dude Jan 07 '15 at 14:14
  • You can have a `count` field in your structure and use it for this purpose – Gopi Jan 07 '15 at 14:16

4 Answers4

1

Arrays that are passed to a function are treated as pointers, so sizeof(contactArray) will always be the size of a pointer to struct Contact - a constant value. See here for more info.

You should add a parameter to the function called size that will represent the size of the array. It is impossible to otherwise calculate it inside the method.

You can also wrap an array of contacts in another struct, struct Contacts for instance, and include a field specifying the number of contacts.

Community
  • 1
  • 1
Daniel Kleinstein
  • 5,262
  • 1
  • 22
  • 39
0

Your array size is dynamic. sizeof(ContactArray) will return the same as sizeof(void*), because you're dealing with a pointer, not a fixed size array. C doesn't provide a method to determine the size of your array. You need to store the size of the array somewhere and pass it along.

Daerst
  • 954
  • 7
  • 24
0

In your code

int howManyContacts=sizeof(ContactArray)/sizeof(struct Contact);

ContactArray is of type pointer-to-some-type [struct Contact, to be specific]. It will occupy the size of a pointer.

OTOH, struct Contact is a structure. sizeof will give the total size of the individual elements + padding [if any].

In your case, sizeof(ContactArray) [size taken by a pointer variable] is < sizeof(struct Contact) and as sizeof returns size_t [unsigned int], due to the integer division performed in your code, the result you're getting is 0, rightly.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Ok then how can I dereference the pointer that way, that I can use it this way:

printf("%s \n",ContactArray[x].Name);
printf("%s \n",ContactArray[x].Surname);
Erneman
  • 11
  • 1
  • You should include as an edit into your original question. Besides, this code looks good to me... Doesn't it work? – Daerst Jan 07 '15 at 15:28