#include <stdio.h>
int main() {
char a = 5;
char b[2] = "hi"; // No explicit room for `\0`.
char c = 6;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每当我们编写一个用双引号括起来的字符串时,C 都会自动为我们创建一个字符数组,其中包含该字符串,并以 \0 字符结尾 http://www.eskimo.com/~scs/cclass/notes/sx8。 html
在上面的示例中,b只有 2 个字符的空间,因此空终止字符没有位置可放置,但编译器正在重新组织内存存储指令,以便将a和c存储b在内存中,以便为 a 腾出\0空间数组的末尾。
这是预期的还是我遇到了未定义的行为?
我们正在将C代码转换为C++.
我注意到以下代码在C中定义得很好,
int main(){
//length is valid. '\0' is ignored
char str[3]="abc";
}
Run Code Online (Sandbox Code Playgroud)
正如在Array初始化中所述:
"如果数组的大小已知,它可能比字符串文字的大小小一个,在这种情况下,终止空字符将被忽略."
但是,如果我要在C++中构建相同的代码,我会收到以下C++错误:
error: initializer-string for array of chars is too long
[-fpermissive] char str[3]="abc";
Run Code Online (Sandbox Code Playgroud)
我希望有人可以对此进行阐述.
问题:
代码示例是否在所有C语言标准中都有效?
它在所有C++语言标准中都无效吗?
是否有一种语言在一种语言中有效而在另一种语言中无效?
这个C程序给出了一个奇怪的结果:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[5] = "abcde";
char str2[5] = " haha";
printf("%s\n", str1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到:
abcde haha
Run Code Online (Sandbox Code Playgroud)
我只想打印第一个字符串,从代码中可以看出.
为什么要打印它们?
我无法理解C风格的字符串是什么.新年快乐
我所知道的:指针包含一个内存地址.取消引用指针将为您提供该内存位置的数据.
int x = 50;
int* ptr = &x; //pointer to an integer, holds memory address of x
cout << "&x: " << &x << endl; //these two lines give the same output as expected
cout << "ptr: " << ptr << endl;
cout << "*ptr: " << dec << (*ptr) << endl; //prints out decimal number 50
//added dec, so the program doesnt
//continue to printout hexidecimal numbers like it did for the
//the memory addresses above
cout …Run Code Online (Sandbox Code Playgroud) 我正在使用gcc 4.9.1/Mingw并使用以下代码编译代码:
gcc test.c -otest.exe -std = c11 -pedantic-errors -Wall -Wextra
此代码提供诊断:
int main (void)
{
char a[5] = {'h','e','l','l','o','\0'};
}
Run Code Online (Sandbox Code Playgroud)
错误:数组初始化器中的多余元素char [5]
但是,此代码不会产生警告:
int main (void)
{
char b[5] = "hello";
}
Run Code Online (Sandbox Code Playgroud)
我认为这两种形式是100%相同的.C标准中是否有任何理由或微妙之处,为什么后者不应该发出警告?
或者这是编译器错误?我知道C标准允许多余的初始化器,与C++不同,所以我不相信gcc 需要进行诊断.但我希望编译器能够始终如一地发出警告.
我期望在下面的程序中使用gcc看到"charmer数组的初始化字符串太长"警告.
程序:
int main()
{
char str1[4]="1234";
char str2[3]="1234";
(void)str1; // Remove unused variable warning.
(void)str2; // Remove unused variable warning.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我只收到警告str2.
以来
char str1[4]="1234";
Run Code Online (Sandbox Code Playgroud)
相当于
char str1[4]= {'1', '2', '3', '4', '\0'};
Run Code Online (Sandbox Code Playgroud)
我们不应该也得到同样的警告str1吗?
这是gcc的缺陷吗?
编译命令:
gcc -Wall -std = c99 soc.c -o soc
gcc 版本是4.8.4.
更新
刚才了解到
char str1[4]="1234";
Run Code Online (Sandbox Code Playgroud)
不等于
char str1[4]= {'1', '2', '3', '4', '\0'};
Run Code Online (Sandbox Code Playgroud)
更新2
char str1[4]="1234";
Run Code Online (Sandbox Code Playgroud)
在C++ 11中是不正确的(第8.5.2/2节).我不认为C99和C++ 11会以不同的方式对待它们.
我有这个简单的程序,它可以正常工作.
#include<stdio.h>
int main(int argc, char *argv[]) {
int mystrlen(char *s);
char s[6] = "ABCDEF";
printf("%d", mystrlen(s)); // print 6
}
int mystrlen(char *s) {
char *p=s; /*Assigning the starting value to p */
while(*s != '\0') {
s = s + 1;
}
return s-p;
}
Run Code Online (Sandbox Code Playgroud)
我将做的唯一更改是int main(int argc, char *argv[]),我将使用int main(void),而不是发送任何命令行参数.
现在,同样的程序对我来说完全行为不端!
#include<stdio.h>
int main(void)
{
int mystrlen(char *s);
char s[6]="ABCDEF";
printf("%d",mystrlen(s)); // prints 8 !!
}
int mystrlen(char *s) {
char *p=s; /*Assigning …Run Code Online (Sandbox Code Playgroud) 我有一个运行时错误,它从数组写入的数据多于其打印ID,AverageMarks和Status:
宣言及其功能
char StudentStatus[10][7];
for(x=0;x<10;x++)
{
fprintf(OutputFile,"%d\t\t%d\t\t\t%s\n",StudentID[x],StudentAvg[x],StudentStatus[x]);
}
Run Code Online (Sandbox Code Playgroud)
但是当它打印出来时
100 77 DismissActive
101 85 Active
102 88 Active
103 86 Active
104 85 Active
105 84 Active
106 84 Active
107 82 Active
108 92 Active
109 75 Dismiss
数组的填充方式:
for(x=0;x<NumOfStudent;x++)
{
if(StudentAvg[x]>80)
{
strcpy(StudentStatus[x],"Active");
printf(". ");
}
else
{
strcpy(StudentStatus[x],"Dismiss");
printf(". ");
}
}
Run Code Online (Sandbox Code Playgroud)
其他声明工作正常,但第一个声明真的很麻烦我.我错误编码的任何建议?