通过使用Dynamic中的指针将字符串复制到另一个字符串中
正在尝试将一个字符串复制到另一个字符串。作为一名基础学习者,我尽了最大努力获得输出,但在课程结束时(第1点),我的逻辑不正常。请参考我在下面给出的输入和输出,以获得清晰的想法 这个程序复制一个字符串通过使用Dynamic中的指针将字符串复制到另一个字符串中,c,string,pointers,dynamic,C,String,Pointers,Dynamic,正在尝试将一个字符串复制到另一个字符串。作为一名基础学习者,我尽了最大努力获得输出,但在课程结束时(第1点),我的逻辑不正常。请参考我在下面给出的输入和输出,以获得清晰的想法 这个程序复制一个字符串 #include<stdio.h> #include<stdlib.h> int main() { int n1,n2,loc; char *p1, *p2; printf("Enter size of p1\n"); scanf("%d"
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n1,n2,loc;
char *p1, *p2;
printf("Enter size of p1\n");
scanf("%d", &n1);
p1 = (char*) malloc (n1 * sizeof(char));
printf("Enter the P1 String\n");
fflush(stdin);
gets(p1);
printf("\nEnter the size of p2\n");
scanf("%d", &n2);
p2 = (char*) malloc (n2 * sizeof(char));
printf("Enter the P2 String\n");
fflush(stdin);
gets(p2);
printf("\nEnter the Location to copy\n");
scanf("%d", &loc);
for(int i=loc-1;i<=n1;i++) //point 1
{
*(p1+i) = *(p1+i)+n2;
}
for(int i=0;i<=n2;i++)
{
*(p2+i) = *(p1+i)+loc;
}
printf("\n Final copy is\n");
printf("%d",p1);
free(p1);
free(p2);
return 0;
}
实际值:
Input:
google
microsoft
output:
Goomicrosoftgle
Input:
google
microsoft
output:
[Some garbage values including given string]
- 首先,不要按原样使用
,因为它已经被弃用了gets()
和p1
在内存中动态分配,它们各自的大小分别为p2
和n1
。因此,当您将更多字符从n2
添加到p2
中时,需要增加输出的大小,否则它们将无法放入该内存空间p1
- 对于使用
的字符串输入,分配的内存必须比实际长度多一个,因为在末尾插入了一个空终止字符scanf
- 在最后的print语句中,您使用的是整数类型的格式说明符
,因此在打印整个字符串时应使用%d
%s
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1, n2, loc;
char *p1, *p2, *output;
printf("Enter size of p1: ");
scanf("%d", &n1);
p1 = malloc((n1 + 1) * sizeof(char));
printf("Enter the P1 String: ");
scanf("%s", p1);
printf("\nEnter the size of p2: ");
scanf("%d", &n2);
p2 = malloc((n2 + 1) * sizeof(char));
printf("Enter the P2 String: ");
scanf("%s", p2);
printf("\nEnter the Location to copy: ");
scanf("%d", &loc);
output = malloc((n1 + n2 + 1) * sizeof(char));
for (int i = 0; i < loc; i++)
*(output + i) = *(p1 + i);
for (int i = loc - 1; i <= n1; i++)
*(output + i + n2) = *(p1 + i);
for (int i = 0; i < n2; i++)
*(output + i + loc) = *(p2 + i);
printf("\nFinal copy is: ");
printf("%s\n", output);
free(p1);
free(p2);
free(output);
return 0;
}
下面是使用动态方法复制字符串的正确逻辑
output=malloc((n1+n2+1)*sizeof(char))
//在为两种大小的字符串分配内存并为NULL添加内存之后。p2=malloc(n2*sizeof(char))代码>(一个太少)nul终止符在哪里?(对于p1
可能也是如此,但您不显示输入的大小。输出
(例如,n1+n2+1
chars必需)。混淆可能是由于在提示符中混合了“大小”
对于“长度”
。sizeof(char)
总是1
,应该省略。(释放分配的内容做得很好)总是(总是)验证每次使用输入函数的返回(尤其是对于scanf
)代码@Rakibul仍然存在一些问题。添加了realloc函数以添加更多内存,但最终输出仍然不完美。请在代码框中找到提到的输出。@DavidC.Rankin感谢您的更正,我在编写此答案时很匆忙,我只是错过了空终止字符
。现在,我已经编辑了answer@Raki这使它更好。回答很好。@RakibulIslam抱歉,我试过使用在线编译器,现在它可以工作了,但我使用代码块学习C。在我的编译器中,输出是不正确的。在我的编译器中,输出就像“最终副本是:gomicrosoft=”。另一点是每次打印“gomicrosoft”后都会给出不同的输出.g:gomicrosoftM/gomicrosoft=/gomicrosoftW.我的编译器有问题吗?如果有问题,我应该怎么做来纠正它。fflush(stdin);
被C标准定义为未定义的行为,请参见,例如,有一个不符合标准的编译器允许它。
Enter size of p1: 6
Enter the P1 String: google
Enter the size of p2: 9
Enter the P2 String: microsoft
Enter the Location to copy: 3
Final copy is: goomicrosoftgle