此代码段中双指针和三指针的意义 #包括 #包括 无效添加(字符**p); 作废打印(字符**p); int-cnt=0; main() { int选项; char**p=NULL; 而(1) { printf(“----菜单------\n”); printf(“1>输入\n 2>打印\n 3>退出\n”); printf(“输入您的选择\n”); scanf(“%d”,&option);getchar(); 开关(选件) { 案例1:添加(p); 打破 案例2:打印(p); 打破 案例3:返回; 默认值:printf(“无效选项”); } } } 无效添加(字符**p) { int i; p=(char**)realloc(p,(cnt+1)*sizeof(char*); if(p==NULL) { printf(“错误:内存不可用\n”); 返回; } p[cnt]=NULL; p[cnt]=(char*)realloc(p[cnt],20*sizeof(char)); 输入(“输入名称”); 获取(p[cnt]); cnt++; printf(“cnt=%d\n”,cnt); } 无效打印(字符**p) { int i; 对于(i=0;i

此代码段中双指针和三指针的意义 #包括 #包括 无效添加(字符**p); 作废打印(字符**p); int-cnt=0; main() { int选项; char**p=NULL; 而(1) { printf(“----菜单------\n”); printf(“1>输入\n 2>打印\n 3>退出\n”); printf(“输入您的选择\n”); scanf(“%d”,&option);getchar(); 开关(选件) { 案例1:添加(p); 打破 案例2:打印(p); 打破 案例3:返回; 默认值:printf(“无效选项”); } } } 无效添加(字符**p) { int i; p=(char**)realloc(p,(cnt+1)*sizeof(char*); if(p==NULL) { printf(“错误:内存不可用\n”); 返回; } p[cnt]=NULL; p[cnt]=(char*)realloc(p[cnt],20*sizeof(char)); 输入(“输入名称”); 获取(p[cnt]); cnt++; printf(“cnt=%d\n”,cnt); } 无效打印(字符**p) { int i; 对于(i=0;i,c,dynamic-memory-allocation,realloc,C,Dynamic Memory Allocation,Realloc,按引用传递。您需要传递指针的地址,以确保函数add()中的更改反映在main()中。避免使用get() 在这种情况下 #include<stdio.h> #include<stdlib.h> void add(char **p); void print(char **p); int cnt=0; main() { int option; char **p=NULL; while(1) { printf("------MENU-----\n"); print

按引用传递。您需要传递指针的地址,以确保函数
add()
中的更改反映在
main()
中。避免使用
get()

在这种情况下

#include<stdio.h>
#include<stdlib.h>
void add(char **p);
void print(char **p);
int cnt=0;
main()
{
  int option;
  char **p=NULL;
while(1)
{
  printf("------MENU-----\n");
  printf("1>input\n 2>print\n3>exit\n");
  printf("enter ur choice\n");
  scanf("%d",&option);getchar();
 switch(option)
 {
    case 1: add(p);
            break;
    case 2: print(p);
            break;
   case 3: return;
   default: printf("Invalid option\n");

 }
}
}
void add(char **p)
{
  int i;
  p=(char**)realloc(p,(cnt+1)*sizeof(char*));
  if(p==NULL)
  {
        printf("Error: memory not available\n");
        return;

  }
  p[cnt]=NULL;
  p[cnt]=(char*)realloc(p[cnt],20*sizeof(char));

  puts("enter a name");
  gets(p[cnt]);
  cnt++;
  printf("cnt=%d\n",cnt);
}
void print(char **p)
{
  int i;
  for(i=0;i<cnt;i++)
  printf("p[%d]=%s\n",i,p[i]);
}
因此,您的
add()
函数定义应该更改以处理此问题。 另一种方法是add函数进行所需的分配,并将该地址返回给指针

add(&p);
检查以下代码:

char **add();
所以你的电话应该是:

char  **add(char **p)
{
  int i;
  p=(char**)realloc(p,(cnt+1)*sizeof(char*));
  if(p==NULL)
  {
        printf("Error: memory not available\n");
        return;

  }
  p[cnt]=NULL;
  p[cnt]=(char*)realloc(p[cnt],20*sizeof(char));

  scanf("%s",p[cnt]);
  cnt++;
  printf("cnt=%d\n",cnt);
  return p;
}

p
main
中按值移交给
add
add
随后修改本地副本,但不修改原始
p

除了糟糕的格式和一切,您还需要将指针指向
main
p
添加到
add

p = add(p);

大体上,您所要做的就是分配
p=NULL
,然后在
print
中使用此空指针,这会导致segfault。请注意
add(p)
相当于
add(NULL)
,并且不会更改
p

您是想用
add(&p)
传递地址吗?如果是这样,您需要稍微修改
*

的编号,在add函数中创建一个2D指针p的副本,并且更新此指针不会反映在主函数中。 这个问题的一个快速解决方案是从add函数返回2D指针“p”

...

  case 1: add(&p);
...

void add(char ***p)
{
  int i;
  *p = realloc(*p,(cnt+1)*sizeof(char*));
  if(*p==NULL)
  {
        printf("Error: memory not available\n");
        return;

  }
  (*p)[cnt]=NULL;
  (*p)[cnt]=realloc((*p)[cnt],20*sizeof(char));

  puts("enter a name");
  gets((*p)[cnt]);
  cnt++;
  printf("cnt=%d\n",cnt);
}

除此之外,我认为没有人提到过重新分配失败的
realloc
陷阱

char** print(char **p)
{
    ....
    ....
    return p;
}
分配新内存失败:然后会发生什么?是的,您将得到返回的
NULL
:但是分配的
p
指向的内存没有得到
释放的
'd。即时内存泄漏

您必须像这样调用
realloc

p=(char**)realloc(p,(cnt+1)*sizeof(char*));

另一个原因是没有使用
realloc
。实际上,你并没有过度使用缓冲区来复制你自己。

除了下面的答案中已经提到的变量
p
在函数
add
中是局部变量之外,我非常确定你必须初始化
p=malloc(…)
开始,因为函数
realloc
需要(作为输入参数)指向先前分配了
malloc
realloc
的内存段的指针。一些提示:使用调试器,一致地缩进代码,避免
get
,不要强制转换
malloc
/
realloc
的返回值,
sizeof(char)
始终为1。@barakmanos:My C标准说“如果
ptr
是一个空指针,那么
realloc
函数的行为就像
malloc
函数一样。”但是,如果
print
没有事先调用
add
,指针是0。@barakmanos-
realloc
可以做
malloc
free
可以做的任何事情(包括初始分配)。这是一个从不使用
realloc
的论点,而不是从不使用
malloc
free
!设计
realloc
的人忘记了“做一件事,把它做好”作为设计原则。@AAT
realloc()
可以“复制”“在不更改物理地址的情况下,将内存内容转移到另一个新的逻辑位置,因为它知道源将是空闲的。因此,不需要
memcpy()
发生。如何
malloc()
free()
执行这种有效的重新分配?”?
char* pTemp = realloc(p, cnt + 1);
if(pTemp != NULL)
{
    p = pTemp;             // Now safe to update p
}
else
{
    // Error handling as required, possibly including freeing p
}