When I use this code it compile without errors ,but when run it ,it shows segmentation fault.I have general idea about segmentation fault,but I can't identify why it happen in this code.Eventhough I use method strcmp (const type) I use the array in the for loop.So I think I chage the location one by one and I didn't go to again write in const memory.This is my code.Can u pls correct this code?
#include <stdio.h>
#include <string.h>
int main()
{
char *string[10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
int i;
int j;
int k;
int ptr;
k=0;
printf("0");
for(i=1;i<10;i+ +)
{
for (j=0;j<i;j++)
{
ptr = strcmp(string [i][2], string[j][2]);
if(ptr==0)
printf("%d",(k) );
}
ptr = strcmp(string [i][2], string[j-1][2]);
if(ptr!=0)
{
k++;
printf("%d",k);
}
}
return 0;
}
Thanks
Mihiri
#include <stdio.h>
#include <string.h>
int main()
{
char *string[10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
int i;
int j;
int k;
int ptr;
k=0;
printf("0");
for(i=1;i<10;i+ +)
{
for (j=0;j<i;j++)
{
ptr = strcmp(string [i][2], string[j][2]);
if(ptr==0)
printf("%d",(k) );
}
ptr = strcmp(string [i][2], string[j-1][2]);
if(ptr!=0)
{
k++;
printf("%d",k);
}
}
return 0;
}
Thanks
Mihiri
Comment