C语言 如何为 const char* 数组赋值并打印到屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32389372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:06:17  来源:igfitidea点击:

How to assign values to const char* array and print to screen

carraysstringchar

提问by jshapy8

Although I am using C++, as a requirement I need to use const char* arrays instead of string or char arrays. Since I am new, I am required to learn about how to use const char*. I have declared my const char* array as const char* str[5]. At a later point in the program I need to populate each on of the 5 elements with values.

尽管我使用的是 C++,但作为要求,我需要使用 const char* 数组而不是字符串或字符数组。因为我是新手,所以我需要学习如何使用 const char*。我已将 const char* 数组声明为const char* str[5]. 在程序的稍后点,我需要用值填充 5 个元素中的每一个。

However, if I try to assign a value like this:

但是,如果我尝试分配这样的值:

const char* str[5];
char value[5];
value[0] = "hello";
str[0] = value[0];

It will not compile. What is the proper way to add a char array to char to a const char* array and then print the array? Any help would be appreciated. Thanks.

它不会编译。将 char 数组添加到 char 到 const char* 数组然后打印数组的正确方法是什么?任何帮助,将不胜感激。谢谢。

回答by Bill Lynch

  1. The string "hello"is made up of 6 characters, not 5.

    {'h', 'e', 'l', 'l', 'o', '
    char value[6] = "hello";
    
    '}
  2. If you assign the string when you declare value, your code can look similar to what your currently have:

    char value[6];
    strncpy(value, "hello", sizeof(value));
    
  3. If you want to do it in two separate lines, you should use strncpy().

    const char * str[5];
    char value[6] = "hello";
    str[0] = value;
    
  4. To place a pointer to valuein the list of strings named str:

    {'h', 'e', 'l', 'l', 'o', '
    char value[6] = "hello";
    
    '}

    Note that this leaves str[1]through str[4]with unspecified values.

  1. 字符串"hello"由 6 个字符组成,而不是 5 个。

    char value[6];
    strncpy(value, "hello", sizeof(value));
    
  2. 如果您在声明时分配字符串value,则您的代码可能与您当前拥有的类似:

    const char * str[5];
    char value[6] = "hello";
    str[0] = value;
    
  3. 如果您想在两个单独的行中执行此操作,则应使用strncpy().

    int main(void) {
      const char* str[5];
    
      str[0] = "He" "llo";  // He llo are string literals that concat into 1
    
      char Planet[] = "Earth";  
      str[1] = Planet;  // OK to assign a char * to const char *, but not visa-versa
    
      const char Greet[] = "How";
      str[2] = Greet;
    
      char buf[4];
      buf[0] = 'R'; // For a character array to qualify as a string, need a null character.  
      buf[1] = 0;   // '
    Hello  
    Earth  
    How  
    R  
    
    ' same _value_ as 0 str[3] = buf; // array buf converts to address of first element: char * str[4] = NULL; // Do not want to print this. for (int i = 0; str[i]; i++) puts(str[i]); return 0; }
  4. value在名为 的字符串列表中放置一个指针str

    value[0] = "hello";
    

    请注意,这使得str[1]通过str[4]与未定值。

回答by chux - Reinstate Monica

Various methods to populate const char* str[5]later point in the program.

const char* str[5]在程序中稍后填充的各种方法。

char value[6];
strncpy(value,"hello",sizeof value);

.

.

char value[]="hello";

回答by ameyCU

str[0]=value;

How can this hold "hello". It can hold only one character.

这怎么能保持“你好”。它只能容纳一个字符。

Also value[5]is not enough for this . There will be no space for '\0'and program will exhibit UB.

value[5]对此也还不够。将没有空间,'\0'并且程序将展示UB

Thus either use value[6]-

因此要么使用value[6]-

##代码##

Or declare like this -

或者这样声明——

##代码##

And after that just make the pointer point to this array. Something like this-

然后让指针指向这个数组。像这样的东西——

##代码##