Aravind
Aravind

Reputation: 149

Pointer to a Pointer in C

I am writing a below Pro*C program and I am getting core dump error, could someone please help me what I am doing wrong. I am new to C

int main(int argc,char *argv[])
{
  char inputpath1[300]="";
  char inputpath2[300]="";
  gets(inputpath1);
  gets(inputpath2);
  ExtractCmpFiles(inputpath1,inputpath2); //contains fullpath with file
return 0;
}

void ExtractCmpFiles(char *inputpath1,char *inputpath2)
{
  int i=0;
  char *strings[2];
  strings[0]=malloc(sizeof(inputpath1)+1);
  strcpy(strings[0],inputpath1);
  strings[1]=malloc(sizeof(inputpath2)+1);
  strcpy(strings[1],inputpath2);
  printf("Your files are:\n%s\n%s\n",strings[0],strings[1]);//i am getting output here
  char **holder='\0';
  char ioarea[4096];
  for(i=0;i<2;i++)
    {
      inFile=fopen(strings[i],"r");
      if(!inFile)
        {
          fprintf(stderr,": Open failure for output file:");
          fprintf(stderr,strings[i]);
          fprintf(stderr,"\n");
        }
      holder=&strings[i];
      holder=strrchr(strings[i],'/');  //checking the address of '/' to get only filename
      holder++; //incrementing pointer
      printf("your holder is: %s\n",*holder);
      if(strncmp(holder,"bills",5)==0) //checking value from the pointer address is "bills" or not
        {
          //do as you wish
        }
    }
}

output:

/your/path/bills.cmp
/your/path/bill.cmp

Thank you all. I have modified the function as per suggestions, but still I am getting core dump error Adding modified function:

 void ExtractCmpFiles(char *inputpath1,char *inputpath2)
{
  char *strings[2];
  strings[0]=(char*)malloc(strlen(*inputpath1)+1);
  strcpy(strings[0],inputpath1);
  strings[1]=(char*)malloc(strlen(*inputpath2)+1);
  strcpy(strings[1],inputpath2);

  printf("Your files are:\n%s\n%s\n",strings[0],strings[1]);

  char *holder=NULL;
  char ioarea[4096];

  int i=0;

  for(i=0;i<2;i++)
    {
      inFile=fopen(strings[i],"r");
      if(!inFile)
        {
          fprintf(stderr,": Open failure for output file:");
          fprintf(stderr,strings[i]);
          fprintf(stderr,"\n");
        }
      holder=strings[i];
      holder=strrchr(strings[i],'/'); 
      printf("i=%d --- %s\n",i,strings[i]);   //when i=0 it is giving output then it is giving coredump
      printf("your holder1 is: %s\n",holder);
      holder++;
      printf("your holder2 is: %s\n",holder);
      if(strncmp(holder,"custbills",9)==0)
        {
          //do something
        }
      else
        {
          //do something
        }

  fclose(inFile);
}

Upvotes: 2

Views: 62

Answers (2)

fandyushin
fandyushin

Reputation: 2432

strings[0]=(char*)malloc(strlen(inputpath1));
strings[1]=(char*)malloc(strlen(inputpath2));

...

char *holder=NULL;

...

fclose(inFile);

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

The variable holder is a pointer to a pointer to char, and you assign to it the result of strrchr, a function that returns a pointer to char.

That will make holder no longer point to the array entry, so when you do holder++ it points somewhere else completely which leads to undefined behavior when you dereference holder. You then continue to pass holder to strncmp which again will give you undefined behavior.

The holder variable is not a character either, so you should not initialize it as one. If you want to initialize it you should initialize it to point to NULL.

The things mentioned above should make the compiler shout warnings at you.


Also, as noted in a comment by Cool Guy, if you want to get the length of a string you should use strlen. When you use sizeof you get the size of the pointer and not what it points to.

Upvotes: 4

Related Questions