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

标签 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 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/

相关文章:

C - 从特定的大范围中获取素数

objective-c - 从 Objective-C 调用 C 函数会丢失传入的值

c - 将int写入c中的文件: getting weird character "\00"

c++ - 代码守卫失败

c - 从 C 移植学习 Rust

c - `:>`(冒号,大于,又名笑脸)在 C 编程语言中是什么意思?

python - 在 Pydev/Eclipse 的 python 程序中使用外部 C 库

c - 即使 char 已签名, 'a' 和 '0' 是否始终具有正值?

C 编程 : Preprocessor, 宏作为标记

c - 如何在 Perl 或 C 中 "replay"wireshark c 数组