简体   繁体   English

从函数获取const char *数组

[英]Getting const char* array from function

Need help in getting const char* array from function so elements can be printed in main. 需要帮助从函数获取const char*数组,以便可以在main中打印元素。

main: 主要:

const char* values[3];
strings_to_array();

printf("%s\n", values[1]);
printf("%s\n", values[2]);

function: 功能:

const char* strings_to_array()
{
    char one_str[16];
    char two_str[16];
    char three_str[16];

    strcpy(one_str, "one");
    strcpy(two_str, "two");
    strcpy(three_str, "three");

    const char* values[] = {one_str, two_str, three_str};
    return values;
}

What is incorrect here and how to get values to main? 这里有什么不正确的地方,以及如何获取主要价值?

The main problem with this code is that: functions in C should not return pointers to local variables as they are stored on the stack, which means they are not available once the function returns. 该代码的主要问题在于:C语言中的函数不应将指向局部变量的指针返回,因为它们存储在堆栈中,这意味着一旦函数返回,它们将不可用。

So this line: 所以这行:

const char* values[] = {one_str, two_str, three_str};

Can be replaced with: 可以替换为:

const char** values = malloc(3*sizeof(char *));
values[0] = strdup(one_str);
values[1] = strdup(two_str);
values[2] = strdup(three_str);

The full working code of the above example: 上面示例的完整工作代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char** strings_to_array()
{
    char one_str[16];
    char two_str[16];
    char three_str[16];

    strcpy(one_str, "one");
    strcpy(two_str, "two");
    strcpy(three_str, "three");

    const char** values = malloc(3*sizeof(char *));
    values[0] = strdup(one_str);
    values[1] = strdup(two_str);
    values[2] = strdup(three_str);
    return values;
}

int main() {
  const char** values = strings_to_array();

  printf("%s\n", values[1]);
  printf("%s\n", values[2]);

  free((void *)values[0]);
  free((void *)values[1]);
  free((void *)values[2]);
  free(values);      

  return 0;
}
  1. It is wrong syntactically because you declared return type as const char* and you are trying to return const char** . 在语法上是错误的,因为您将返回类型声明为const char*并且您正在尝试返回const char**

  2. It is wrong semantically because you are trying to return a pointer to array allocated on stack. 从语义上讲这是错误的,因为您试图返回一个指向分配在堆栈上的数组的指针。

Nothing initializes the array of character pointers named "values" in main. 在main中,什么也没有初始化名为“ values”的字符指针数组。 What is that function supposed to do? 该功能应该做什么? It does nothing, and its return value is ignored in main. 它什么也不做,其返回值在main中被忽略。

The variables one_str, two_str, three_str, and values are local variables of the 'strings_to_array' function. 变量one_str,two_str,Three_str和values是'strings_to_array'函数的局部变量。 They have no valid existence outside this function. 它们在此功能之外没有有效的存在。

By returning 'values' (ie: a pointer to a local variable), you return a pointer to an invalid memory location. 通过返回“值”(即:指向局部变量的指针),可以返回指向无效内存位置的指针。

One way is to declare your variables as static since you are going to access them outside of that function. 一种方法是将变量声明为静态变量,因为您将在该函数之外访问它们。 It just means that memory for those are not lost once you exit the function. 这只是意味着一旦退出该功能,这些内存就不会丢失。 Also, you should be returning a char ** instead of char * . 另外,您应该返回char **而不是char *

const char** strings_to_array()
{
    static char one_str[16];
    static char two_str[16];
    static char three_str[16];

    strcpy(one_str, "one");
    strcpy(two_str, "two");
    strcpy(three_str, "three");

    static const char* values[] = {one_str, two_str, three_str};
    return values;
}

As you are dealing with literals ( "one" , ...) you could simply assign their addresses. 在处理文字( "one" ,...)时,您只需分配它们的地址即可。

main: 主要:

void string_to_array(const char **);

const char * values[3] = {NULL};
strings_to_array(values);

printf("%s\n", values[0]);    
printf("%s\n", values[1]);
printf("%s\n", values[2]);

function: 功能:

void strings_to_array(const char ** values)
{
  values[0] = "one";
  values[1] = "two";
  values[2] = "three";
}

And if you really need it as a function returning a reference to the array do it this way: 并且如果您真的需要它作为返回对数组的引用的函数,请按照以下方式进行操作:

const char ** strings_to_array2(const char ** values)
{
  values[0] = "one";
  values[1] = "two";
  values[2] = "three";

  return values;
}

This keeps you from doing the ugly call to free() on const char * s. 这样可以避免对const char * s进行free()的丑陋调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM