(C) 字符数组大小在输入后改变 ?
(C) Char array size changes after input ?
以下有什么区别? ( C )
char x[100] ;
char y[] = ""; // no space between the double quotations
char z[] = " "; // space between the double quotations
如果用户在数组 y 中输入了一个输入,例如 "test",它的大小是否会更改为 5?
char y[] ="";
gets(y); // user entered "test"
如果用户在数组 x 中输入大于 100 的输入,它的大小是否会发生变化?
char x[100] ;
gets(x); // user entered an input larger than 100
以及此代码有效的原因:(如果用户输入 "test" 它将打印 "test" )
#include<stdio.h>
#include<string.h>
int main(){
char name[] = " " ; // space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
而这个不是? (这个打印奇怪的符号)(如果用户输入 "test" 它将打印 "t" 和一个笑脸符号)
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
这个让我抓狂,它没有循环工作,即使双引号之间没有 space
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
printf("%c",name[0]);
printf("%c",name[1]);
printf("%c",name[2]);
printf("%c",name[3]);
return 0 ;
}
即使双引号之间没有 space,这个也可以使用 ( puts ) 工作:
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ;
gets(name);
puts(name);
return 0 ;
}
否,数组未调整大小。
之所以有效,是因为行为未定义,可能发生的事情之一就是它有效。
当您这样做时,您的写入超出了数组的范围,根据非法写入位置的数据不同,会发生不同的事情。
永远不要使用 gets()
因为它不能防止缓冲区溢出,这是你的程序正在做的,你应该使用 fgets()
函数,它以数组的大小作为参数来防止写入超过该大小的字节进入数组
fgets(array, sizeOfTheArray, stdin);
会阻止这个问题,并且代码不会像您认为的那样“工作”。
此外,c 中的字符串不会将它的大小存储在任何地方,所以像你这样调用 strlen()
是不好的,这个
for(int i=0 ; i< strlen(name) ; i++)
/* ^ do not do this */
导致性能不佳,你可以像
这样存储值
size_t length = strlen(name);
for(int i = 0 ; i < length ; i++)
或者,使用 c 字符串是非 nul
字节后跟 nul
字节的序列这一事实,就像这样
for(int i = 0 ; name[i] != '[=13=]' ; i++)
if the user entered an input for example "test" in the array y , does it's size changes to 5 ?
没有。数组的大小是常量。(在 y
的情况下为 1)
and if user entered an input larger than 100 in the array x , does it's size changes ?
没有。数组的大小是常量。(在 x
的情况下为 100)
在这两种情况下,代码都会触发 Undefined Behavior(which means that anything can happen) as extra characters of the input are written in invalid locations. This is why the gets()
function is dangerous too! The reason being that gets()
does not prevent buffer overflows。
and why this code works : ( if user entered "test" it will print "test" )
如果输入超过一个字符,任何事情都可能发生。同样,由于 Undefined Behavior
it worked without a loop , even with no space between double quotations
and this one works using ( puts ) even with no space between double quotations :
以下有什么区别? ( C )
char x[100] ;
char y[] = ""; // no space between the double quotations
char z[] = " "; // space between the double quotations
如果用户在数组 y 中输入了一个输入,例如 "test",它的大小是否会更改为 5?
char y[] ="";
gets(y); // user entered "test"
如果用户在数组 x 中输入大于 100 的输入,它的大小是否会发生变化?
char x[100] ;
gets(x); // user entered an input larger than 100
以及此代码有效的原因:(如果用户输入 "test" 它将打印 "test" )
#include<stdio.h>
#include<string.h>
int main(){
char name[] = " " ; // space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
而这个不是? (这个打印奇怪的符号)(如果用户输入 "test" 它将打印 "t" 和一个笑脸符号)
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
for(int i=0 ; i< strlen(name) ; i++) {
printf("%c",name[i]);
}
return 0 ;
}
这个让我抓狂,它没有循环工作,即使双引号之间没有 space
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ; // no space between the double quotations
gets(name);
printf("%c",name[0]);
printf("%c",name[1]);
printf("%c",name[2]);
printf("%c",name[3]);
return 0 ;
}
即使双引号之间没有 space,这个也可以使用 ( puts ) 工作:
#include<stdio.h>
#include<string.h>
int main(){
char name[] = "" ;
gets(name);
puts(name);
return 0 ;
}
否,数组未调整大小。
之所以有效,是因为行为未定义,可能发生的事情之一就是它有效。
当您这样做时,您的写入超出了数组的范围,根据非法写入位置的数据不同,会发生不同的事情。
永远不要使用 gets()
因为它不能防止缓冲区溢出,这是你的程序正在做的,你应该使用 fgets()
函数,它以数组的大小作为参数来防止写入超过该大小的字节进入数组
fgets(array, sizeOfTheArray, stdin);
会阻止这个问题,并且代码不会像您认为的那样“工作”。
此外,c 中的字符串不会将它的大小存储在任何地方,所以像你这样调用 strlen()
是不好的,这个
for(int i=0 ; i< strlen(name) ; i++)
/* ^ do not do this */
导致性能不佳,你可以像
这样存储值size_t length = strlen(name);
for(int i = 0 ; i < length ; i++)
或者,使用 c 字符串是非 nul
字节后跟 nul
字节的序列这一事实,就像这样
for(int i = 0 ; name[i] != '[=13=]' ; i++)
if the user entered an input for example "test" in the array y , does it's size changes to 5 ?
没有。数组的大小是常量。(在 y
的情况下为 1)
and if user entered an input larger than 100 in the array x , does it's size changes ?
没有。数组的大小是常量。(在 x
的情况下为 100)
在这两种情况下,代码都会触发 Undefined Behavior(which means that anything can happen) as extra characters of the input are written in invalid locations. This is why the gets()
function is dangerous too! The reason being that gets()
does not prevent buffer overflows。
and why this code works : ( if user entered "test" it will print "test" )
如果输入超过一个字符,任何事情都可能发生。同样,由于 Undefined Behavior
it worked without a loop , even with no space between double quotations
and this one works using ( puts ) even with no space between double quotations :