Create a string array with Characters in C

19,326

Solution 1

char *paths[10];
paths[0][0]='1';

paths is an array of pointers. paths is not initialized to anything. So, it has garbage values. You need to use allocate memory using malloc. You just got unlucky that this program actually worked silently. Think of what address location would paths[0][0] would yield to assign 1 to it.

On the other hand, this worked because -

char *paths[10];
paths[0] = "123456";

"123456" is a string literal residing in the reading only location. So, it returns the starting address of that location which paths[0] is holding. To declare correctly -

const char* paths[10];

Solution 2

You have a lot of options provided by various answers, I'm just adding a few points. You can create a string array as follows:
I. You can create an array of read only strings as follows:

char *string_array0[] = {"Hello", "World", "!" };

This will create array of 3 read-only strings. You cannot modify the characters of the string in this case i.e. string_array0[0][0]='R'; is illegal.
II. You can declare array of pointers & use them as you need.

char *string_array1[2]; /* Array of pointers */
string_array1[0] = "Hey there"; /* This creates a read-only string */
/* string_array1[0][0] = 'R';*/ /* This is illegal, don't do this */
string_array1[1] = malloc(3); /* Allocate memory for 2 character string + 1 NULL char*/
if(NULL == string_array1[1])
{
/* Handle memory allocation failure*/
}

string_array1[1][0] = 'H';
string_array1[1][1] = 'i';
string_array1[1][2] = '\0'; /* This is important. You can use 0 or NULL as well*/
...
/* Use string_array1*/
...
free(string_array1[1]); /* Don't forget to free memory after usage */

III. You can declare a two dimensional character array.

char string_array2[2][4]; /* 2 strings of atmost 3 characters can be stored */
string_array2[0][0] = 'O';
string_array2[0][1] = 'l';
string_array2[0][2] = 'a';
string_array2[0][3] = '\0';

string_array2[1][0] = 'H';
string_array2[1][1] = 'i';
string_array2[1][2] = '\0'; /* NUL terminated, thus string of length of 2 */  

IV. You can use pointer to pointer.

char ** string_array3; 
int i;
string_array3 = malloc(2*sizeof(char*)); /* 2 strings */
if(NULL == string_array3)
{
        /* Memory allocation failure handling*/
}

for( i = 0; i < 2; i++ )
{
    string_array3[i] = malloc(3); /* String can hold at most 2 characters */
    if(NULL == string_array3[i])
    {
        /* Memory allocation failure handling*/
    }
}

strcpy(string_array3[0], "Hi");

string_array3[1][0]='I';
string_array3[1][1]='T';
string_array3[1][2]='\0';

/*Use string_array3*/

for( i = 0; i < 2; i++ )
{
    free(string_array3[i]);
}

free(string_array3);

Points to remember:

  • If you are creating read-only string, you cannot change the character in the string.

  • If you are filling the character array, make sure you have memory to accommodate NUL character & make sure you terminate your string data with NUL character.

  • If you are using pointers & allocating memory, make sure you check if memory allocation is done correctly & free the memory after use.
  • Use string functions from string.h for string manipulation.

Hope this helps!
P.S.: printf(1,"%s\n",paths[0]); looks shady

Solution 3

paths is an array of 10 pointers to char. However, the initial elements are pointers which can not be dereferenced. These are either wild (for an array in a function) or NULL for a static array. A wild pointer is an uninitialized one.

I would guess yours is static, since paths[0] is NULL. However, it could be a coincidence.

When you dereference a NULL or uninitialized pointer, you're reading or writing to memory you don't own. This causes undefined behavior, which can include crashing your program.

You're actually lucky it doesn't crash. This is probably because the compiler sees you're writing a constant to paths[0][2], and changes your printf to print the constant directly. You can not rely on this.

If you want to have a pointer you're allowed to write to, do:

paths[0] = malloc(string_length + 1);

string_length is the number of characters you can write. The 1 gives you room for a NUL. When you're done, you have to free it.

Solution 4

For your second example, if you know the size of each string you can write e.g.

char paths[10][6];
paths[0][0]='1';

...

paths[0][5]='6';

This way you have 10 strings of length 6 (and use only the first string so far).

Share:
19,326
SMUsamaShah
Author by

SMUsamaShah

Working at Jagex.

Updated on June 15, 2022

Comments

  • SMUsamaShah
    SMUsamaShah almost 2 years

    When I do this

    char *paths[10];
    paths[0] = "123456";
    
    printf(1,"%s\n",paths[0]);
    printf(1,"%c\n",paths[0][2]);
    

    Output:

    123456
    3
    

    But when I you do this

    char *paths[10];
    paths[0][0]='1';
    paths[0][1]='2';
    paths[0][2]='3';
    paths[0][3]='4';
    paths[0][4]='5';
    paths[0][5]='6';
    printf(1,"%s\n",paths[0]);
    printf(1,"%c\n",paths[0][2]);
    

    Output:

    (null)
    3
    

    Why it is null in this case?
    How to create a string array using characters in C? I am a bit new to C and feeling some difficulties to program in C