In the loop, the elements in the 2d array, d has the right strings. but once it exits the loop, all the elements in d are the last string.. how should i resolve this?
Code:
#include<stdio.h>
#include<string.h>
int main (void)
{
char s[]="abcd:efgh:ijkl:\0";
char *d[100];
printf("s is %s\n",s);
char wasi[]="wasi";
char ahmed[]="ahmed";
int k = strlen(s); int x=0,y=0; int z=0;
char c = s[x]; char test[1000]; //memset(test,"0",sizeof(test));
while(x<k)
{
while(c!=':')
{
if(c=='\0')
{break;}
test[y]=c;
x++;y++;
c=s[x];
}
test[y]='\0';
//test contains a string between colons
printf("test is %s\n",test);
//printf("z is %d\n",z);
d[z]=test;
strcat(d[z],"\0");
printf("d[%d] is %s\n",z,d[z]);
z++;
x++;c=s[x];
y=0;
if(c=='\0')
{break;}
}
d[z]="\0";
// printing the contents of the string
int i;
for( i=0; i<2;i++)
{
printf("d[%d] = %s\n",i,d[i]);
}
// EVERYTHING INSIDE d IS THE SAME AS THE LAST STRING
return 0;
}