gpt4 book ai didi

c - 如何修复警告 : comparison between pointer and integer in C

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

因此,对于我的 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 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);
}

最佳答案

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

The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

所以你应该将 strcmp 的结果与 0 进行比较,而不是与字符串进行比较。

关于c - 如何修复警告 : comparison between pointer and integer in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913533/

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