Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c:将字符串传递给函数失败_C_String - Fatal编程技术网

c:将字符串传递给函数失败

c:将字符串传递给函数失败,c,string,C,String,我不知道为什么我的程序在向函数传递字符串时崩溃。我很高兴它能起作用/ void add_person(node **head, char name[MAXDL], char surn[MAXDL]); int main() { int i; char nm[MAXDL], sn[MAXDL]; node **head = NULL; for (i = 0; i < 3; i++) { scanf("%s", nm); scanf("%s", sn); add_pe

我不知道为什么我的程序在向函数传递字符串时崩溃。我很高兴它能起作用/

void add_person(node **head, char name[MAXDL], char surn[MAXDL]);

int main()
{
int i;
char nm[MAXDL], sn[MAXDL];
node **head = NULL;
for (i = 0; i < 3; i++)
{
    scanf("%s", nm);
    scanf("%s", sn);
    add_person(*head, nm, sn); //IN THAT LINE THE PROBLEM OCCURS
}
//...

system("PAUSE");
return 0;
}


void add_person(node **head, char name[MAXDL], char surn[MAXDL])
{
//body
}
void add_person(节点**head,字符名[MAXDL],字符surn[MAXDL]);
int main()
{
int i;
字符nm[MAXDL],sn[MAXDL];
节点**头=空;
对于(i=0;i<3;i++)
{
扫描频率(“%s”,纳米);
扫描频率(“%s”,序号);
添加_person(*head,nm,sn);//在该行出现问题
}
//...
系统(“暂停”);
返回0;
}
void add_person(节点**head,字符名[MAXDL],字符surn[MAXDL])
{
//身体
}
该方案的目的是建立一个名单,并在其中增加三人。 我已经评论了这句话,其中有些地方不对。 调试器: “在ConsoleApplication2.exe中的0x00D91A54处引发异常:0xC0000005:访问冲突读取位置0x00000000

如果有此异常的处理程序,则程序可以安全地继续。”


嗯,老实说,我不知道那里出了什么问题(

您将
指定为
,然后取消引用。这会导致崩溃。您只需传入
,而不是
*头
,它就不会崩溃。但是,您将传入
,我想这不是您想要做的


NULL
是地址0x00000000,这就是为什么它说您正在尝试读取0x00000000。您永远不想这样做。

尝试以下更改

void add_person(node **head, char name[MAXDL], char surn[MAXDL]);

int main()
{
int i;
char nm[MAXDL], sn[MAXDL];
node *head = NULL; // <-------------- **head -> *head
for (i = 0; i < 3; i++)
{
    scanf("%s", nm);
    scanf("%s", sn);
    add_person(&head, nm, sn); <----------- *head -> &head
}
//...

system("PAUSE");
return 0;
}


void add_person(node **head, char name[MAXDL], char surn[MAXDL])
{
// assuming that you will allocate node in this function like so
*head = malloc(sizeof(node)); <------------- allocation here?
}
void add_person(节点**head,字符名[MAXDL],字符surn[MAXDL]);
int main()
{
int i;
字符nm[MAXDL],sn[MAXDL];
节点*head=NULL;//*head
对于(i=0;i<3;i++)
{
扫描频率(“%s”,纳米);
扫描频率(“%s”,序号);
添加_人(&head,nm,sn);&head
}
//...
系统(“暂停”);
返回0;
}
void add_person(节点**head,字符名[MAXDL],字符surn[MAXDL])
{
//假设您将像这样在这个函数中分配节点

*head=malloc(sizeof(node));如果看不到添加人的主体,请尝试:

  • node**head=NULL;
    更改为
    node*head=NULL;
  • add_person(*head,nm,sn);
    更改为
    add_person(&head,nm,sn);

  • 请提供一个(集中于“最小值”)。并且“我的程序崩溃”不是特定的问题描述。请使用调试器。
    glowa
    包含NULL,您正在尝试使用
    *glowa
    。崩溃与字符串无关。调试器:ConsoleApplication2.exe中0x00D91A54处引发异常:0xC0000005:访问冲突读取位置0x00000000。“看起来不像调试器消息。如果您不知道调试器是什么,请搜索术语。void add_person(节点**头,字符名[maxl],字符surn[maxl])否。因为需要传递函数
    节点**
    &head
    节点**