| | |
strpbrk
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> main(int ab, char** ar) { char buff[800]; int n_chars; int i; char *pch[300]; printf("\nplease enter text => "); fgets(buff, sizeof(buff), stdin); for (i=0;i<ab;i++) { pch[i] = strpbrk (ar[i],buff); printf ("%c ",pch[i]); pch[i] = strpbrk (pch[i],buff); } }
DarkCoder+
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.
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.
still getting the same thing...
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> main(int ab, char** ar) { char buff[800]; int n_chars; int i; char *pch[300]; printf("\nplease enter text => "); fgets(buff, sizeof(buff), stdin); while (pch[i] != NULL) { pch[i] = strpbrk (ar[i], buff); printf ("%s " , pch[i]); pch[i] = strpbrk (pch[i]+1,buff); } }
Last edited by rowly; Apr 10th, 2007 at 2:18 pm. Reason: syntax error
DarkCoder+
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????
>>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.
![]() |
Similar Threads
- removing double quotes using strpbrk() (C)
- emulating seach engine using C (C)
- Scanning an inputted array of words, and separating each word into a new array? (C)
- memory allocation ptr to array? how? (C)
- how can i find out some char from string? (C++)
Other Threads in the C Forum
- Previous Thread: read a array of type struct
- Next Thread: How to declare struct in the class
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






