1

I'm fairly new to C and I'm trying to wrap my head around the initialization of a multidimensional character array, for my assignment I'm asked to read from a file and store the text in an array,

I have to read input for 5 fictitious people and store some information about them, so I realized that my array will look something like:

char input[5][];

What I am confused about is the second parameter, I'm not sure what to set it too. There are 9 fields of information which I will be storing about these people, and I can't seem to find an answer to if I should set this second number to the amount of fields, or how large it should be. ie,

char input[5][9];

or

char input[5][256];

Also, if it's the latter, is there a practice of how larger I should set it to, or just pick a number? Thanks!

2 Answers2

4

I suggest you take the following approach: instead of making an array of char to store the information about these persons, you should make a struct person, which would have some info variable with predefined lenght

struct person {
    char name[50];
    char address[50];
    char phone_number[15];
};

or, if you want, you can make these char pointers (such like char *name, char *address), and then you can malloc() the desired amount of memory to each variable as you want (also, don't forget to free the memory after you use it).

Then, in your main, you could make an array of persons, such as struct person[5];, and manipulate them as you want.

edit: Also note that, as Pankrates commented, when you define a size for your array you must make sure that the input will not exceed the lenght of the array, otherwise you'll be writing stuff on memory that does not belong to you, which causes undefined behavior.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • 1
    Please note that with these approaches, you should always make sure that user input will fit in the allocated memory, i.e. in the first case entering a name in `person.name` that exceeds 49 characters will cause undefined behaviour – Pankrates Oct 01 '13 at 01:48
  • @Pankrates very well put, thanks. I forgot to add it to my answer. – Natan Streppel Oct 01 '13 at 01:53
1

To figure this you should try to learn about the input. If you are absolutely sure there will be only 9 fields(of single char data) for 5 people, then you should set it to char input[5][9].

If you think there could be more fields of information than 9 in the future, then you could go for a higher value like char input[5][256]. However this would waste a lot of space if the data for fields are sparse. This is static assignment of memory

So the third option when you are unsure about the number of fields for each person, is to assign memory dynamically like--

char* input[5];
input[0] = malloc(sizeof(char) * 100); // space of 100 chars

Here in the last case, the memory is given at runtime instead of compile time. You can also get the size from user input and use it for assign memory for fields.

char* input[5];
int size = 0;
scanf("%d",&size);
input[0] = malloc(sizeof(char) * size);

Further more, if you need to store fields, where each field takes more than one character then you should create a structure.

struct person
{  char name[30];   // memory could be assigned dynamically or statically
   char field1[30];
   char field2[30];
   ...
};
hrv
  • 925
  • 6
  • 13