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:

#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);
  }
 
}

Recommended Answers

All 3 Replies

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 is a pointer to a string, not a single character.

still getting the same thing...

#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);
  } }

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+1
that is wrong -- why are you adding 1 to a pointer value????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.