how can i initialize array of string in c?

Thursday, July 10, 2014

I was trying to initialize the array of string in c..I could do that in c++ using the below code..



#include<iostream>
#include<string>
using namespace std;
int main()
{
string* ptr=new string[2]{"hi","how"};
cout<<ptr[0]<<endl<<ptr[1]<<endl;
}


I know a way in c language..The way is below..



#include <stdio.h>
typedef char * string;

int main(void)
{
string strs[5]; // Make 5 strings
int i;

strs[0] = "first string";
strs[1] = "second string";
strs[2] = "third string";
strs[3] = "this is the fourth string";
strs[4] = "and finally this is the fifth string";

for(i = 0;i < 5;++i)
puts(strs[i]);

return 0;
}


As new can't be used in c..So how can i used the same method using malloc() instead of new??







http://ift.tt/1xYT1Yi