如何修复警告:C 中指针和整数之间的比较



因此,对于我的C代码,我正在编写一个二叉搜索树,该树接受一个名为node的typedef结构,该结构具有char*名称和char*电话。我在此代码中所做的是将其放在二叉搜索树中,并按字母顺序输出列表,旁边是 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 ordern");
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  %sn", current->name, current->phoneNum);
print_tree_inorder(current->right);
}

从手册 http://man7.org/linux/man-pages/man3/strcmp.3.html:

strcmp(( 函数比较两个字符串 s1 和 s2。 它返回 如果找到 S1,则为小于、等于或大于零的整数, 分别小于、匹配或大于 S2。

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

来自strcmp

RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first
n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

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

一般使用strcmp比较字符串s1s2的计算可以简单如下:

if( strcmp(s1,s2) == 0 ) 
{
// equal
}
else if( strcmp(s1,s2) == 0 ) 
{
// s1 < s2
}
else 
{
// s1 > s2
}

最新更新