You can use 2D array to store multiple strings. For 10 strings each of length 100
char variable[10][100];
printf("Enter Strings\n");
for (int i = 0; i < 10 ;i++)
scanf("%100s", variable[i]);
Better to use fgets to read string.
fgets(variable[i], sizeof(variable[i]), stdin);
You can also use dynamic memory allocation by using an array of pointers to char.
You can use 2D array to store multiple strings. For 10 strings each of length 100
char variable[10][100];
printf("Enter Strings\n");
for (int i = 0; i < 10 ;i++)
scanf("%100s", variable[i]);
Better to use fgets to read string.
fgets(variable[i], sizeof(variable[i]), stdin);
You can also use dynamic memory allocation by using an array of pointers to char.
The most efficient way is to have an array of character pointers and allocate memory for them as needed:
char *strings[10];
int main(int ac, char *av[]) {
memset(strings, 0, 10 * sizeof(char *));
for (int i = 0; i < 10; i += 1) {
char ins[100];
scanf("%100s", ins);
strings[i] = malloc(strlen(ins) + 1);
if (strings[i]) {
strcpy(strings[i], ins);
}
}
}
How store multiple string or create an array of strings in C, by using 1D array?
The short answer is: You can't
A string in C is by itself a char array (with a zero termination) so there is no way to have multiple strings in a 1D array.
You can make it a 2D array like:
int main()
{
// Make a 2D array to store
// 4 strings with 9 as max strlen
char str[4][10] = {"Linux", "Ubuntu", "Arch", "Void"};
for (int i=0; i<4; ++i) printf("%s\n", str[i]);
return 0;
}
Another approach is to use a 1D array of char pointers to string literals - like:
int main()
{
// Make a 1D array to store
// 4 char pointers
char *str[4] = {"Linux", "Ubuntu", "Arch", "Void"};
for (int i=0; i<4; ++i) printf("%s\n", str[i]);
return 0;
}
but notice that the strings are not saved in the array. The compiler place the strings somewhere in memory and the array just holds pointers to those strings.
Also notice that in the second example you are not allowed to modify the strings later in the program. In the first example you are allowed to change the strings after the initialization, e.g. doing strcpy(str[0], "Centos");
BTW: This may be of interrest Are string literals const?
It is possible to store multiple strings in a 1D array - the issue is accessing anything beyond the initial string. For example:
char strs[] = “foo\0bar\0bletch\0blurga”;
The 1D array strs contains 4 strings, but if we pass strs to any library function, only ”foo” will be used.. We would need to maintain a separate array of pointers into strs to access anything beyond the first string:
char *ptrs[] = {strs, &strs[4], &strs[8], &strs[15]};
If you need your strings to be contiguous in memory then this is a valid aproach, but it is cumbersome, and will be a massive pain in the ass if you need to update any of them. Better to use a 2D array.
I know that strings are char arrays already and I was wondering if there's a way to store not strings in an array. It would be like storing arrays into an array and that sounds a bit weird.
As far as I know an array is a pointer so I'd tend to say no, but after all pointers are just storing numbers like any variable.
To maybe be clearer, what I'd want to do is have an array that's like that :
array[0] = BLUE
array[1] = RED
array [2] = YELLOW
etc
You need a 2 dimensional character array to have an array of strings:
#include <stdio.h>
int main()
{
char strings[3][256];
scanf("%s %s %s", strings[0], strings[1], strings[2]);
printf("%s\n%s\n%s\n", strings[0], strings[1], strings[2]);
}
Use a 2-dimensional array char input[3][10];
or
an array of char pointers (like char *input[3];) which should be allocated memory dynamically before any value is saved at those locations.
First Case, take input values as scanf("%s", input[0]);, similarly for input[1] and input[2]. Remember you can store a string of max size 10 (including '\0' character) in each input[i].
In second case, get input the same way as above, but allocate memory to each pointer input[i] using malloc before. Here you have flexibility of size for each string.
So I'm new to C and programming and I'm trying to make a simple program to store a series of objects sorted by brand and model and keep that information inside the variable. I was wondering if there's such a way to store the brands names inside this variable called "brand" and later call a brand of my choice from that variable to be shown. Well, I'm pretty sure there's such thing, I've been looking for different data structures, maybe Array is not the suitable one.
Anyway, that's it, I don't want any kind of done stuff, I'm trying to make this project so I can learn more about C, learn to read documentation, learn the "why's" for what I'll be doing in the coding, learn how to look for solving my problem. Thank you already!
If to place the missed semicolon at the end then this statement
const char temp[] = "GET / HTTP/1.0\r\n"
"Host:www.google.com\r\n"
"\r\n";
is equivalent to
const char temp[] = "GET / HTTP/1.0\r\nHost:www.google.com\r\n\r\n";
According to the C Standard in the section where translation phases are described there is written
6. Adjacent string literal tokens are concatenated
Sometines it is convinient to split a long string literal that does not fit a line into several shorter adjacent literals.
const char str[] = "stringstringstring";
const char str[] = "string" "string" "string";
const char str[] = "string"
"string"
"string";
#define NAME "string"
const char str[] = "string" NAME "string";
Will all have the same result. C concatenates adjacent strings.
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.
First: you cannot define an array size with a variable. I mean: char buf[variable]; doesn't work.
You have to do like this:
char **buf;
buf = malloc(sizeof(char) * number_of_strings);
if (buf == NULL)
return (MALLOC_ERROR);
Or with a macro like this:
// in your header file
#define BUF_SIZE 12
// in your .c file
char *buf[BUF_SIZE];
Then you also have to malloc the 2nd dimension of your array. For example :
int i;
i = 0
while (buf[i])
{
buf[i] = malloc(sizeof(char) * string_length);
if (buf[i] == NULL)
return (MALLOC_ERROR);
i++;
}
And don't forget to free all dimensions of your array.
An array of arrays is the way to go.
// Array of size 5 (Don't forget to free!)
char **arrayOfStrings = malloc(5*sizeof(char*));
char *aString = "Hi";
arrayOfStrings[0] = aString;
//Literals work too
arrayOfStrings[1] = "Hallo";
aString = "Ahoy";
arrayOfStrings[2] = aString;
ArrayOfStrings values at end: Hi | Hallo | Ahoy | | |