2

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

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that i don't have the complete string or its length while storing in array. Therefore i must store it character by character. Thanks for very elaborated answer.
@LifeH2O: In that case use pointer array & malloc. If you are not sure of the number of string, there is another option. Use pointer to pointer. Do you know the number of strings when using?
No, I am actually parsing a file and storing strings in array (of predefined size) by checking delimiters.
2
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];

3 Comments

I am almost new to C, can you help me creating a string array using characters, as I was trying to do in second example?
Instead I got your point. When i do paths[0] = "123456"; it is automatically initialized to it length (6). But when I do paths[0][0] it does not know the size of second dimension of the array. Correct?
It is not about the dimension. String literals are allocated memory automatically by the underlying implementation. But when you are saying [0][0], you are trying to access a valid memory location which haven't assigned though.
2

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.

Comments

1

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).

1 Comment

You should have values only till paths[0][4], paths[0][5] should be NUL character
0

You can define the string yourself, right after

#inlcude <stdio.h>

like this

typedef char string[];

and in main you can do this

string paths = "123456";
printf("%s\n", paths);
return 0;

so your code would look like this

#include <stdio.h>
typedef char string[];

int main() {
    string paths = "123456";
    printf("%s", paths);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.