Is It Possible to Have Multiple Strings in a Single Dimensional Array in Ansi C?

I know how to make string array using a two dimensional array but can I do something like this:

char array[];
array[1]="bob";
array[2]="cat";
5

4 Answers

Yes, but not the way you're trying to do it.

For example, you could do this:

char arr[] = "bob\0cat";

arr contains two strings, "bob" starting at arr[0] and "cat" starting at arr[4]. This isn't particularly convenient, since you have to traverse the array to find the strings, but it does use memory efficiently.

Each element of arr is a single char, so you can't store a string in each element, but you can store multiple strings consecutively.

There are other ways to store lists of strings. The most common is to define an array of char* pointers, with each pointer pointing to (the first character of) a string that's been allocated elsewhere. The argv parameter of main is the most common example of this approach.

In your example:

char array[];

This is invalid; if you want to define an array object, you have to specify the number of elements.

array[1]="bob";
array[2]="cat";

These assignments are both invalid. array[1] and array[2] are both single char objects. You can't assign a string to a single char. Actually the expression "bob" is implicitly converted to a char* pointer -- which you still can't assign to a char object.

If you defined your array as an array of char* pointers, those assignments would be valid, and in fact that's the usual way to represent a list of strings:

char *array[2];  /* an array of pointers */
array[0] = "bob";
array[1] = "cat";

The strings themselves are allocated outside the array; the array contains pointers to them. Each pointer actually points to a single char object, but we can use that pointer to access the entire string.

Yes you can, but you have to use an array of pointers:

#include<stdio.h>
#include<conio.h>
int main()
{
    char *ar[]={"one","two","three"};//array of pointer
    for(int i=0;i<3;i++)
    {
        printf("%s\n",ar[i]);
    }
}   

Yes, possible only if you can imagine the memory as a raw contiguous unit and know exactly what you are doing.

for example,

lets consider a memory block of 25 bytes as below

00                        04 
+----+----+----+----+----+
|    |    |    |    |    |
+----+----+----+----+----+ 09
|    |    |    |    |    |
+----+----+----+----+----+ 14
|    |    |    |    |    |
+----+----+----+----+----+ 19
|    |    |    |    |    |
+----+----+----+----+----+ 24
|    |    |    |    |    |
+----+----+----+----+----+ 29

Lets consider my allocation was like this

void *p = malloc( 5 * 5 * sizeof( char ) );

I can access a row of size 5 with pure pointer arithmatic as

for instance lets say I'll copy a string in 3rd row

memcpy ( p+(5*3) , "Hello", 5 );

Only problem is maintaining this raw area is little too complicated than usual, besides there are advantages in usage.

1

If you're talking about storing pointers to strings, then yes:

const char *strings[SIZE];
strings[0] = "bob";
strings[1] = "cat";
...

Note that we're only storing the addresses of the string literals "bob" and "cat" in the strings array; the contents of the string literals are stored somewhere else.

strings is declared as an array of const char * since attempting to modify the contents of a string literal leads to undefined behavior.

If you want to store the contents of multiple strings in a single 1-D array, then you'll need to concatenate them together, something like:

char dest[SIZE] = {0}; // want to make sure at least the first element is 0
strcat( dest, "bob" );
strcat( dest, "cat" );

after which dest contains the string "bobcat", where "bob" starts at index 0 and "cat" starts at index 3. Note that you cannot use the = operator to assign string contents; you must either use a library function like strcat, or you must assign each character individually, like

dest[0] = 'b';
dest[1] = 'o';
dest[2] = 'b';
...

If you want to keep track of where individual strings begin and end within that 1-D sequence, then you will need to add some sort of delimiter, like

strcat( dest, "bob" );
strcat( dest, "$" );
strcat( dest, "cat" );

giving you the string "bob$cat"; you could then use strtok or strchr to find individual strings in the list of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest