[英]How can I store String value in char array in C?
I have char array to store string values. 我有char数组来存储字符串值。 I wanted to store the value of a string variable into the char array.
我想将字符串变量的值存储到char数组中。
char Password[30];
char User[2];
int i;
for(i=0; i<5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password,30);
}
I wanted to input the values for the array and it should throw a buffer overflow but I couldn't do it. 我想输入数组的值,它应该抛出缓冲区溢出但我不能这样做。 How can I do it?
我该怎么做?
This should work for you: 这应该适合你:
#include <stdio.h>
#include <string.h>
int main() {
char Password[30];
char User[5][30];
int i;
for(i = 0; i < 5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password);
}
for(i = 0; i < 5; i++)
printf("Password %d: %s\n", i+1, User[i]);
return 0;
}
The second for loop is to show the output and that every thing is stored right! 第二个for循环是显示输出,并且每个东西都存储正确!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.