指针之间的类型不兼容错误

如何解决指针之间的类型不兼容错误

void remove_element(struct Node *list)
{   
    struct Node *temp = list;
    printf("Enter the element value you want to remove");
    int value;
    scanf("%d",&value);
    if(temp->data == value){ //first node is to be deleted
        *list = temp->next; // error here
        free(temp);
    }
}

错误:从“结构节点*”类型分配给“结构节点”类型时,类型不兼容 尽管已成功编译

 struct Node *temp = list; 

此行类似,但未显示错误。

解决方法

struct Node *temp = list;

相同
struct Node *temp;
temp = list;

因此将错误行更改为

list = temp->next;

请注意,*的含义因上下文而略有不同。在声明中,它表示您要声明一个指针而不是一个常规变量。在表达式中使用时,它意味着您要取消引用指针。在声明期间无法取消引用它,这不会引起任何问题,因为无论如何这都是不确定的行为。

,

首先出现错误的原因是...

*list = temp->next; //not *list it should be list

这样做可以编译您的代码,但是我认为您会得到意想不到的结果。因为:

list=temp->next // This will make the pointer to constantly point to the head

但是我认为您正在尝试进行线性搜索。 因此,您还需要将上述行更改为

temp = temp->next;

使代码按预期工作。

,

因为参数list的声明像

struct Node *list

然后在此语句中使用表达式*list

*list = temp->next;

具有类型struct Node,而右侧操作数具有类型struct Node *

你必须写

list = temp->next;

但是无论如何要注意,当传递的列表为空时,该函数可以调用未定义的行为。并且应该在节点上搜索目标值,而不是仅检查根节点是否包含目标值。

最糟糕的是,该函数甚至不更改指向头节点的指针,因为该指针是通过值传递给该函数的。所以这句话

    list = temp->next; // error here

将原始指针的副本更改为根节点,而不是在main中声明的原始指针本身。

该功能至少应定义为

int remove_element( struct Node **list )
{   
    printf( "Enter the element value you want to remove: " );
    int value;
    
    int success = scanf( "%d",&value ) == 1;

    if ( success )
    {
        while ( *list != NULL && ( *list )->data != value )
        {
            list = &( *list )->next;
        }

        success = *list != NULL;

        if ( success )
        {
            struct Node *tmp = *list;
            *list = ( *list )->next;
            free( tmp );
        }
    }

    return success;
}

如果主要是您有声明

struct Node *list = NULL;
//...

然后必须像这样调用函数

remove_element( &list );

或类似的

if ( remove_element( &list ) )
{
    puts( "A node was removed." );
}

如果函数仅做一件事,那就更好:删除节点。对于必须删除的值的提示应放在函数外部。在这种情况下,可以通过以下方式定义功能

int remove_element( struct Node **list,int value )
{   
    while ( *list != NULL && ( *list )->data != value )
    {
        list = &( *list )->next;
    }

    int success = *list != NULL;

    if ( success )
    {
        struct Node *tmp = *list;
        *list = ( *list )->next;
        free( tmp );
    }

    return success;
}

这是一个演示程序。

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

struct Node
{
    int data;
    struct Node *next;
};
    
    
int push_front( struct Node **list,int data )
{
    struct Node *temp = malloc( sizeof( struct Node ) );
    int success = temp != NULL;

    if ( success )
    {       
        temp->data = data;
        temp->next  = *list;
        *list = temp;
        
    }

    return success;
}

void clear( struct Node **list )
{
    while ( *list != NULL )
    {
        struct Node *temp = *list;
        *list = ( *list )->next;
        free( temp );
    }
}

void display( const struct Node *list )
{
    for ( const struct Node *current = list; current != NULL; current = current->next ) 
    {
        printf( "%d -> ",current->data );
    }
    
    puts( "null" );
}

int remove_element( struct Node **list,int value )
{   
    while ( *list != NULL && ( *list )->data != value )
    {
        list = &( *list )->next;
    }

    int success = *list != NULL;

    if ( success )
    {
        struct Node *tmp = *list;
        *list = ( *list )->next;
        free( tmp );
    }

    return success;
}

int main(void) 
{
    struct Node *list = NULL;
    
    const int N = 10;
    
    for ( int i = N; i != 0; i-- )
    {
        push_front( &list,i );
    }
    
    display( list );
    
    int value = 1;
    
    if ( remove_element( &list,value ) )
    {
        printf( "The element with the value %d was removed.\n",value );
        
    }

    display( list );

    value = 10;
    
    if ( remove_element( &list,value );
        
    }

    display( list );

    value = 5;
    
    if ( remove_element( &list,value );
        
    }

    display( list );

    clear( &list );
    
    display( list );

    return 0;
}

其输出为

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The element with the value 1 was removed.
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The element with the value 10 was removed.
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
The element with the value 5 was removed.
2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 9 -> null
null

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-