我怎么能存储在用C字符数组的字符串值? [英] How can I store String value in char array in C?
本文介绍了我怎么能存储在用C字符数组的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字符数组存储字符串值。我想一个字符串变量的值存储到char数组。
I have char array to store string values. I wanted to store the value of a string variable into the char array.
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?
推荐答案
这应该为你工作:
#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!
这篇关于我怎么能存储在用C字符数组的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文