PDA

View Full Version : [SOLVED] String Array in C ---How to recursively add strings?



Zacinfinite
February 17th, 2011, 07:41 AM
<<I just want to use loop to enter strings in array>>

char array[4];
int i=0;

for(i=0;i<=4;i++)
{
printf("enter the %d element",i);
scanf("%s",&array[i]);
}


<<THIS DOESNT WORK! HELP>>

worksofcraft
February 17th, 2011, 07:55 AM
<<I just want to use loop to enter strings in array>>

char array[4];
int i=0;

for(i=0;i<=4;i++)
{
printf("enter the %d element",i);
scanf("%s",&array[i]);
}


<<THIS DOESNT WORK! HELP>>

You have an array of char
what you want is an array of strings...

In C strings are done using array of char so you might might do it like so:


#include <stdio.h>

int main(int argc, char *argv[], char *envp[]) {
char array[4][120];
int i=0;

for(i=1;i<=4;i++)
{
printf("enter the %d element:",i);
scanf("%s",array[i-1]);
}
for(i=1;i<=4;i++)
{
printf("%d element= %s\n",i,array[i-1] );
}
}

Zacinfinite
February 17th, 2011, 08:02 AM
Thanks Man