Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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,因此,对于我的C代码,我正在编写一个二进制搜索树,它接受一个名为node的typedef结构,该结构具有char*name和char*phone。我在这段代码中做的是,我把它放在一个二叉搜索树中,然后按字母顺序输出列表,旁边是phoneNum。它工作,但我得到的错误 警告:指针和整数之间的比较 我该怎么做才能解决这个问题 #include <stdio.h> #include <stdlib.h> #include <limits.h> #include &l

因此,对于我的
C
代码,我正在编写一个二进制搜索树,它接受一个名为node的typedef结构,该结构具有char*name和char*phone。我在这段代码中做的是,我把它放在一个二叉搜索树中,然后按字母顺序输出列表,旁边是phoneNum。它工作,但我得到的错误

警告:指针和整数之间的比较

我该怎么做才能解决这个问题

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h> 
#include <string.h>
#include <malloc.h>
#define MAX 10

char* nameArray[] = {"George", "Zoey", "Hannah", "David", "Avery",
        "Justin", "Sasha", "Babara", "Steve", "Garnett", NULL};
char* number[] = {"610-11-1212", "508-123-1000", "617-555-1212",
        "818-919-8100", "710-777-1170", "310-333-1300", "510-555-1001","333-310-3201", "445-440-0044", "220-210-2210", NULL};
int flag;
typedef struct node
{

    char* name;
    char* phoneNum;
    struct node * left;
    struct node * right;
} node_t;


//int compareStrings(char* str1, char* str2){return (strcmp(str1, str2));}


void insert(node_t * tree, char* name, char* phoneNum);
void print_tree_inorder(node_t * current);
s
int main()
{
    int sum = 0;
    node_t * test_list = malloc(sizeof(node_t));

    /* set values explicitly, alternative would be calloc() */
    test_list->name =  "";
    test_list->phoneNum = "";
    test_list->left = NULL;
    test_list->right = NULL;


         for(int i = 0; i<MAX; i++){
            insert(test_list,nameArray[i],number[i] );
        }

    printf("\n In order\n");
    print_tree_inorder(test_list);
}

void insert(node_t * tree, char* name, char* phoneNum)
{  

    if (tree->name == 0)
    {
        /* insert on current (empty) position */
        tree->name = name;
        tree->phoneNum = phoneNum;
    }
    else
    {

        if ( strcmp(tree->name, name) < tree->name) //here
        {
            /* insert left */
            if (tree->left != NULL)
            {
                insert(tree->left, name, phoneNum);
            }
            else /* no left nodes*/
            {
                tree->left = malloc(sizeof(node_t));
                /* set values explicitly, alternative would be calloc() */
                tree->left->name = name;
                tree->left->phoneNum = phoneNum;
                tree->left->left = NULL;
                tree->left->right = NULL;
            }
        }
        else /*add node to right */
        {
            if ( strcmp(tree->name, name) >= tree->name) //here
            {
                /* insert right */
                if (tree->right != NULL)
                {
                    insert(tree->right, name, phoneNum);
                }
                else
                {
                    tree->right = malloc(sizeof(node_t));
                    /* set values explicitly, alternative would be calloc() */
                    tree->right->name = name;
                    tree->right->phoneNum = phoneNum;
                    tree->right->left = NULL;
                    tree->right->right = NULL;
                }
            }
        }
    }

}

void print_tree_inorder(node_t * current) {
    if (current == NULL) return;
    print_tree_inorder(current->left);
    printf(" %s  %s\n", current->name, current->phoneNum);
    print_tree_inorder(current->right);
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值10
char*nameArray[]={“乔治”、“佐伊”、“汉娜”、“大卫”、“艾弗里”,
“贾斯汀”、“萨沙”、“巴巴拉”、“史蒂夫”、“加内特”,空};
字符*编号[]={“610-11-1212”、“508-123-1000”、“617-555-1212”,
“818-919-8100”,“710-777-1170”,“310-333-1300”,“510-555-1001”,“333-310-3201”,“445-440-0044”,“220-210-2210”,空};
int标志;
类型定义结构节点
{
字符*名称;
char*phoneNum;
结构节点*左;
结构节点*右;
}节点t;
//int compareStrings(char*str1,char*str2){return(strcmp(str1,str2));}
void insert(node_t*tree,char*name,char*phoneNum);
按顺序作废打印树(节点*当前);
s
int main()
{
整数和=0;
node_t*test_list=malloc(sizeof(node_t));
/*显式设置值,可选值为calloc()*/
测试列表->名称=”;
测试列表->phoneNum=“”;
测试列表->左=空;
测试列表->右=空;
for(int i=0;iname==0)
{
/*在当前(空)位置插入*/
树->名称=名称;
树->电话号码=电话号码;
}
其他的
{
if(strcmp(tree->name,name)name)//这里
{
/*向左插入*/
如果(树->左!=空)
{
插入(树->左,名称,phoneNum);
}
else/*没有左节点*/
{
树->左=malloc(sizeof(node_t));
/*显式设置值,可选值为calloc()*/
树->左->名称=名称;
树->左->phoneNum=phoneNum;
树->左->左=空;
树->左->右=空;
}
}
else/*将节点添加到右侧*/
{
if(strcmp(tree->name,name)>=tree->name)//此处
{
/*插入右侧*/
如果(树->右!=空)
{
插入(树->右,名称,phoneNum);
}
其他的
{
树->右=malloc(sizeof(node_t));
/*显式设置值,可选值为calloc()*/
树->右->名称=名称;
树->右->phoneNum=phoneNum;
树->右->左=空;
树->右->右=空;
}
}
}
}
}
无效打印树顺序(节点*当前){
if(current==NULL)返回;
按顺序打印树(当前->左);
printf(“%s%s\n”,当前->名称,当前->电话号码);
按顺序打印树(当前->右侧);
}
来自手册:

函数的作用是:比较两个字符串s1和s2。它回来了 如果找到s1,则为小于、等于或大于零的整数, 分别小于、匹配或大于s2

因此,您应该将strcmp的结果与0进行比较,而不是与字符串进行比较。

手册:

函数的作用是:比较两个字符串s1和s2。它回来了 如果找到s1,则为小于、等于或大于零的整数, 分别小于、匹配或大于s2

因此,您应该将strcmp的结果与0进行比较,而不是与字符串进行比较。

from

您的比较,
(strcmp(tree->name,name)name)
,是将
char*
int
进行比较,这实际上是错误的

通常,在比较字符串
s1
时使用
strcmp
s2
的计算方法如下

if( strcmp(s1,s2) == 0 ) 
{
   // equal
}
else if( strcmp(s1,s2) == 0 ) 
{
   // s1 < s2
}
else 
{
  // s1 > s2
}
if(strcmp(s1,s2)==0)
{
//相等的
}
否则如果(strcmp(s1,s2)==0)
{
//s1s2
}
来自

您的比较,
(strcmp(tree->name,name)name)
,是将
char*
int
进行比较,这实际上是错误的

通常,在比较字符串
s1
时使用
strcmp
s2
的计算方法如下

if( strcmp(s1,s2) == 0 ) 
{
   // equal
}
else if( strcmp(s1,s2) == 0 ) 
{
   // s1 < s2
}
else 
{
  // s1 > s2
}
if(strcmp(s1,s2)==0)
{
//相等的
}
否则如果(strcmp(s1,s2)==0)
{
//s1s2
}

欢迎来到堆栈溢出。请阅读,采取,阅读,以及。最后,学习如何创建一个。不要忘了在显示的代码中添加注释,告诉我们它在哪里。不过有一个提示:,特别是它返回的内容。欢迎使用堆栈溢出。请阅读,采取,阅读,以及。最后,学习如何创建一个。别忘了在你显示的代码中添加注释,告诉我们它在哪里。不过有一个提示:,特别是它返回的内容。谢谢!这真的帮助了我。我是第一次使用strcmp(),也是第一次使用C。谢谢!这真的帮助了我。我不熟悉使用strcmp(),也不熟悉C。