简体   繁体   English

在 C 中打印字符串数组

[英]Print String Array in C

This is an array of strings in C language.这是 C 语言的字符串数组。 I try to print the elements, but the program doesn't print them.我尝试打印元素,但程序不打印它们。 What is the error in the code?代码中的错误是什么?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char day[7]={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};
 
    printf("%s\n", day[0]);
}

The statement该声明

char day[7];

declares an array of char s, which can only accommodate 7 bytes — including the null-terminator if it's a string — not 7 strings of indeterminate length.声明一个char数组,它只能容纳 7 个字节——如果它是一个字符串,则包括空终止符——而不是 7 个不确定长度的字符串。

You can instead declare an array of pointers to char , or char * s, where each pointer points to a sequence of char s in memory.您可以改为声明指向charchar *的指针数组,其中每个指针指向 memory 中的一系列char

const char *day[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

In memory, it will look something like this:在 memory 中,它看起来像这样:

--------------------------------------
day[0] -> | "Saturday"
--------------------------------------
day[1] -> | "Sunday"
--------------------------------------
day[2] -> | "Monday"
--------------------------------------
day[3] -> | "Tuesday"
--------------------------------------
day[4] -> | "Wednesday"
--------------------------------------
day[5] -> | "Thursday"
--------------------------------------
day[6] -> | "Friday"
--------------------------------------

As day is an array of pointers, you can make it's elements (ie pointers) point to somewhere else in memory, but you can't change the string literals themselves.由于day是一个指针数组,您可以使它的元素(即指针)指向 memory 中的其他地方,但您不能更改字符串文字本身。

This declaration本声明

char day[7]={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

is invalid.是无效的。

There is not declared an array of strings.没有声明一个字符串数组。 There is declared an array of 7 characters that are initialized by addresses of string literals.声明了一个由 7 个字符组成的数组,这些字符由字符串文字的地址初始化。

The compiler should issue a message relative to this declaration because there is no implicit conversion from the type char * (the type of the initializing expressions) to the type char .编译器应该发出一条与此声明相关的消息,因为没有从类型char * (初始化表达式的类型)到类型char的隐式转换。

Instead you should declare a one-dimensional array of pointers like相反,您应该声明一个一维指针数组,例如

char * day[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

or better like或者更喜欢

const char * day[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

because you may not change string literals (though in C opposite to C++ string literals have types of non-constant character arrays)因为您可能不会更改字符串文字(尽管在 C 中与 C++ 字符串文字具有非常量字符数组类型)

In these declarations the string literals having array types are implicitly converted to pointers to their first characters.在这些声明中,具有数组类型的字符串文字被隐式转换为指向其第一个字符的指针。

Or a two-dimensional array like或者像这样的二维数组

char day[7][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday"};

In this declaration all characters including the terminating zero character '\0' of the string literals are used to explicitly initialize elements of the declared array.在此声明中,包括字符串文字的终止零字符'\0'在内的所有字符都用于显式初始化声明数组的元素。 All characters of the array that do not have a corresponding explicit initializer will be zero initialized.数组中没有相应显式初始值设定项的所有字符都将初始化为零。

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

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