C字符串中无效的初始化器

发布于 2025-01-21 09:39:12 字数 705 浏览 2 评论 0原文

我正在测试一个大写,以在C中较小的Converter(我相对较新),并且在主文件中遇到了一些问题(我在下面已附加了)。我可以添加上锅。C代码,但我认为这与我的问题无关。我会遇到一些无效的初始化错误,但我会做到的。我附上了错误消息的图像。您还可以在代码中查看我已经评论了一些strcpy方法,因为它们导致了一些运行时分段错误错误。有人知道发生了什么吗? 错误消息

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "upperlower.h"

#define SAMPLESTRING "AbCdEfGhIjKlMnOpQrStUvWxYz"

int main()
{
    char* lower[] = SAMPLESTRING;
    char* upper[] = SAMPLESTRING;

    // strcpy(*lower, SAMPLESTRING);
    // strcpy(*upper, SAMPLESTRING);

    ToLower(lower, lower);
    ToUpper(upper, upper);

    printf("Lower: %s\nUpper: %s\n", lower, upper);
}

I am testing out a uppercase to lowercase converter in C (I'm relatively new), and I've been having some issues in the main file (which I have attached below). I can add the upperlower.c code, but I don't think it's related to my problem. I am getting some invalid initializer errors, and I would've though this works. I have attached an image of the error message. You can also see in the code that I have commented out some strcpy methods, as they caused some runtime segmentation fault errors. Does anyone know what's going on?
Error Message

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "upperlower.h"

#define SAMPLESTRING "AbCdEfGhIjKlMnOpQrStUvWxYz"

int main()
{
    char* lower[] = SAMPLESTRING;
    char* upper[] = SAMPLESTRING;

    // strcpy(*lower, SAMPLESTRING);
    // strcpy(*upper, SAMPLESTRING);

    ToLower(lower, lower);
    ToUpper(upper, upper);

    printf("Lower: %s\nUpper: %s\n", lower, upper);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2025-01-28 09:39:12

键入char * lower [n];,您要求编译器在字符数组上创建指针。

但是您只需要一个字符数组:

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;

Typing char * lower[N];, you ask your compiler to create a pointer on a character array.

But you want just a characters array:

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;
焚却相思 2025-01-28 09:39:12

从C标准(6.7.9初始化)

14 角色类型可能由字符初始化
字符串文字
或utf -8字符串文字,可选封闭
牙套
。字符串文字的连续字节(包括
如果有空间或数组是
尺寸未知)初始化数组的元素。

也就是说,您可以声明字符数组并用字符串字面的字符进行初始化,

char lower[] = SAMPLESTRING;

但是您声明了类型char *char *而不是字符数组的数组。在这种情况下,您需要将初始化器封闭在括号中,例如

char* lower[] = { SAMPLESTRING };

上面的行中,有指向指针char *char *的数组,该数组指向字符串文字的第一个字符采样。但是,使用指针(数组的单个元素),您可能不会更改字符串文字。任何更改字符串字面的尝试都会导致不确定的行为。

考虑到

printf("Lower: %s\nUpper: %s\n", lower, upper);

使用格式字符串“%s”printf的呼叫,看来您将准确处理字符数组。否则,您有RO写入,

printf("Lower: %s\nUpper: %s\n", lower[0], upper[0]);

因此您需要写入

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;

char lower[] = { SAMPLESTRING };
char upper[] = { SAMPLESTRING };

在这种情况下,而不是

char* lower[] = SAMPLESTRING;
char* upper[] = SAMPLESTRING;

strcpy的调用中看起来像是

strcpy( lower, SAMPLESTRING);
strcpy( upper, SAMPLESTRING);

由于数组由于初始化而已经包含了该字符串的字体。

From the C Standard (6.7.9 Initialization)

14 An array of character type may be initialized by a character
string literal
or UTF−8 string literal, optionally enclosed in
braces
. Successive bytes of the string literal (including the
terminating null character if there is room or if the array is of
unknown size) initialize the elements of the array.

That is you could declare a character array and initialize it with a string literal like

char lower[] = SAMPLESTRING;

However you declared an array of pointers of the type char * instead of a character array. In this case you need to enclose the initializer in braces like

char* lower[] = { SAMPLESTRING };

In the line above there is declared an array with one element of the pointer type char * that points to the first character of the string literal SAMPLESTRING. However using the pointer (the single element of the array) you may not change the string literal. Any attempt to change a string literal results in undefined behavior.

Taking into account this call of printf

printf("Lower: %s\nUpper: %s\n", lower, upper);

where there is used the format string "%s" it seems you are going to deal exactly with character arrays. Otherwise you had ro write

printf("Lower: %s\nUpper: %s\n", lower[0], upper[0]);

So you need to write

char lower[] = SAMPLESTRING;
char upper[] = SAMPLESTRING;

or

char lower[] = { SAMPLESTRING };
char upper[] = { SAMPLESTRING };

instead of

char* lower[] = SAMPLESTRING;
char* upper[] = SAMPLESTRING;

In this case calls of strcpy will look like

strcpy( lower, SAMPLESTRING);
strcpy( upper, SAMPLESTRING);

though the arrays already contain this string literal due to their initializations.

终弃我 2025-01-28 09:39:12

您可能会感到困惑的部分是使用全局常数作为字符数组的值。

使用GOBAR常数设置值等于使用本地变量,字符串或字符数组设置值。

这些方法中的任何一个都应该足够:

char array[] = globalvariable;
char array[] = localvariable;
char array[] = "this is a string";
char array[] = {'t','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'}

如果您打算使用指针,则用于指向数组的数组变量不需要仅仅是一个数组。

使用您的代码,这将是这样执行的:

char * lower;
char * upper;

The part you may be confused about is using a global constant as the value for the character array.

Setting the value using a gobal constant is equivalent to setting the value using a local variable, string, or character array.

Any of these methods should be sufficient:

char array[] = globalvariable;
char array[] = localvariable;
char array[] = "this is a string";
char array[] = {'t','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'}

if you intend on using a pointer, then your array variable used to point towards the array does not need to be an array just a pointer.

Using your code, this would be performed like this:

char * lower;
char * upper;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文