在C中,不需要的字符被添加到数组的末尾
我想键入一个字符序列并用临时数组保存它们。之后,我想用临时数组的值创建具有一定大小的实际数组。代码如下:在C中,不需要的字符被添加到数组的末尾,c,arrays,pointers,C,Arrays,Pointers,我想键入一个字符序列并用临时数组保存它们。之后,我想用临时数组的值创建具有一定大小的实际数组。代码如下: #include <stdio.h> int main() { char c; char temp[100]; char array[24]; int i; char *ptrtemp = temp; // create a temporary array with getchar function while(1)
#include <stdio.h>
int main()
{
char c;
char temp[100];
char array[24];
int i;
char *ptrtemp = temp;
// create a temporary array with getchar function
while(1) {
c = getchar();
if(c == '\n')
break;
*ptrtemp = c;
i++;
ptrtemp++;
}
// type wrong in case of wrong size
if(i != 24) {
printf("Data is wrong");
exit(0);
}
char *ptrarray = array;
char *ptrtemp2 = temp;
// create the actual array
for(i=0;i<24;i++) {
*ptrarray = *ptrtemp2;
if(i == 23)
break;
ptrarray++;
ptrtemp2++;
}
//printing the actual array
printf("\n%s\n", array);
}
#包括
int main()
{
字符c;
炭温度[100];
字符数组[24];
int i;
char*ptrtemp=温度;
//使用getchar函数创建临时数组
而(1){
c=getchar();
如果(c=='\n')
打破
*ptrtemp=c;
i++;
ptrtemp++;
}
//如果大小不正确,则键入错误
如果(i!=24){
printf(“数据错误”);
出口(0);
}
char*ptrarray=数组;
char*ptrtemp2=温度;
//创建实际数组
对于(i=0;i两个问题:
您在代码中使用的是未初始化的i。(我认为您看到的问题不是因为这个)
所有C字符串都需要以“\0”结尾。为了确保是这样,您始终可以执行以下操作:
memset(数组,0,sizeof(数组))
你做的事情太复杂了
首先,如前所述,i没有初始化。其次,数组中没有为终止0留下空间。最后,您可以轻松地直接写入数组:
char array[24 + 1]; // need space for the terminating 0 character
for(int i = 0; i < 24; ++i)
{
// write to array directly:
array[i] = getchar();
if(array[i] == '\n')
{
printf("Data is wrong");
return 0;
}
}
array[24] = 0; // this is important if using %s!
printf("\n%s\n", array);
char数组[24+1];//终止0字符需要空间
对于(int i=0;i<24;++i)
{
//直接写入阵列:
数组[i]=getchar();
if(数组[i]='\n')
{
printf(“数据错误”);
返回0;
}
}
数组[24]=0;//如果使用%s,这一点很重要!
printf(“\n%s\n”,数组);
实际上,您还有一个选择,因为您知道,您总是希望打印24个字符:
char array[24]; // skipping(!) space for the terminating 0 character
for(int i = 0; i < 24; ++i)
{
// just as above
}
// NOT now (would be UB, as array is too short):
// array[24] = 0;
// but now need to give precision to tell how many bytes to write
// (maximally; if a terminating 0 would be encountered before,
// printf would stop there)
printf("\n%.24s\n", array);
char数组[24];//跳过终止0字符的(!)空间
对于(int i=0;i<24;++i)
{
//同上
}
//现在不行(可能是UB,因为数组太短):
//数组[24]=0;
//但是现在需要精确地告诉我们要写入多少字节
//(最大值;如果之前遇到终止0,
//printf将在这里停止)
printf(“\n%.24s\n”,数组);
我修改了以下内容:
开始时已将i初始化为0
memset(temp,0,sizeof(temp))
添加以下内容以确保它不会跨越数组边界
类似地,为数组变量添加了以下内容
在for循环的开头执行以下检查,以确保它不会写入数组的第24个元素
if(i==23)
打破
#包括
int main()
{
字符c;
炭温度[100];
字符数组[24];
int i=0;//应初始化为0
char*ptrtemp=温度;
memset(temp,0,sizeof(temp));
//使用getchar函数创建临时数组
而(1){
c=getchar();
如果(c=='\n')
打破
*ptrtemp=c;
i++;
ptrtemp++;
如果(i==99)
{
打破
}
}
char*ptrarray=数组;
char*ptrtemp2=温度;
memset(数组,0,sizeof(数组));
//创建实际数组
对于(i=0;i)在对象的值不确定时使用具有自动存储持续时间的对象的值的未定义行为。int i;
-->
int i=0;
create array的25个元素的可能重复项,并像此数组一样以null终止[24]=0;它可能会从内存中抛出垃圾数据,因为您从未初始化未使用的值并将其设置为某个值,这应该可以解决此问题
if (i == 99)
{
break;
}
memset(array, 0, sizeof(array));
if(i == 23)
break;
#include <stdio.h>
int main()
{
char c;
char temp[100];
char array[24];
int i = 0; //Should be initialized to 0
char *ptrtemp = temp;
memset(temp, 0, sizeof(temp));
// create a temporary array with getchar function
while(1) {
c = getchar();
if(c == '\n')
break;
*ptrtemp = c;
i++;
ptrtemp++;
if (i == 99)
{
break;
}
}
char *ptrarray = array;
char *ptrtemp2 = temp;
memset(array, 0, sizeof(array));
// create the actual array
for(i=0;i<24;i++) {
if(i == 23)
break;
*ptrarray = *ptrtemp2;
ptrarray++;
ptrtemp2++;
}
//printing the actual array
printf("\n%s\n", array);
}