| | |
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
Views: 1158 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






