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

问题描述

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...