Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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 - Fatal编程技术网

C 给定代码中的逻辑错误在哪里?

C 给定代码中的逻辑错误在哪里?,c,C,我正在尝试将新节点添加到链接列表中。Online tutorials()使用指向指针的指针,我无法理解它是如何工作的。所以我决定自己编写代码,但push()函数有问题。这是代码 #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node* next; }; void print_list(struct node *head_ptr) { while(head

我正在尝试将新节点添加到链接列表中。Online tutorials()使用指向指针的指针,我无法理解它是如何工作的。所以我决定自己编写代码,但push()函数有问题。这是代码

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node* next;
};

void print_list(struct node *head_ptr)
{
    while(head_ptr!=NULL)
    {
        printf("%d->",head_ptr->data);
        head_ptr = head_ptr->next;
    }
}

void push(struct node *head_ptr, int data_val)
{
    struct node *new_node = (struct node*)malloc(sizeof(struct node));
    new_node -> data = data_val;

    new_node->next = head_ptr;
    head_ptr = new_node;
}

int main()
{
    struct node *head = (struct node*)malloc(sizeof(struct node));
    struct node *second = (struct node*)malloc(sizeof(struct node));
    struct node *third = (struct node*)malloc(sizeof(struct node));

    head -> data = 1;
    head -> next = second;
    second -> data = 2;
    second -> next = third;
    third -> data = 3;
    third -> next = NULL;

    print_list(head);
    push(head,0);
    printf("\n");
    print_list(head);

    return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
无效打印列表(结构节点*head\u ptr)
{
while(head_ptr!=NULL)
{
printf(“%d->”,head\u ptr->data);
head_ptr=head_ptr->next;
}
}
无效推送(结构节点*head\u ptr,int data\u val)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建节点->数据=数据值;
新建节点->下一步=head\u ptr;
head_ptr=新_节点;
}
int main()
{
结构节点*头=(结构节点*)malloc(sizeof(结构节点));
结构节点*second=(结构节点*)malloc(sizeof(结构节点));
结构节点*third=(结构节点*)malloc(sizeof(结构节点));
头部->数据=1;
头->下一个=秒;
第二->数据=2;
第二->下一个=第三个;
第三->数据=3;
第三->下一步=空;
打印列表(标题);
推(头,0);
printf(“\n”);
打印列表(标题);
返回0;
}
它编译无误。但是,值为0的预期节点不会插入到链接列表的开头

电流输出 1->2->3-> 预期产量 0->1->2->3->


请帮助我在不使用push()函数中指向指针的指针的情况下获得所需的输出。此外,共享指向源的链接,该链接对指向指针的指针有很好的解释,并且与链接列表相关

你做得不对

问题是,当您在push中传递
头时,它的副本将传递给
push
。它们的内容是相同的。现在你在做什么

创建新节点。然后对变量的副本进行更改。然后函数结束,变量的生存期结束,对分配内存的访问也结束(造成内存泄漏)。而
main()
中的原始
head
仍然相同。这就是它打印
1-2-3
的原因

解决方案-1
head=push(head,0)

解决方案2 就这样说吧

push(&head,0);
解决方案2的解释 现在,将
&head
传递给函数意味着将其地址传递给函数
push()
。现在在
push()。现在,通过取消对它的引用,您基本上就是
main()
head
,您对
*head
所做的任何更改都将保留,因为您正在更改为实际变量,其地址已传递

解决方案1的解释 这里我们没有传递地址。而是传递
main()
head
变量的内容。现在您分配内存并得到它的地址。您可以对其进行更改。然后在
下一个
中输入
头部的地址
,然后将
头部
指向新节点。你可能会问,它会改变原始头部吗?不。这就是为什么我们返回它并将其存储在
main()
中的原始bal
head

注:
  • 动态分配的内存具有超出函数范围的生存期。这就是为什么你可以返回它的地址,不会有任何问题

  • 您不需要强制转换
    malloc
    的返回类型。这是不必要的

    struct node*new_node=malloc(sizeof(struct node))

更清楚

struct node *new_node = malloc(sizeof *new_node));
  • 还要检查
    malloc
    的返回值

  • 使用完动态分配的内存后,请释放该内存

给定代码中的逻辑错误在哪里

在C语言中,函数参数总是“按值调用”

函数可以更改函数参数,但这对调用函数中的参数没有影响。例如:

void x(int a)
{
    a = a + 10;
    printf("%d\n", a);
}

void y(void)
{
    int b = 9;
    x(b);   /* Line 1 */
    x(123); /* Line 2 */
}
线
a=a+10
将产生打印
19
133
的效果,而不是打印
9
123

但是,它将具有将“第1行”中的
b
更改为
19
的效果;对于“第2行”,应该清楚的是,这样的效果甚至是不可能的

在代码中,
head\u ptr=…
行具有相同的效果

请帮助我获得所需的输出

由于C语言不提供真正的“按引用调用”参数,您可以使用指向变量的指针作为“按值调用”参数来模拟“按引用调用”参数:

void x(int *a)
{
    (*a) = (*a) + 10;
    printf("%d\n", *a);
}

...

x(&b);
或者,在您的示例中:

/* Note the two "**"s in this line: */
void push(struct node **head_ptr, int data_val)
{
    ...
    *head_ptr = new_node; /* Note the "*" */
}

...

push(&head,0);

当然,您只能以这种方式将变量传递给函数。

C中的所有内容都是按值传递的。甚至是指针 所以 我有两个指针

void swap(char* first, char* second)
{
  //take temp var and swap
}

main(){
char *first, *second;
first = 0x1;
second = 0x2;

printf("%x, %x", first, second);
swap(first, second);
printf("%x, %x", first, second);

}
此代码不会交换值,因为在swap()中,堆栈上本地创建的值正在交换。 要意识到指针的行为就像另一个普通变量一样。 是的,但是如果取消引用它们,则可以交换swap()中指针指向的值

问题是:您传递的头是按值传递的,push()函数执行其任务,但对头的赋值是在本地完成的。

***已解决***
***SOLVED***

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node* next;
};

void print_list(struct node *head_ptr)
{
    while(head_ptr!=NULL)
    {
        printf("%d->",head_ptr->data);
        head_ptr = head_ptr->next;
    }
}

void push(struct node *head_ptr, int data_val, ***struct node *new_node***)
{

    new_node -> data = data_val;

    new_node->next = head_ptr;
    head_ptr = new_node;
}

int main()
{
    struct node *head = (struct node*)malloc(sizeof(struct node));
    struct node *second = (struct node*)malloc(sizeof(struct node));
    struct node *third = (struct node*)malloc(sizeof(struct node));
    ***struct node *new_node = (struct node*)malloc(sizeof(struct node));***

    head -> data = 1;
    head -> next = second;
    second -> data = 2;
    second -> next = third;
    third -> data = 3;
    third -> next = NULL;

    print_list(head);
    push(head,0,new_node);
    printf("\n");
    print_list(***new_node***);

    return 0;
}
#包括 #包括 结构节点{ int数据; 结构节点*下一步; }; 无效打印列表(结构节点*head\u ptr) { while(head_ptr!=NULL) { printf(“%d->”,head\u ptr->data); head_ptr=head_ptr->next; } } 无效推送(结构节点*头节点,整数数据值,***结构节点*新节点***) { 新建节点->数据=数据值; 新建节点->下一步=head\u ptr; head_ptr=新_节点; } int main() { 结构节点*头=(结构节点*)malloc(sizeof(结构节点)); 结构节点*second=(结构节点*)malloc(sizeof(结构节点)); 结构节点*third=(结构节点*)malloc(sizeof(结构节点)); ***结构节点*新节点=(结构
void swap(char* first, char* second)
{
  //take temp var and swap
}

main(){
char *first, *second;
first = 0x1;
second = 0x2;

printf("%x, %x", first, second);
swap(first, second);
printf("%x, %x", first, second);

}
***SOLVED***

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node* next;
};

void print_list(struct node *head_ptr)
{
    while(head_ptr!=NULL)
    {
        printf("%d->",head_ptr->data);
        head_ptr = head_ptr->next;
    }
}

void push(struct node *head_ptr, int data_val, ***struct node *new_node***)
{

    new_node -> data = data_val;

    new_node->next = head_ptr;
    head_ptr = new_node;
}

int main()
{
    struct node *head = (struct node*)malloc(sizeof(struct node));
    struct node *second = (struct node*)malloc(sizeof(struct node));
    struct node *third = (struct node*)malloc(sizeof(struct node));
    ***struct node *new_node = (struct node*)malloc(sizeof(struct node));***

    head -> data = 1;
    head -> next = second;
    second -> data = 2;
    second -> next = third;
    third -> data = 3;
    third -> next = NULL;

    print_list(head);
    push(head,0,new_node);
    printf("\n");
    print_list(***new_node***);

    return 0;
}