gpt4 book ai didi

C 编译警告 : passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:47 26 4
gpt4 key购买 nike

/*Implementation of Binary Tree*/

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

struct bin_tree
{
int INFO;
struct node *LEFT, *RIGHT;
};

typedef struct bin_tree node;

node *insert(node *,int); /*Function prototype for insering a new node*/
void display(node *); /*Function prototype fpr displaying the tree nodes*/

int count=1; /*counter for ascertaining left or right position for the new node*/

int main()
{
struct node *root = NULL;
int element, choice;

clear();

/*Displaying a menu of choices*/
while(1)
{
clear();
printf("\n Select an option\n");
printf("\n1 - Insert");
printf("\n2 - Display");
printf("\n3 - Exit");

printf("\n\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
{
printf("\n\n Enter the node value: ");
scanf("%d", &element);
root = insert(root, element); /*calling the insert function for inserting a new element into the tree*/
getch();
break;
}

case 2:
{
display(root); /*calling the display function for printing the node values*/
getch();
break;
}

case 3:
{
exit(1);
break;
}

default:
{
printf("\n incorrect choice.Please try again.");
getch();
break;
}
}
}
}

node *insert(node *r, int n)
{
if(r==NULL)
{
r=(node*)malloc(sizeof(node));
r->LEFT = r->RIGHT = NULL;
r->INFO = n;
count = count+1;
}
else
{
if(count%2==0)
r->LEFT = insert(r->LEFT,n);
else
r->RIGHT = insert(r->RIGHT,n);
}
return(r);
}

void display(node *r)
{
if(r->LEFT!=NULL)
display(r->LEFT);
printf("%d\n",r->INFO);
if(r->RIGHT!=NULL)
display(r->RIGHT);
}

gcc -Wall -c "BinaryTree.c" (in directory: /home/dere/IGNOUPROGRAMS/C)

BinaryTree.c: In function ‘main’:
BinaryTree.c:46:5: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:16:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:46:10: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:53:5: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:17:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c: In function ‘insert’:
BinaryTree.c:86:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:86:11: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:88:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:88:12: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c: In function ‘display’:
BinaryTree.c:96:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:99:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
Compilation finished successfully.

对于此错误的任何帮助,我们将不胜感激。

最佳答案

main() 函数中将 root 声明为 node * 本身的类型。无需声明为 struct node *

node *root = NULL;

因为 rootstruct bin_treetypedef

警告是由于声明不匹配。由于 insert() 函数定义为 node *insert(node *,int); 但根变量定义为 struct node * 及其作为参数传递给 insert() 函数。

关于C 编译警告 : passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com