strpbrk

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

strpbrk

 
0
  #1
Apr 10th, 2007
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. }
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strpbrk

 
0
  #2
Apr 10th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: strpbrk

 
0
  #3
Apr 10th, 2007
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
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strpbrk

 
0
  #4
Apr 10th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC