c串联两个字符串的程序
我正在尝试在不使用任何功能的情况下将两个字符串与指针连接在一起。当我输入两个字符串时,例如第一个hello hello,然后世界输出是
Hello
World
而不是Helloworld.任何帮助。
#include<stdio.h>
#include<stdlib.h>
int main(){
char *s=(char *)malloc(sizeof(char *));
char *s2=(char *)malloc(sizeof(char *));
fgets(s,10,stdin);
fgets(s2,10,stdin);
int i=0;
while(*s){
s++;
i++;
}
while(*s2!='\0'){
*s=*s2;
s++;
s2++;
i++;
}
*s='\0';
printf("%s",s-i);
}
I'm trying to concatenate two strings together with pointers without using any of the functions.When I enter the two strings for example first hello and then world the output is
hello
world
and not helloworld.Any help would be appreciated.
#include<stdio.h>
#include<stdlib.h>
int main(){
char *s=(char *)malloc(sizeof(char *));
char *s2=(char *)malloc(sizeof(char *));
fgets(s,10,stdin);
fgets(s2,10,stdin);
int i=0;
while(*s){
s++;
i++;
}
while(*s2!='\0'){
*s=*s2;
s++;
s2++;
i++;
}
*s='\0';
printf("%s",s-i);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序具有不确定的行为,因为您没有为输入字符串分配内存。
您仅分配两个指针的内存(
sizeof(char *)
)。您需要分配足够大的内存,其中可以包含输入的字符串及其在第一个字符阵列中的串联。
函数
fgets
可以将新的行字符'\ n'附加到输入的字符串。您需要覆盖它。另外,您不应该更改原始指针,因为您需要使用它们来释放分配的内存。
并考虑到结果字符串至少包含
11
字符,包括终止零字符'\ 0'
而不是10
字符,如果您将要输入“ Hello”
和“ World”
并将其置于它们。尽管总的来说,最好保留13
字符,如果输入的字符串将不包含新的行字符。该程序可以看起来像以下方式
可能是程序输出的
方式,而不是这些行
,您可以编写的
阵列为零初始化,以保持空字符串,以防何时打断fgets的呼叫。
The program has undefined behavior because you did not allocate memory for entered strings.
You only allocated memory for two pointers (
sizeof(char *)
).You need to allocate memory large enough that can contains entered strings and their concatenation in the first character array.
The function
fgets
can append the new line character '\n' to an entered string. You need to overwrite it.Also you should not change the original pointers because you need to use them to free the allocated memory.
And take into account that the result string will contain at least
11
characters including the terminating zero character'\0'
instead of10
characters if you are going to enter"hello"
and"world"
and concatenate them. Though in general it is better to reserve13
characters if the entered strings will not contain the new line character.The program can look for example the following way
The program output might be
Instead of these lines
you could write
The arrays are zero initialized to keep empty strings in case when calls of fgets will be interrupted.
fgets()
读取到文件的末尾或行末尾,但在读取的数据中包含行的末尾。因此,在您的情况下,您还将字符串与新线条连接在一起。
在另一个注意下
是为
sizeof(char*)
字符的内存分配,即指针的大小,而不是x字符数。同样,由于您将一个10个字符串串联另一个字符串t另一个字符串,因此需要分配该字符串才能至少保持(20个chars + 1个null)。
fgets()
reads to the end of the file or end of line, but includes the end of the line in the data read.So in your case, you are also concatenating the string with the new lines.
On a different note your statement
char *s=(char *)malloc(sizeof(char *));
is allocating memory for
sizeof(char*)
characters i.e.: the size of a pointer, not X number of characters.Also since you are concatenating one 10 character string t another, the string needs to be allocated to hold at least that (20 chars + 1 null).