Home > Back-end >  How to initialize a 2d array in C
How to initialize a 2d array in C

Time:06-25

I am trying to figure out a way to initialize my 2d string array and Iwanted to know if there is a better way of doing it than what I have coded below.

Is there also a way to not provide the size of the array as I will be writing to it later on, so I do not know the size (currently set to size 30).

Example of array content at a later stage : "Football","Rugby","Tennis"

char sports_array[30][81];

int i;

for (i=0; i<30; i  ){
  strcpy(sports_array[i],"");
}

The problem with this is that if the array only holds 2 values, then I will have to still loop through the rest (size 30) which I don't want as these will be empty. I need the array to adjust depending on how many values it holds later down the line.

Any guidance would be great.

CodePudding user response:

You don't have to iterate over all indices.

In case of pointer (char* or char**) you can test for the condition NULL, in case of an char array (char[]) you can test for '\0'.

In your case (char sports_array[30][81] = {""}) you could do the following:

for (int i=0; sports_array[i][0] != '\0';   i) { ... }

Hence discarding all empty strings.

If you work with fixed size arrays (char[]), allways initialize with "", {""} or {'\0'}.

If you want to work with malloc and friends (therefore using char pointers - char* or char**), always make sure to initialize the pointers with NULL (either manually or calloc).

If you can guarantee that your pointers (strings) are proper initialized, testing for either NULL or '\0' can (and mostly) should be the condition for any loop.

CodePudding user response:

Since you commented that the strings will not be modified, an array of pointers could be used.

#include <stdio.h>
#include <stdlib.h>

int main ( void) {
    char *sports_array[30] = { NULL}; // initialize 30 pointers to NULL
    int array_count = 0; // count of used pointers.

    sports_array[0] = "Football"; // assign a string literal to a pointer
    printf ( "%s\n", sports_array[0]);
      array_count;

    sports_array[1] = "Rugby";
    printf ( "%s\n", sports_array[1]);
      array_count;

    for ( int each = 0; each < array_count;   each) {
        printf ( "%s\n", sports_array[each]);
    }

    return 0;
}

CodePudding user response:

In the declaration of the array you could write for example

char sports_array[30][81] = { "" };

in this case all characters of the array are zero-initialized.

If you want to reinitialize it such a way that each element of the array would contain an empty string then you can write

for ( size_t i = 0; i < 30; i   )
{
    sports_array[i][0] = '\0';
}

Then you can write for example

strcpy( sports_array[0], "Football" );
strcpy( sports_array[1], "Rugby" );
strcpy( sports_array[2], "Tennis" );

If you want to allocate strings dynamically then you can declare the array like

char * sports_array[30] = { 0 };

In this case all elements of the array will be null pointers.

If you do not know how many strings the array will contain then you can declare a pointer like

char **sports_array = NULL;

and then use the function realloc when a new string is added to the array.

  • Related