Create an array of strings without allocating each string - c

I am trying to figure out how to create an array of strings (considering I know the max length of each string).
char** strings = NULL;
strings = malloc (5*sizeof(char*));
Once I did that, how can I just fill the array without the need to allocate each string separately? Lets say I know the max length of a string is 20, how to I set it?
After the allocation of the string I wish to do the following:
strings[0] = "string";
strings[1] = "another string";
etc.
Thanks

You can declare an array of pointers to char and then assign string literals to those pointers
char *strings[5];
strings[0] = "string";
strings[1] = "another string";
/* ... */
But note that, these strings will be immutable.
You can also use an array of char arrays
char strings[5][20]; // As you know max length of string is 20
strcpy(strings[0], "string");
strcpy(strings[1], "another string");
/* ... */
One of the advantage of latter is strings will be mutable.

If you know the maximum size of each line, and if you know the maximum number of lines, you could simply define a two-dimensional array of chars, i.e. char arr[5][20+1]. Then you will have reserved space for up to 5 lines, each comprising up to 20 characters (+ null char).
And you could also define a type alias representing such a line (if you like):
#define MaxLineLength 20
typedef char Line[MaxLineLength+1];
int main() {
Line input = { 0 };
scanf("%20s", input);
Line a[5] = { 0 };
strcpy(a[0], input);
strcpy(a[1], "string1");
return 0;
}

Related

Should I manually allocate string ending char '\0' in c?

I'm new to c, and confused by string ending char '\0', should I allocate it?
for example, I want to store a string with a max length of 6;
If I use array, should I use char str[6] or char str[7]?
char as[3] = "abc";
printf("%s\n", as);
//seems no problem
If I use char pointer, should I use char *str = malloc(6) or char *str = malloc(7)?
For an array that is pre-initialized, you don't need to write a number in the brackets. You can just write
char str[] = "this is my string";
And the compiler will automatically calculate the number of bytes needed.
But for malloc, you must add 1. Ex:
char *strdup(const char *str)
{
char *ret = malloc(strlen(str) + 1);
strcpy(ret, str);
return ret;
}
You should be using string length + 1. In your case you must use 7 while declaring the char array.
The example you provided would have worked because of the undefined behaviour shown by printf().
In addition to stackptr's answer:
If you are planning to overwrite your array:
char str[30] = "abc";
...
strcpy(str, "Hello world"); /* This will overwrite the content of "str" */
... the length of the array must be the maximum length of the string plus 1.
In the example above you may write strings of up to 29 characters length to the array.
Note that the following definition:
char str[] = "abc";
... implicitly creates an array of 4 characters length so you are limit to 3 characters.

unsure about initializing one dimensional char array with array of strings

As per my understanding array of strings can be initialized as shown below or using two dimensional array. Please tell is there any other possibility.
char *states[] = { "California", "Oregon", "Washington", "Texas" };
I have observed in U-boot source that environment variables are stored in one dimensional array as shown here:
uchar default_environment[] = {
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
#endif
...
"\0"
};
Can you help me understand this?
A "string" is effectively nothing more than a pointer to a sequence of chars terminated by a char with the value 0 (note that the sequence must be within a single object).
char a[] = {65, 66, 67, 0, 97, 98, 99, 0, 'f', 'o', 'o', 'b', 'a', 'r', 0, 0};
/* ^ ^ ^ ^ */
In the above array we have four elements with value 0 ... so you can see that as 4 strings
// string 1
printf("%s\n", a); // prints ABC on a ASCII computer
// string 2
printf("%s\n", a + 4); // prints abc on a ASCII computer
// string 3
printf("%s\n", a + 8); // prints foobar
// string 4
printf("%s\n", a + 14); // prints empty string
As per my understanding array of strings can be initialized as shown below or using two dimensional array. Please tell is there any other possibility.
I have observed in U-boot source that environment variables are stored in one dimensional array.
If you have the implication that this default_environment is an array of strings, then it is not. This has nothing to do with array of strings initialization as in your first example.
You can try remove all #ifdef and #endif, then it'd be clear that default_environment is simply a concatenation of individual strings. For instance, "bootargs=" CONFIG_BOOTARGS "\0". Notice the \0 at the end, it will ensure that the string assigned to default_environment will not get pass the first line, given CONFIG_BOOTARGS is defined.
uchar default_environment[] = {
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
#endif
...
"\0"
};
They are not creating an array of strings there, such as your char *states[], it's a single string that is being created (as a char[]). The individual 'elements' inside the string are denoted by zero-termination.
To translate your example
char *states[] = { "California", "Oregon", "Washington", "Texas" };
to their notation would be
char states[] = { "California" "\0" "Oregon" "\0" "Washington" "\0" "Texas" "\0" "\0" };
which is the same as
char states[] = { "California\0Oregon\0Washington\0Texas\0\0" };
You can use these by getting a pointer to the start of each zero-terminated block and then the string functions, such as strlen will read until they see the next '\0' character.
As for the why of it, #M.M.'s comment gives some good indication.
If we simplify the question to what is the difference between initialising a char *foo versus a char foo[100] it might help. Check out the following code:
char buffer1[100] = "this is a test";
char *buffer2 = "this is a test";
int main(void)
{
printf("buffer1 = %s\n", buffer1);
buffer1[0] = 'T';
printf("buffer1 = %s\n", buffer1);
printf("buffer2 = %s\n", buffer2);
buffer2[0] = 'T';
printf("buffer2 = %s\n", buffer2); // this will fail
}
In the first case (buffer1) we initialise a character array with a string. The compiler will allocate 100 bytes for the array, and initialise the contents to "this is a test\0\0\0\0\0\0\0\0\0...". This array is modifiable like any other heap memory.
In the second case, we didn't allocate any memory for an array. All we asked the compiler to do is set aside enough memory for a pointer (4 or 8 bytes typically), then initialise that to point to a string stored somewhere else. Typically the compiler will either generate a separate code segment for the string, or else just store it inline with the code. In either case this string is read-only. So on the final line where I attempted to write to it, it caused a seg-fault.
That is the difference between initialising an array and a pointer.
The common technique is to use an array of pointers (note: this is different from a 2D array!) as:
char *states[] = { "California", "Oregon", "Washington", "Texas" };
That way, states is an array of 4 char pointer, and you use it simply printf("State %s", states[i]);
For completeness, a 2D array would be char states[11][5] with all rows having same length which is pretty uncommon, and harder to initialize.
But some special use cases or API(*) require (or return) a single char array where strings are (normally) terminated with \0, the array itself being terminated by an empty element, that it two consecutive \0. This representation allows a single allocation bloc for the whole array, when the common array of pointers has the array of pointers in one place and the strings themselves in another place. By the way, it is easy to rebuild an array of pointers from that 1D characater arrays with \0 as separators, and it is generally done to be able to easily use the strings.
The last interesting point of the uchar default_environment[] technique is that it is a nice serialization: you can directly save it to a file and load it back. And as I have already said, the common usage is then to build an array of pointers to access easily to the individual strings.
(*) For example, the WinAPI functions GetPrivateProfileSection and WritePrivateProfileSection use such a representation to set or get a list of key=value strings in one single call.
Yes, you can create and initialized array of string or hierarchy of arrays using pointers. Describing it in details in case someone needs it.
A single char
1. char states;
Pointer to array of chars.
2. char *states = (char *) malloc(5 * sizeof(char)):
Above statement is equivalent to char states[5];
Now its up to you if you initialize it with strcpy() like
strcpy(states, "abcd");
or use direct values like this.
states[0] = 'a';
states[1] = 'b';
states[2] = 'c';
states[3] = 'd';
states[4] = '/0';
Although if you store any other char on index 4 it will work but it would be better to end this using null character '\0' .
Pointer to array of pointers or pointer to a matrix of chars
3. char ** states = (char **) malloc(5 * sizeof(char *));
It is an array of pointers i.e. each element is a pointer, which can point to or in other word hold a string etc. like
states[0] = malloc ( sizeof(char) * number );
states[1] = malloc ( sizeof(char) * number );
states[2] = malloc ( sizeof(char) * number );
states[3] = malloc ( sizeof(char) * number );
states[4] = malloc ( sizeof(char) * number );
Above statement is equivalent to char states[5][number];
Again its up to you how you initialize these pointers to strings i.e.
strcpy( states[0] , "hello");
strcpy ( states[1], "World!!");
states[2][0] = 'a';
states[2][1] = 'b';
states[2][2] = 'c';
states[2][3] = 'd';
states[2][4] = '\0';
Pointer to matrix of pointers or pointer to 3D chars
char *** states = (char ***) malloc(5 * sizeof(char**));
and so on.
Actually each of these possibilities reaches somehow to pointers.

How can I duplicate an array ? (with same size)

I want to create a new array with the same size of "chaine" but only after the function
char chaine[Lenght]; //Lenght = 20
function(chaine, sizeof(chaine));
When I called "function" the size of "chaine" is changing randomly.
The new array "chaine2" also needs to be full of " * " characters.
Let met try to explain with some printf :
printf("chaine = %s\n", chaine);
will show on screen something like : chaine = WORDS (5 characters)
And i want "chaine2" to be shown like this : chaine2 = ***** (5 stars)
I apologize for my english, thank you for reading
#include <stdio.h>
#include <stdlib.h>
char *dup_and_fillStar(const char *src, const size_t size){
char *p,*ret;
ret=p=(char*)malloc(sizeof(char)*size);
while(*src++)
*p++='*';
*p = '\0';
return ret;
}
#define Length 20
int main(void){
char chaine[Length] = "WORDS";
char *chaine2;
chaine2 = dup_and_fillStar(chaine, sizeof(chaine));
printf("chine = %s\n", chaine);
printf("chine2 = %s\n", chaine2);
free(chaine2);
return(0);
}
Remember that char arrays are special, in the sense that they have a size, which you specify when you declare them, and a length, which depends on their contents. The size of an array is the amount of memory that's been allocated to it. The length of a string is the number of characters before a terminating null ('\0').
some_func() {
int len = 20; // Size of the array
char chaine[len]; // Uninitialized array of size 20.
memset(chaine, '\0', sizeof(chaine)); // Init to all null chars, len = 0
strcpy(chaine, "WORDS"); // Copy a string, len = 5
char *chaine2 = function(chaine, sizeof(chaine));
printf("%s\n", chaine2);
free (chaine2);
}
When you pass an array to a function, it's treated like a pointer. So sizeof(str) inside the function will always return the size of pointer-to-char, and not the size of the original array. If you want to know how long the string is, make sure it's null-terminated and use strlen() like this:
char *function(char *str, int len) {
// Assume str = "WORDS", len = 20.
char *new_str = malloc(len); // Create a new string, size = 20
memset(new_str, '\0', len); // Initialize to nulls
memset(new_str, '*', strlen(str)); // Copy 5 '*' chars, len = 5
return new_str; // Pointer to 20 bytes of memory: 5 '*' and 15 '\0'
}
I'm going to assume that you're using C99 (and so are able to use variable length arrays).
If you want to create the array outside of function() (where chaine is still an array), you can simply do:
char chaine2[Lenght];
Or, probably better:
char chaine2[sizeof(chaine)];
If you're inside function(), note that chaine will be a pointer, since the compiler sees an array definition in a function parameter as a pointer to it's first element (if this is confusing, remember that pointers are not arrays)
So, even if function is defined like this:
function(char chaine[], size_t length) {
the compiler will see it as:
function(char *chaine, size_t length) {
Either way, you can create chaine2 by saying:
char chaine2[length];
but NOT
char chaine2[sizeof(chaine)]; // DONT DO THIS
since the sizeof will return the size of the pointer instead of the size of the array.
However you create it, if you want to fill the new array with '*' characters, you can use memset():
memset(chaine2,'*',sizeof(chaine2));

Printing an array of characters

I have an array of characters declared as:
char *array[size];
When I perform a
printf("%s", array);
it gives me some garbage characters, why it is so?
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
This url indicates printf takes in the format of: `int printf ( const char * format, ... );
#include <stdio.h>
#include <string.h>
#define size 20
#define buff 100
char line[buff];
int main ()
{
char *array[100];
char *sep = " \t\n";
fgets(line, buff, stdin);
int i;
array[0] = strtok(line, sep);
for (i = 1; i < size; i++) {
array[i] = strtok(NULL, sep);
if (array[i] == NULL)
break;
}
return 0;
}
You declare an array of characters like so:
char foo[size];
You seem to have it mixed up with char *, which is a pointer to a character. You could say
char *bar = foo;
which would make bar point to the contents of foo. (Or, actually, to the first character of foo.)
To then print the contents of the array, you can do one of the following:
// either print directly from foo:
printf("%s", foo);
// or print through bar:
printf("%s", bar);
Note, however, that C performs no initialization of the contents of variables, so unless you specifically set the contents to something, you'll get garbage. In addition, if that garbage doesn't happen to contain a \0; that is, a char with value 0, it will keep on outputting past the end of the array.
Your array is not initialized, and also you have an array of pointers, instead of an array of char's. It should be char* array = (char*)malloc(sizeof(char)*size);, if you want an array of char's. Now you have a pointer to the first element of the array.
Why are we making such a simple thing sound so difficult?
char array[SIZE];
... /* initialize array */
puts(array); /* prints the string/char array and a new line */
/* OR */
printf("%s", array); /* prints the string as is, without a new line */
The char in array after the end of what you want to be your string (ie. if you want your string to read "Hello" that would be the next char after the 'o') must be the terminating NUL character '\0'. If you use a C function to read input that would automatically be appended to the end of your buffer. You would only need to worry about doing it manually if you were individually writing characters to your buffer or something for some reason.
EDIT: As with pmg's comment, the '\0' goes wherever you want the string to end, so if you wanted to shorten your string you could just move it up closer to the front, or to have an empty string you just have array[0] = '\0';. Doing so can also be used to tokenise smaller strings inside a single buffer, just as strtok does. ie. "Part1\0Part2\0Part3\0". But I think this is getting away from the scope of the question.
ie. you wanted to store the first 3 chars of the alphabet as a string (don't know why anyone would do it this way but it's just an example):
char array[4];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = '\0';
printf("%s\n", array);
If you have something like char array[] = "Hello"; the '\0' is automatically added for you.
char *array[size];
array is not a char * with that, it's more like a char ** (pointer to an array of chars, with is similar to pointer to pointer to char).
If all you need is a C string, either:
char array[size];
and make sure you 0-terminate it properly, or
char *array;
and make sure you properly allocate and free storage for it (and 0-terminate it too).

How do I create an array of strings in C?

I am trying to create an array of strings in C. If I use this code:
char (*a[2])[14];
a[0]="blah";
a[1]="hmm";
gcc gives me "warning: assignment from incompatible pointer type". What is the correct way to do this?
edit: I am curious why this should give a compiler warning since if I do printf(a[1]);, it correctly prints "hmm".
If you don't want to change the strings, then you could simply do
const char *a[2];
a[0] = "blah";
a[1] = "hmm";
When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".
If you do want to be able to change the actual string content, the you have to do something like
char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");
This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.
There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:
char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
...
strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");
You can add a list of initializers as well:
char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};
This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.
Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:
char *strs[NUMBER_OF_STRINGS];
Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:
char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};
Instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:
strs[i] = "bar";
strs[i] = "foo";
But you may not be able to change the string's contents; i.e.,
strs[i] = "bar";
strcpy(strs[i], "foo");
may not be allowed.
You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:
strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");
BTW,
char (*a[2])[14];
Declares a as a 2-element array of pointers to 14-element arrays of char.
Ack! Constant strings:
const char *strings[] = {"one","two","three"};
If I remember correctly.
Oh, and you want to use strcpy for assignment, not the = operator. strcpy_s is safer, but it's neither in C89 nor in C99 standards.
char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];
strcpy(arr[0], "blah");
Update: Thomas says strlcpy is the way to go.
Here are some of your options:
char a1[][14] = { "blah", "hmm" };
char* a2[] = { "blah", "hmm" };
char (*a3[])[] = { &"blah", &"hmm" }; // only since you brought up the syntax -
printf(a1[0]); // prints blah
printf(a2[0]); // prints blah
printf(*a3[0]); // prints blah
The advantage of a2 is that you can then do the following with string literals
a2[0] = "hmm";
a2[1] = "blah";
And for a3 you may do the following:
a3[0] = &"hmm";
a3[1] = &"blah";
For a1 you will have to use strcpy() (better yet strncpy()) even when assigning string literals. The reason is that a2, and a3 are arrays of pointers and you can make their elements (i.e. pointers) point to any storage, whereas a1 is an array of 'array of chars' and so each element is an array that "owns" its own storage (which means it gets destroyed when it goes out of scope) - you can only copy stuff into its storage.
This also brings us to the disadvantage of using a2 and a3 - since they point to static storage (where string literals are stored) the contents of which cannot be reliably changed (viz. undefined behavior), if you want to assign non-string literals to the elements of a2 or a3 - you will first have to dynamically allocate enough memory and then have their elements point to this memory, and then copy the characters into it - and then you have to be sure to deallocate the memory when done.
Bah - I miss C++ already ;)
p.s. Let me know if you need examples.
If you don't want to keep track of number of strings in array and want to iterate over them, just add NULL string in the end:
char *strings[]={ "one", "two", "three", NULL };
int i=0;
while(strings[i]) {
printf("%s\n", strings[i]);
//do something
i++;
};
Or you can declare a struct type, that contains a character arry(1 string), them create an array of the structs and thus a multi-element array
typedef struct name
{
char name[100]; // 100 character array
}name;
main()
{
name yourString[10]; // 10 strings
printf("Enter something\n:);
scanf("%s",yourString[0].name);
scanf("%s",yourString[1].name);
// maybe put a for loop and a few print ststements to simplify code
// this is just for example
}
One of the advantages of this over any other method is that this allows you to scan directly into the string without having to use strcpy;
If the strings are static, you're best off with:
const char *my_array[] = {"eenie","meenie","miney"};
While not part of basic ANSI C, chances are your environment supports the syntax. These strings are immutable (read-only), and thus in many environments use less overhead than dynamically building a string array.
For example in small micro-controller projects, this syntax uses program memory rather than (usually) more precious ram memory. AVR-C is an example environment supporting this syntax, but so do most of the other ones.
In ANSI C:
char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";
The string literals are const char *s.
And your use of parenthesis is odd. You probably mean
const char *a[2] = {"blah", "hmm"};
which declares an array of two pointers to constant characters, and initializes them to point at two hardcoded string constants.
Your code is creating an array of function pointers. Try
char* a[size];
or
char a[size1][size2];
instead.
See wikibooks to arrays and pointers
hello you can try this bellow :
char arr[nb_of_string][max_string_length];
strcpy(arr[0], "word");
a nice example of using, array of strings in c if you want it
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
int i, j, k;
// to set you array
//const arr[nb_of_string][max_string_length]
char array[3][100];
char temp[100];
char word[100];
for (i = 0; i < 3; i++){
printf("type word %d : ",i+1);
scanf("%s", word);
strcpy(array[i], word);
}
for (k=0; k<3-1; k++){
for (i=0; i<3-1; i++)
{
for (j=0; j<strlen(array[i]); j++)
{
// if a letter ascii code is bigger we swap values
if (array[i][j] > array[i+1][j])
{
strcpy(temp, array[i+1]);
strcpy(array[i+1], array[i]);
strcpy(array[i], temp);
j = 999;
}
// if a letter ascii code is smaller we stop
if (array[i][j] < array[i+1][j])
{
j = 999;
}
}
}
}
for (i=0; i<3; i++)
{
printf("%s\n",array[i]);
}
return 0;
}
char name[10][10]
int i,j,n;//here "n" is number of enteries
printf("\nEnter size of array = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<1;j++)
{
printf("\nEnter name = ");
scanf("%s",&name[i]);
}
}
//printing the data
for(i=0;i<n;i++)
{
for(j=0;j<1;j++)
{
printf("%d\t|\t%s\t|\t%s",rollno[i][j],name[i],sex[i]);
}
printf("\n");
}
Here try this!!!
I was missing somehow more dynamic array of strings, where amount of strings could be varied depending on run-time selection, but otherwise strings should be fixed.
I've ended up of coding code snippet like this:
#define INIT_STRING_ARRAY(...) \
{ \
char* args[] = __VA_ARGS__; \
ev = args; \
count = _countof(args); \
}
void InitEnumIfAny(String& key, CMFCPropertyGridProperty* item)
{
USES_CONVERSION;
char** ev = nullptr;
int count = 0;
if( key.Compare("horizontal_alignment") )
INIT_STRING_ARRAY( { "top", "bottom" } )
if (key.Compare("boolean"))
INIT_STRING_ARRAY( { "yes", "no" } )
if( ev == nullptr )
return;
for( int i = 0; i < count; i++)
item->AddOption(A2T(ev[i]));
item->AllowEdit(FALSE);
}
char** ev picks up pointer to array strings, and count picks up amount of strings using _countof function. (Similar to sizeof(arr) / sizeof(arr[0])).
And there is extra Ansi to unicode conversion using A2T macro, but that might be optional for your case.
Each element is a pointer to its first character
const char *a[2] = {"blah", "hmm"};
A good way is to define a string your self.
#include <stdio.h>
typedef char string[]
int main() {
string test = "string";
return 0;
}
It's really that simple.

Resources