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]);
}
Answer from cybermage14 on Stack OverflowVideos
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.
The idea regarding '\0' in wizzwizz4's answer helped me to solve this issue. However, I have done it in another way:
#include <stdio.h>
int main(){
char input[50];
printf("%s\n","Enter The String:");
fgets(input,sizeof(input),stdin);
for(int i=0; input[i]!='\0'; i++){
printf("%c",input[i]);
}
}
It is working great and it's quite short. This is a good, safe approach, if we exclude that the printed string won't have the '\0' itself.
I suggest you keep the length of the string available at all times, in a variable. Alternatively you can use the function strlen().
Most string related functions have a prototype in <string.h>. You need to include that standard header.
#include <string.h>
char text[51] = {0};
int textlen = 0; // should be size_t
// possibly, in other lang: text = "foobar"
strcpy(text, "foobar");
textlen = strlen(text); // 6 in this easy case
// possibly, in other lang: text = text + character
if (textlen < 50) {
text[textlen++] = character; // add character to text, update textlen
text[textlen] = '\0'; // remember the terminator
} else {
fprintf(stderr, "not enough space for all characters.\n");
exit(EXIT_FAILURE);
}
Read about strings, for example, at chapter 8 of the c-faq
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
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 | | |
In c a string is a pointer to some block of memory each byte of which contains a character. Say I have the following code:
char * helloString = "hello"
Once this has run all that is stored in helloString is the position in memory of the character 'h'. The next 5 positions in memory contain bytes representing 'e', 'l', l', 'o' and '\0' the last being a special character called NULL which tells c where the end of the string is.
In order to copy a string at runtime to have to first allocate some memory to put the characters then copy them across to the new memory. The reason that strcpy failed in your example is that there was no memory allocated for your program to copy the characters into. You need to do something like this:
int len = strlen(argv[1]);
char test[len]; //or char * test = malloc(len);
strcpy(argv[1], test);
Note however that this code will do very bad things (seg fault if you're lucky) if argv[1] doesn't have a '\0' at the end!
Since you mentioned it is pseudo code, I cannot help with fixing your code. You must allocate memory for your test variables or initialize them with length that fits your input.