9

As i know, i can create an array with item inside such as:

char *test1[3]= {"arrtest","ao", "123"};

but how can i store my input into array like code above because i only can code it as

input[10];
scanf("%s",&input) or gets(input);

and it store each char into each space.

How can i store the input "HELLO" such that it stores into input[0] but now

H to input[0],E to input[1], and so on.

1
  • you don't need & before input in scanf("%s", input) Commented Jan 27, 2014 at 9:08

6 Answers 6

8

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]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

what does 256 means in strings array?
@alex that is the length of each string. So there is an array of 3 strings, and each string has space for 256 characters (really only 255 characters, because there is always a terminating null character (\0) at the end of a string)
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.

Comments

0

Did not really understand what you need. But here is what I guessed.

char *a[5];//array of five pointers

for(i=0;i<5;i++)// iterate the number of pointer times in the array
{
char input[10];// a local array variable
a[i]=malloc(10*sizeof(char)); //allocate memory for each pointer in the array here
scanf("%s",input);//take the input from stdin
strcpy(a[i],input);//store the value in one of the pointer in the pointer array
}

1 Comment

example if i input "HELLO" when i printf("%s", input); my output should HELLO but not H.
0

try below code:

char *input[10];
input[0]=(char*)malloc(25);//mention  the size you need..
scanf("%s",input[0]);
printf("%s",input[0]);

Comments

0
int main()
{

int n,j;
cin>>n;
char a[100][100];
for(int i=1;i<=n;i++){
    j=1;
    while(a[i][j]!=EOF){
        a[i][j]=getchar();
        j++;
    }
}

1 Comment

Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

This code inspired me on how to get my user input strings into an array. I'm new to C and to this board, my apologies if I'm not following some rules on how to post a comment. I'm trying to figure things out.

#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]);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.