如何在 C 中将 char 附加到动态分配内存的字符串?
how can I append a char to a string allocating memory dynamically in C?
我写了这段代码,但在字符串的开头插入了垃圾:
void append(char *s, char c) {
int len = strlen(s);
s[len] = c;
s[len + 1] = '[=10=]';
}
int main(void) {
char c, *s;
int i = 0;
s = malloc(sizeof(char));
while ((c = getchar()) != '\n') {
i++;
s = realloc(s, i * sizeof(char));
append(s, c);
}
printf("\n%s",s);
}
我该怎么做?
当您调用 strlen
时,它会搜索 '[=12=]'
字符来结束字符串。您的字符串中没有此字符,因为 strlen
的行为是不可预测的。
您的 append
功能确实不错。
另外,一件小事,您需要将 return 0;
添加到您的主要功能。而 i
应该从 1 开始,而不是 0。
它应该是这样的:
int main(void){
char *s;
size_t i = 1;
s = malloc (i * sizeof(char));//Just for fun. The i is not needed.
if(s == NULL) {
fprintf(stderr, "Coul'd not allocate enough memory");
return 1;
}
s[0] = '[=10=]';
for(char c = getchar(); c != '\n' && c != EOF; c = getchar()) {//it is not needed in this case to store the result as an int.
i++;
s = realloc (s,i * sizeof(char) );
if(s == NULL) {
fprintf(stderr, "Coul'd not allocate enough memory");
return 1;
}
append (s,c);
}
printf("%s\n",s);
return 0;
}
感谢帮助我改进代码的评论(以及我的英语)。我并不完美:)
您的代码中存在多个问题:
- 你迭代直到从标准输入流中读取一个换行符 (
'\n'
)。如果在您读取换行符之前出现文件末尾,这将导致无限循环,如果您从空文件重定向标准输入,则会发生这种情况。
c
应定义为 int
以便您可以正确测试 EOF
。
s
应始终以 null 终止,您必须在 malloc()
之后将第一个字节设置为 '[=16=]'
,因为此函数不会初始化它分配的内存。
i
应初始化为 1
,因此第一个 realloc()
将数组扩展 1 等。按照编码,您的数组太短一个字节,无法容纳额外的字符。
- 你应该检查内存分配失败。
- 为了良好的风格,您应该在退出程序之前释放分配的内存
main()
应该 return 一个 int
,最好是 0
才能成功。
这是更正后的版本:
#include <stdio.h>
#include <stdlib.h>
/* append a character to a string, assuming s points to an array with enough space */
void append(char *s, char c) {
size_t len = strlen(s);
s[len] = c;
s[len + 1] = '[=10=]';
}
int main(void) {
int c;
char *s;
size_t i = 1;
s = malloc(i * sizeof(char));
if (s == NULL) {
printf("memory allocation failure\n");
return 1;
}
*s = '[=10=]';
while ((c = getchar()) != EOF && c != '\n') {
i++;
s = realloc(s, i * sizeof(char));
if (s == NULL) {
printf("memory allocation failure\n");
return 1;
}
append(s, c);
}
printf("%s\n", s);
free(s);
return 0;
}
内部realloc
需要多分配一个元素(用于尾部[=12=]
)并且你必须在开始循环之前初始化s[0] = '[=13=]'
。
顺便说一句,你可以用 strcat()
替换你的 append
或者写成
size_t i = 0;
s = malloc(1);
/* TODO: check for s != NULL */
while ((c = getchar()) != '\n') {
s[i] = c;
i++;
s = realloc(s, i + 1);
/* TODO: check for s != NULL */
}
s[i] = '[=10=]';
我写了这段代码,但在字符串的开头插入了垃圾:
void append(char *s, char c) {
int len = strlen(s);
s[len] = c;
s[len + 1] = '[=10=]';
}
int main(void) {
char c, *s;
int i = 0;
s = malloc(sizeof(char));
while ((c = getchar()) != '\n') {
i++;
s = realloc(s, i * sizeof(char));
append(s, c);
}
printf("\n%s",s);
}
我该怎么做?
当您调用 strlen
时,它会搜索 '[=12=]'
字符来结束字符串。您的字符串中没有此字符,因为 strlen
的行为是不可预测的。
您的 append
功能确实不错。
另外,一件小事,您需要将 return 0;
添加到您的主要功能。而 i
应该从 1 开始,而不是 0。
它应该是这样的:
int main(void){
char *s;
size_t i = 1;
s = malloc (i * sizeof(char));//Just for fun. The i is not needed.
if(s == NULL) {
fprintf(stderr, "Coul'd not allocate enough memory");
return 1;
}
s[0] = '[=10=]';
for(char c = getchar(); c != '\n' && c != EOF; c = getchar()) {//it is not needed in this case to store the result as an int.
i++;
s = realloc (s,i * sizeof(char) );
if(s == NULL) {
fprintf(stderr, "Coul'd not allocate enough memory");
return 1;
}
append (s,c);
}
printf("%s\n",s);
return 0;
}
感谢帮助我改进代码的评论(以及我的英语)。我并不完美:)
您的代码中存在多个问题:
- 你迭代直到从标准输入流中读取一个换行符 (
'\n'
)。如果在您读取换行符之前出现文件末尾,这将导致无限循环,如果您从空文件重定向标准输入,则会发生这种情况。 c
应定义为int
以便您可以正确测试EOF
。s
应始终以 null 终止,您必须在malloc()
之后将第一个字节设置为'[=16=]'
,因为此函数不会初始化它分配的内存。i
应初始化为1
,因此第一个realloc()
将数组扩展 1 等。按照编码,您的数组太短一个字节,无法容纳额外的字符。- 你应该检查内存分配失败。
- 为了良好的风格,您应该在退出程序之前释放分配的内存
main()
应该 return 一个int
,最好是0
才能成功。
这是更正后的版本:
#include <stdio.h>
#include <stdlib.h>
/* append a character to a string, assuming s points to an array with enough space */
void append(char *s, char c) {
size_t len = strlen(s);
s[len] = c;
s[len + 1] = '[=10=]';
}
int main(void) {
int c;
char *s;
size_t i = 1;
s = malloc(i * sizeof(char));
if (s == NULL) {
printf("memory allocation failure\n");
return 1;
}
*s = '[=10=]';
while ((c = getchar()) != EOF && c != '\n') {
i++;
s = realloc(s, i * sizeof(char));
if (s == NULL) {
printf("memory allocation failure\n");
return 1;
}
append(s, c);
}
printf("%s\n", s);
free(s);
return 0;
}
内部realloc
需要多分配一个元素(用于尾部[=12=]
)并且你必须在开始循环之前初始化s[0] = '[=13=]'
。
顺便说一句,你可以用 strcat()
替换你的 append
或者写成
size_t i = 0;
s = malloc(1);
/* TODO: check for s != NULL */
while ((c = getchar()) != '\n') {
s[i] = c;
i++;
s = realloc(s, i + 1);
/* TODO: check for s != NULL */
}
s[i] = '[=10=]';