You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

链表求和代码编译错误求助:invalid operands to binary +问题排查

链表求和代码编译错误解析

编译错误信息

PE12.c: In function ‘sumOfLinkedList’:
PE12.c:69:28: error: invalid operands to binary + (have ‘Link’ and ‘struct Link’)
        sum = *current_link + *current_link->next;
                            ^

问题代码

#include <stdio.h>  /* for printf */
#include <stdlib.h> /* for malloc, free, NULL */
#include "link.h"

#define ARR_SZ 6

/* function for step 1 */
Link* getNewLink(int value, Link* next); 

/* function for step 4 */
void printLinkedList(Link* head); 

/* function for step 5 */
int sumOfLinkedList(Link* head);  

int main() {
    Link* head = NULL;
    int array[ARR_SZ] = {7, 1, -4, 16, 0, 3};
    int arrLen = ARR_SZ;
    int i = 0;


    /* Step 1: add the value -2 to the linked list */
    getNewLink(-2, head);
    /* TODO - Your code for Step 1 goes here 
     * You must also implement getNewLink() */
     
    /* Step 2: print the value at head */
    printf("%p", head);
    /* TODO - Your code for Step 2 goes here  */
    /* Step 3: add the 6 values from array to the linked list */
    for(i=0; i <= array[i] && i < arrLen; ++i) {
        getNewLink(i, head);
    } 
    /* TODO - Your code for Step 3 goes here */
    /* Step 4 */
    printLinkedList(head);

    /* Step 5 */
    printf("The sum is: %d\n", sumOfLinkedList(head));

    return EXIT_SUCCESS;
}

Link* getNewLink(int value, Link* next) {

    Link* newNode = (Link*) malloc(sizeof(Link));
    newNode->value = value;
    newNode->next = NULL;
    
    return newNode;
}

void printLinkedList(Link* head) {
    
    while (head != NULL) {
        printf("%d ", head->value);
        head = head->next;
    }
}

int sumOfLinkedList(Link* head) {

    int sum;
    
    Link *current_link = head;
       while ( current_link != NULL) {
        printf("%d ", current_link->value);
       sum = *current_link + *current_link->next;
    }
    
    return sum;
}

错误原因与修正方案

编译错误核心原因

sumOfLinkedList函数里的sum = *current_link + *current_link->next;存在两个致命问题:

  • *current_link是整个Link结构体实例,*current_link->next也是结构体对象,C语言不允许直接对结构体执行加法运算,这是编译报错的直接原因。
  • 求和逻辑错误:链表求和需要累加每个节点的value成员,而非将两个结构体相加。

其他隐性问题与修正

  1. 链表节点未正确链接:getNewLink返回的新节点没有被赋值给链表头或现有节点的next指针,导致所有新节点都是游离状态,head始终为NULL。比如Step1需要改为:

    head = getNewLink(-2, head);
    
  2. sum未初始化:局部变量sum未初始化时是随机值,必须初始化为0才能保证求和正确。

  3. 循环死循环:sumOfLinkedList的while循环没有移动current_link指针,需要添加current_link = current_link->next;来遍历链表。

  4. main函数逻辑错误:

    • Step3的for循环条件i <= array[i]完全错误,应改为i < arrLen;且调用getNewLink时要传入array[i]而非i,才能添加数组元素。
    • Step2中printf("%p", head)打印的是指针地址,要打印节点值需改为printf("%d", head->value)(前提是head不为空)。

修正后的sumOfLinkedList函数

int sumOfLinkedList(Link* head) {
    int sum = 0;
    Link *current_link = head;
    while (current_link != NULL) {
        sum += current_link->value;
        current_link = current_link->next;
    }
    return sum;
}

内容的提问来源于stack exchange,提问作者Tyler Reed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 00:55:39