Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a string array with Characters in C

Tags:

c

string

char

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

like image 347
SMUsamaShah Avatar asked Jan 18 '26 20:01

SMUsamaShah


1 Answers

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

like image 160
another.anon.coward Avatar answered Jan 20 '26 13:01

another.anon.coward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!