943,511 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1503
  • C RSS
Apr 10th, 2007
0

strpbrk

Expand Post »
hey guyz, i'm trying to search each ar for matching characters in the buff, but I seem to be getting a buffer overflow. Can anyone give me any pointers, any help would be appreciated, this is my code:
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. main(int ab, char** ar)
  7. {
  8. char buff[800];
  9. int n_chars;
  10. int i;
  11. char *pch[300];
  12.  
  13. printf("\nplease enter text => ");
  14. fgets(buff, sizeof(buff), stdin);
  15. for (i=0;i<ab;i++)
  16. {
  17.  
  18. pch[i] = strpbrk (ar[i],buff);
  19. printf ("%c ",pch[i]);
  20. pch[i] = strpbrk (pch[i],buff);
  21. }
  22.  
  23. }
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 10th, 2007
0

Re: strpbrk

not buffer overflow -- you are not checking that strpbrk() returns a NULL pointer, which it can if the string does not contains the characters.

pch is a 2d array of pointers, so if strpbrk() returns NULL that printf will fail. The format specification in printf() is incorrect too -- should be "%s", not "%c" because pch[i] is a pointer to a string, not a single character.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Apr 10th, 2007
0

Re: strpbrk

still getting the same thing...
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. main(int ab, char** ar)
  7. {
  8. char buff[800];
  9. int n_chars;
  10. int i;
  11. char *pch[300];
  12.  
  13. printf("\nplease enter text => ");
  14. fgets(buff, sizeof(buff), stdin);
  15. while (pch[i] != NULL)
  16. { pch[i] = strpbrk (ar[i], buff);
  17. printf ("%s " , pch[i]);
  18. pch[i] = strpbrk (pch[i]+1,buff);
  19. } }
Last edited by rowly; Apr 10th, 2007 at 2:18 pm. Reason: syntax error
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 10th, 2007
0

Re: strpbrk

you have not changed anything. you have to check for NULL AFTER calling strpbrk(), not before. And you also created an infinite loop because the value of i counter is never changed.

>>pch[i]+1
that is wrong -- why are you adding 1 to a pointer value????
Last edited by Ancient Dragon; Apr 10th, 2007 at 3:35 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: read a array of type struct
Next Thread in C Forum Timeline: How to declare struct in the class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC