【问题标题】:Pointer to a Pointer in C指向 C 中的指针的指针
【发布时间】:2015-05-13 12:44:36
【问题描述】:

我正在编写下面的 Pro*C 程序,但出现核心转储错误,有人可以帮我解决我做错了什么吗?我是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
        }
    }
}

输出:

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

谢谢大家。我已根据建议修改了该功能,但仍然出现核心转储错误 添加修改功能:

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

【问题讨论】:

  • malloc 中使用strlen
  • 你也应该写sizeof(*inputpath1)而不是sizeof(inputpath1)。其他人都一样

标签: c pointers char


【解决方案1】:

变量holder 是一个指向char 的指针,您将strrchr 的结果分配给它,该函数返回指向char 的指针。

这将使holder 不再指向数组条目,因此当您执行holder++ 时,它会完全指向其他位置,这会在您取消引用holder 时导致未定义的行为。然后您继续将holder 传递给strncmp,这又会给您带来未定义的行为。

holder 变量也不是字符,因此您不应将其初始化为字符。如果你想初始化它,你应该初始化它指向NULL

上面提到的事情应该会让编译器对你发出警告。


此外,正如 Cool Guy 的评论中所指出的,如果您想获取字符串的长度,您应该使用 strlen。当您使用 sizeof 时,您会得到 指针 的大小,而不是它所指向的大小。

【讨论】:

    【解决方案2】:
    strings[0]=(char*)malloc(strlen(inputpath1));
    strings[1]=(char*)malloc(strlen(inputpath2));
    

    ...

    char *holder=NULL;
    

    ...

    fclose(inFile);
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2019-09-10
      • 1970-01-01
      • 2011-05-19
      • 2021-07-14
      相关资源
      最近更新 更多