| | |
use of scanf()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
> scanf("%s", &retry);
First buffer overflow - retry is a single char, you can't put a string into a single char.
Second problem, calling a function recursively just to implement a while loop is poor form.
Third, you're mixing scanf() with fgets(), which will surely lead to trouble at some point.
Separate the function into two steps
- readInput
- TokeniseInput.
Both will be a lot simpler than what you have at the moment.
First buffer overflow - retry is a single char, you can't put a string into a single char.
Second problem, calling a function recursively just to implement a while loop is poor form.
Third, you're mixing scanf() with fgets(), which will surely lead to trouble at some point.
Separate the function into two steps
- readInput
- TokeniseInput.
Both will be a lot simpler than what you have at the moment.
Thats why we insist to post the code or if you cant, then U need to reconstruct the problem in smaler code. The post I posted don't give seg error, if U don't belive me compile and run the code which I posted.
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Aug 2006
Posts: 78
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
//# include<stdio.h> void function(); main() { function(); } void function() { int i,index,k,m; char str1[100],retry,str[100],*ptr,*ctr; int j=0; printf("enter the string"); fgets(str, sizeof(str), stdin); if((ptr = strchr(str, '\n')) != NULL) *ptr = '\0'; printf("enter the string to be compared"); fgets(str1, sizeof(str1) , stdin); if((ctr = strchr(str1, '\n')) != NULL) *ctr = '\0'; /* printf("enter index"); */ /* canf("%d",& index); */ char *ch,*pch; ch=strtok(str,":"); printf(" ratg"); if (ch==NULL) { printf("entered string is incorrect,reenter? y/n"); scanf("%s",&retry); if(retry=='y') function(); else return; } else printf("\nch is NULL\n"); while(ch!=NULL) { pch=strstr(ch,";"); i=pch-ch; /* strncpy(tr[j].member1,ch,i); */ /* strcpy(tr[j].member2,pch+1); */ j++; ch=strtok(NULL,":"); }
i tried your code . yes it is not giving any segmentation error but it is not performing desired operation
first of all it is giving a warning:
assignment makes pointer from integer without a cast
for statements 23 and 42
then it is not going inside the loop for asking the user if he wants to enter again the string
i am working on linux , i hope this is not creating any problems
i will be posting a reconstructed program of mine for your convinience
•
•
Join Date: Aug 2006
Posts: 78
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
// # include<stdio.h> void function(); main() { function(); } void function() { int i,index,k,m; char str1[100],retry,str[100],*ptr,*ctr; int j=0; printf("enter the string"); fgets(str, sizeof(str), stdin); if((ptr = strchr(str, '\n')) != NULL) *ptr = '\0'; printf("enter the string to be compared"); fgets(str1, sizeof(str1) , stdin); if((ctr = strchr(str1, '\n')) != NULL) *ctr = '\0'; /* printf("enter index"); */ /* canf("%d",& index); */ char *ch,*pch; ch=strtok(str,":"); printf(" ratg"); if (ch==NULL) { printf("entered string is incorrect,reenter? y/n"); scanf("%s",&retry); if(retry=='y') function(); else return; } else printf("\nch is NULL\n"); while(ch!=NULL) { pch=strstr(ch,";"); i=pch-ch; /* strncpy(tr[j].member1,ch,i); */ /* strcpy(tr[j].member2,pch+1); */ j++; ch=strtok(NULL,":"); } }
i tried your code but it is not giving desired result, it is not asking the user if he wants to reenter
also there are warnings for statements 23 and 42
assignment makes pointer from integer without a cast
i am working on linux , i hope that is not causing any problem
i am working on reconstruction of the code for your convinience
•
•
Join Date: Aug 2006
Posts: 78
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
//# include<stdio.h> # include <string.h> # include<stdlib.h> void function(char *); int j; struct test { char member1[20]; char member2[20]; }; struct test *tr; int main() { char str[100]; printf("enier the string"); scanf("%s",&str); tr=(struct test *)malloc( sizeof (struct test)*100); function(str); free(tr); return 0; } void function(char * str) { long int i,index,k; char str1[100],*ch,*pch; j=0; ch=strtok(str,":"); if (ch==NULL)exit(0); while(ch!=NULL) { pch=strstr(ch,";"); i=pch-ch; strncpy(tr[j].member1,ch,i); strcpy(tr[j].member2,pch+1); j++; ch=strtok(NULL,":"); }; for(k=0;k<j;k++) { printf("\n%s",tr[k].member1); printf("\n%s",tr[k].member2);} }
it gives desired output when string entered is nghg;jhjkh:kjhkh;mhjkh
but gives segmentatiopn error for hjghjgjg
it should exit from the program for such a string
Last edited by rati; Sep 27th, 2006 at 3:36 am. Reason: code written in incorrect way
The seg fault is becouse if you dont have ';' then pch is NULL. So seg fault will happen only if you dont have ';' in the input string. You need to change the logic
Run this
C Syntax (Toggle Plain Text)
#include<stdio.h> #include <string.h> #include<stdlib.h> void function(char *); int j; struct test { char member1[20]; char member2[20]; }; struct test *tr; int main() { char str[100], *ptr; printf("enter the string: "); /* scanf("%s",&str); */ fgets(str, sizeof(str), stdin); if((ptr = strchr(str, '\n')) != NULL) *ptr = '\0'; tr=(struct test *)malloc( sizeof (struct test)*100); function(str); free(tr); return 0; } void function(char * str) { long int i,index,k; char str1[100],*ch = NULL,*pch = NULL; j=0; tr->member1[0] = '\0'; tr->member2[0] = '\0'; ch=strtok(str, ":"); printf("*ch = %c\n", *ch); /* debug print */ if (ch==NULL) exit(0); while(ch!=NULL) { pch=strstr(ch,";"); if (pch == NULL) { ch=strtok(NULL,":"); continue; } printf("*pch = %c\n", *pch); /* debug print */ i=pch-ch; printf("i = %ld\n", i); strncpy(tr[j].member1,ch,i); strcpy(tr[j].member2,pch+1); j++; ch=strtok(NULL,":"); } for(k=0;k<j;k++) { printf("\n%s",tr[k].member1); printf("\n%s",tr[k].member2); } }
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- Source code for printf, scanf, cin, cout? (C)
- Scanf Equivalent (Java)
- Scanf problem (C)
- scanf - compiles but causes error (C++)
Other Threads in the C Forum
- Previous Thread: file compression prgogram
- Next Thread: deamon processes
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






