C字符串中无效的初始化器
我正在测试一个大写,以在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
键入
char * lower [n];
,您要求编译器在字符数组上创建指针。但是您只需要一个字符数组:
Typing
char * lower[N];
, you ask your compiler to create a pointer on a character array.But you want just a characters array:
从C标准(6.7.9初始化)
也就是说,您可以声明字符数组并用字符串字面的字符进行初始化,
但是您声明了类型
char *char *
而不是字符数组的数组。在这种情况下,您需要将初始化器封闭在括号中,例如上面的行中,有指向指针
char *char *
的数组,该数组指向字符串文字的第一个字符采样
。但是,使用指针(数组的单个元素),您可能不会更改字符串文字。任何更改字符串字面的尝试都会导致不确定的行为。考虑到
使用格式字符串
“%s”
的printf
的呼叫,看来您将准确处理字符数组。否则,您有RO写入,因此您需要写入
或
在这种情况下,而不是
在
strcpy
的调用中看起来像是由于数组由于初始化而已经包含了该字符串的字体。
From the C Standard (6.7.9 Initialization)
That is you could declare a character array and initialize it with a string literal like
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 likeIn 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 literalSAMPLESTRING
. 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
where there is used the format string
"%s"
it seems you are going to deal exactly with character arrays. Otherwise you had ro writeSo you need to write
or
instead of
In this case calls of
strcpy
will look likethough the arrays already contain this string literal due to their initializations.
您可能会感到困惑的部分是使用全局常数作为字符数组的值。
使用GOBAR常数设置值等于使用本地变量,字符串或字符数组设置值。
这些方法中的任何一个都应该足够:
如果您打算使用指针,则用于指向数组的数组变量不需要仅仅是一个数组。
使用您的代码,这将是这样执行的:
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:
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: