c - segmentation fault while running the programme -
i have written code parsing string words. here code. can 1 here fix segmentation fault error during run time?
calling fun :
int main() { int count = 0, i; // count hold numbr of words in string line. char buf[max_lenths]; // buffer hold string char *options[max_orgs]; // options hold words got after parsing. printf("enter string"); scanf("%s",buf); count = parser(buf,options); // calling parser for(i = 0; < count; ++i) printf("option %d %s", i, options[i]); return 0; }
called function:
int parser(char str[], char *orgs[]) { char temp[1000];//(char *)malloc(strlen(str)*sizeof(char)); int list = 0; strcpy(temp, str); *orgs[list]=strtok(str, " \t "); while(((*orgs[list++]=strtok(str," \t"))!=null)&&max_orgs>list) list++; printf("count =%d",list); return list; }
note : i'm trying learn c these days, can 1 tutorial (pdf) or site learn these strings pointers, , sending string functions arguments?
int parser(char str[], char *orgs[]){ int list=0; orgs[list]=strtok(str, " \t\n"); while(orgs[list]!=null && ++list < max_orgs) orgs[list]=strtok(null," \t\n"); printf("count = %d\n",list); return list; } int main(){ int count=0,i; char buf[max_lenths]; char *options[max_orgs]; printf("enter string: "); fgets(buf, sizeof(buf), stdin);//input include space character count=parser(buf,options); for(i=0;i<count;++i) printf("option %d %s\n",i,options[i]); return 0; }
Comments
Post a Comment