将字符串从struct成员复制到char数组的strcpy()失败。为什么?

时间:2018-12-07 15:12:09

标签: c arrays function pointers

我有一个node结构,用于创建二进制搜索树。在每个节点内部,我存储一个整数KEY和一个对应的字符串value。我正在树中执行一些搜索,并希望返回仅包含特定节点的键值对的数组。

为此,我通过引用传递数组并将整数KEY保存到该数组。效果很好,但是当我尝试与字符串相同时,结果却很糟糕。

在下面的代码中,我试图将root[count].value;中的字符串复制到p_value_arr[*p_unique_count]这是一个char数组。

结构定义

   typedef struct node {
        int KEY;
        char *value;
        int node_count;
        struct node *left, *right;
        int unique_count;
    } node;

用于遍历图形并复制唯一键值对的功能。 KEY未正确复制到数组,而value未正确复制到数组。

void unique_key(node *root, int *p_unique_count, int p_unique_arr[], char *p_value_arr[]) { 
    int count = 0;      
    //unique *temp = (unique *)malloc(n * sizeof(unique));

    if (root != NULL)
    {
        unique_key(root->left, p_unique_count, p_unique_arr, p_value_arr);
        if (root->node_count == 1) {

            root[count].unique_count = *p_unique_count;
            p_unique_arr[*p_unique_count] = root[count].KEY;
            printf("%s\n", root[count].value);
            //"warning: assignment makes integer from pointer without a cast"
            strcpy(p_value_arr[*p_unique_count],root[count].value);  

            printf("%d(%d) -> %s %d\n", root->KEY, root->node_count, root->value, root->unique_count);
                        (*p_unique_count)++;
            count++;
        }
        unique_key(root->right, p_unique_count, p_unique_arr, p_value_arr);
    }
}

使用BST中的给定密钥插入新节点的实用程序功能

node* insert_node(node* node, int key, char *value)
{
    /* If the tree is empty, return a new node */
    if (node == NULL) 
        return newNode(key,value);

    // If key already exists in BST, icnrement count and return 
    if (key == node->KEY)
    {
        (node->node_count)++;
    //  return node;
    }

    /* Otherwise, recur down the tree */
    if (key < node->KEY)
        node->left = insert_node(node->left, key, value);
    else
        node->right = insert_node(node->right, key, value);

    /* return the (unchanged) node pointer */
    return node;
}

node *newNode(int KEY, char *value)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->KEY = KEY;
    strcpy(temp->value, value);
    temp->left = temp->right = NULL;
    temp->node_count = 1;
    return temp;
}

主要驱动程序代码

int main() {
    int unique_count = 0;
    int in_count = 0;
    int unique_arr[10]; /
    char *value_arr[10];   // an array of pointers  

    /* Let us create following BST.  Passing values along with key */
    node *root = NULL;

    //this is for storing commands 
    root = insert_node(root, 2, "Hello");
    root = insert_node(root, 3, "Thanks");

    printf("\nkeys of the given tree \n");
    unique_key(root, &unique_count, unique_arr, *value_arr);
    for(int i = 0; i < 10; i++) {                
        printf("%d %s\n", unique_arr[i], value_arr[i]);  //Mismatching the argument type "char" and conversion specifier "s" and nothing prints here
    }

}

输出:

  

你好

     给定树的

     

分段错误

关于如何有效地将struct成员内部的字符串复制到char数组的任何建议?


编辑:

完整代码: https://pastebin.com/CB4Gp0gY

由于char *value_arr[10];是一个指针数组,因此我遵循了K&R的5.6章。C编程语言将指针数组传递给该函数。我现在没有任何警告,但段错误仍然存​​在。

我在NetBeans 8.2上也设置了more warnings

调试器的输出:

/cygdrive/C/Users/****/AppData/Roaming/NetBeans/8.2/bin/nativeexecution/dorun.sh: line 71: 16516 Segmentation fault      (core dumped) sh "${SHFILE}"

2 个答案:

答案 0 :(得分:0)

要在这里跟进隆丁

node *newNode(int KEY, char *value)
{
  // Allocate a new overall structure
  struct node *temp = (struct node *)malloc(sizeof(struct node));

  //Copy the integer key
  temp->KEY = KEY;

  // uh oh - copy the given string into a random location in memory and segfault.
  // Hint - you need to allocate enough memory to hold the incoming string.
  // Advanced hint - If you don't want to make a copy of the string, you can 
  // just store its pointer, but it would want to be marked constant at the least... 
  strcpy(temp->value, value);

  // Set tree stuff and count, but we are already dead...
  temp->left = temp->right = NULL;
  temp->node_count = 1;
  return temp;
}

printf("%d %s\n", unique_arr[i], value_arr[i]);  //Mismatching the argument type "char" and conversion specifier "s" and nothing prints here

将失败,因为value_arr[i]不是字符串,而是char *。为此,它必须指向有效的C字符串,或者指向具有正确'\0'终止字符串的内存。

请看一下他给定的链接,因为您需要更深入地了解C字符串的工作原理。

答案 1 :(得分:0)

char *value_arr[10]; //问题在这里

这将初始化一个指针数组,但在将其用于诸如strcpy()之类的指针之前不会分配内存。根据K&R第5.6章,应该使用alloc()为函数内的指针数组分配内存。

如果要使指针指向某个用于存储字符串的内存,则必须创建一个这样的内存区域,并将指针设置为指向该内存。

 char *p;

 alloc(strlen(root[count].value) +1);

 strcpy(p, root[count].value);
 p_value_arr[*p_unique_count] = p;
相关问题