How to store strings in an array ?

0 Gitesh Khanna · September 4, 2015
i want to know if their is a way to store a string such as "Hello"  etc to be saved in an array
eg.
string[0]= "Hello"
string[1]= "bye"


i tried doing this through pointers but i dont seem to succeed :


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i ;
    char *s[30] ;
    char string ;
    
    for(i=1 ; i<=5 ; i++)
    {
    scanf("%s",string) ;
    *s = string ;
    }
    for(i=1 ; i<=5 ; i++)

    {


    printf("%s",*s) ;
    }



    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Sha Kal · September 5, 2015
char mystring[] = " Hello " ;
char mystring2[] = " bye " ;
0 Gitesh Khanna · September 8, 2015
^ Sha Kal.
i want to store a STRING in an array.
not a CHAR as array.

such that each address stores a STRING.
0 Dol Lod · September 11, 2015
So you want a two dimensional array like this?

char** x=malloc(sizeof(char*)*2);
x[0]="Hello"
x[1]="World"
  • 1

C

107,149 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator