1

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";
Kolong Kinghu
  • 41
  • 1
  • 6
  • 2
    possible duplicate of [How do I create an array of strings in C?](http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c) – too honest for this site Sep 17 '15 at 16:20
  • I've read that post but I'm not trying use a multi-dimensional array or pointers – Kolong Kinghu Sep 17 '15 at 16:21
  • 1
    You already do. Guess what `"bob"` is? If you do not want to, the answer is "no"! Think for yourself how that should work. – too honest for this site Sep 17 '15 at 16:22
  • You could, whatever the point is,but you would have to either index each one, or keep a count of how many, so that you know whether the `'\0'` signifies the end of a string or the end of all the array. – Weather Vane Sep 17 '15 at 16:25
  • You could store into a single-dimensional array, but I can't think of a useful way to do it without either hard-coding the strings or at least temporarily storing into an array of pointers. I mean, you could use `argv` to get strings from the command line, but that is an array of pointers, too. – Austin Mullins Sep 17 '15 at 16:35

4 Answers4

5

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

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]);
    }
}   
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

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.

asio_guy
  • 3,667
  • 2
  • 19
  • 35
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.

John Bode
  • 119,563
  • 19
  • 122
  • 198