c语言puts()输出数组出现乱码,
我想要把一个数组的内容复制到另一个数组里,最后puts(ar1)就出现乱码了#include<stdio.h>#include<ctype.h>voidCOPY(char...
我想要把一个数组的内容复制到另一个数组里,
最后puts(ar1)就出现乱码了
#include <stdio.h>
#include <ctype.h>
void COPY(char *, char *, int);
int main()
{
char ar1[100];
char ar2[100];
int n;
puts("Please enter a message to ar2");
gets(ar2);
puts("How many characters you need to copy?");
scanf_s("%d",&n);
COPY(ar1, ar2, n);
puts("Now let's see what would happen when we puts(ar1)");
puts("Puts(ar1)");
puts(ar1);
return 0;
}
void COPY(char *ar1, char *ar2, int n)
{
int i;
for (i = 0; i < n; i++)
*(ar1 + i) = *(ar2 + i);
} 展开
最后puts(ar1)就出现乱码了
#include <stdio.h>
#include <ctype.h>
void COPY(char *, char *, int);
int main()
{
char ar1[100];
char ar2[100];
int n;
puts("Please enter a message to ar2");
gets(ar2);
puts("How many characters you need to copy?");
scanf_s("%d",&n);
COPY(ar1, ar2, n);
puts("Now let's see what would happen when we puts(ar1)");
puts("Puts(ar1)");
puts(ar1);
return 0;
}
void COPY(char *ar1, char *ar2, int n)
{
int i;
for (i = 0; i < n; i++)
*(ar1 + i) = *(ar2 + i);
} 展开
2个回答
展开全部
//因为没有结束标识'\0'
#include <stdio.h>
#include <ctype.h>
void COPY(char *, char *, int);
int main()
{
char ar1[100];
char ar2[100];
int n;
puts("Please enter a message to ar2");
gets(ar2);
puts("How many characters you need to copy?");
scanf("%d",&n);
COPY(ar1, ar2, n);
puts("Now let's see what would happen when we puts(ar1)");
puts("Puts(ar1)");
puts(ar1);
return 0;
}
void COPY(char *ar1, char *ar2, int n)
{
int i;
for (i = 0; i < n; i++)
*(ar1 + i) = *(ar2 + i);
*(ar1+i) = '\0';//加一句 就对了,你的字符串没有结束标识
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询