Shash
Shash

Reputation: 4260

When is the best time to use a Structure or an array

I am a little new to C programming. I was writing a C program which has 3 integers to handle. I had all of them inside an array and suddenly I had a thought of why should I not use a structure.

My question here is when is the best time to use a structure and when to use an array. And is there any memory usage difference between the two in this particular case.

Any help regarding this is appriciated. Thanks!

Upvotes: 3

Views: 2646

Answers (6)

guga
guga

Reputation: 724

Use structures to group information about a single object. Use arrays to group information about multiple objects.

Upvotes: 0

Jay
Jay

Reputation: 24905

In my opinion, you should think first from the perspective of the design to decide which one to use. In your question you have mentioned that "I have three integers to handle". The point here is that how did you arrive at three integers?

Just as many others have noted, let's say you need store details of a person, first you need to think of the person as an object and then decide what all information relevant to that person you will need and then decide what data type you need to use for each of those details. What you are trying to do is that you have decided that data types first and then trying work your way up.

To just put in simple words about the difference between structure and array. Structure is a Composite Data Type (or a User defined data type) whereas array is just a collection of similar data.

Upvotes: 0

Lie Ryan
Lie Ryan

Reputation: 64953

You can think of structure like an object in OOP languages, a structure ties related data into a single type and allows you to access each member of the structure using the member's name instead of array indices. If you can think of a singular name that could unify the related data then you should be using a structure.

An array can be thought of as a list of items, if the name you thought of above contains the word list or collection or is a plural, then you should be using arrays or other collection types. The primary use of arrays is to loop over it and apply the same operation to every items in the array or a range of items in the array. If you used an array but never looped over it, it's an indication that probably array may not be the best data type.

Upvotes: 1

hildensia
hildensia

Reputation: 1740

I would suggest to use an array if the different things you store are logically the same data, but different instance of this. (like a list of telephone numbers or ages). And use a struct when they mean different things (like age and size) bound together because they are related to the same thing (a person).

The size is equal, since both store 3 integers without anything else; You could actually cast the struct to an array and use it like that (although you shouldn't do that for its ugliness).

You could test that with this simple programm:

#include <stdio.h>

struct three_numbers{
  int x;
  int y;
  int z;
};

int main(int argc, char** argv) {
  int test[3];
  printf("struct: %d, array: %d\n", sizeof(three_numbers), sizeof(test));
}

prints on my system:

struct: 12, array: 12

Upvotes: 0

Mike Kwan
Mike Kwan

Reputation: 24477

The difference is about semantic information. If you want to store your information as a list where there is no semantic distinction between different members of that list, then use an array. Perhaps each member of the list represents a different value for the same thing.

If each of those integers represents something special or different, use a struct. Note the implications of using a struct, such as the fact that people expect the members to be closely related semantically.

struct has other advantages over array which can make it more powerful. For example, its ability to encapsulate multiple data types.

If you are passing this information between many functions, a structure is likely more practical (because there is no need to pass the size). It would be bad to pass an array (which decays to a pointer) and expect the callee to know how many items are in the array. Using a struct implicitly makes this part of the function contract.

In terms of size, there is no difference. A 4 byte int would typically be 4-byte aligned.

Upvotes: 2

Alex In Paris
Alex In Paris

Reputation: 1077

An array is best when you want to loop through the values (which, essentially, means they're strongly related). Otherwise a structure allows you to give them meaningful names and avoids the need to document that array, e.g. myVar[1] is the name of the company and myVar[0] is its phone number, etc. as opposed to companyName, companyPhone.

Upvotes: 4

Related Questions