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 arr
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]);
}
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]);
}
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++;
}
}
try below code:
char *input[10];
input[0]=(char*)malloc(25);//mention the size you need..
scanf("%s",input[0]);
printf("%s",input[0]);
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.
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
}