[英]Print array with pointer in C
I have a pointer to an array of char
and I need to print the entire array (not one after the other but the entire array).我有一个指向char
数组的指针,我需要打印整个数组(不是一个接一个,而是整个数组)。 I have the error: zsh: segmentation fault
.我有错误: zsh: segmentation fault
。 I don't understand why.我不明白为什么。 My code take a text from stdin
and put the chars in an array.我的代码从stdin
获取文本并将字符放入数组中。 If the first array char_array
isn't big enough I put them in the second one called bigger_array
.如果第一个数组char_array
不够大,我会将它们放在名为bigger_array
的第二个bigger_array
。
int main() {
int c;
int index = 0;
size_t size = 100;
char *char_array = malloc(size * sizeof(char));
if (char_array != 0) {
while ((c = getchar()) != EOF && index < size) {
char_array[index] = c;
index++;
}
size_t newSize = size * 100;
char *bigger_array = realloc(char_array, newSize * sizeof(char));
if (bigger_array != 0) {
bigger_array = char_array;
while ((c = getchar()) != EOF && index < newSize) {
bigger_array[index] = c;
index++;
}
size = newSize;
}
}
printf(*char_array);
return 0;
}
bigger_array = char_array;
should be char_array = bigger_array;
应该是char_array = bigger_array;
. . Otherwise, the pointer to newly allocated buffer is overwritten to the pointer to old buffer and bad thing will happen.否则,指向新分配缓冲区的指针将被覆盖到指向旧缓冲区的指针,并且会发生不好的事情。printf(*char_array);
is bad because it is passing a integer (one character) where pointer is required.不好,因为它在需要指针的地方传递一个整数(一个字符)。 It should be printf("%.*s", index, char_array);
它应该是printf("%.*s", index, char_array);
. . Note that the length to print is specified and you don't need to add terminating null-character thanks to that.请注意,指定了要打印的长度,因此您无需添加终止空字符。There are multiple issues in your code:您的代码中有多个问题:
<stdio.h>
nor <stdlib.h>
你不包括<stdio.h>
和<stdlib.h>
char_array
and store big_array
to char_array
so char_array
points to the bigger allocated array.您应该释放char_array
并将big_array
存储到char_array
以便char_array
指向更大的分配数组。printf(*char_array)
has undefined behavior for multiple reasons. printf(*char_array)
由于多种原因具有未定义的行为。 You can print the characters with fwrite(char_array, 1, index, stdout)
or with printf("%.*s\\n", (int)index, char_array)
.您可以使用fwrite(char_array, 1, index, stdout)
或printf("%.*s\\n", (int)index, char_array)
。Here is a modified version:这是一个修改后的版本:
#include <stdio.h>
#include <stdlib.h>
int main() {
int c;
size_t index = 0;
size_t size = 100;
char *char_array = malloc(size);
if (char_array != NULL) {
while ((c = getchar()) != EOF) {
if (index == size) {
size_t new_size = size + size / 2 + size / 8; /* increase by the golden ratio */
char *new_array = realloc(char_array, new_size);
if (new_array == NULL) {
fprintf(stderr, "out of memory\n");
free(char_array);
return 1;
}
size = new_size;
char_array = new_array;
}
char_array[index++] = c;
}
printf("%.*s\n", (int)index, char_array);
}
return 0;
}
Considere this as a comment ^^将此视为评论^^
I advise you to use flags for warning with your complier.我建议你使用标志来警告你的编译器。 for exemple for gcc
flags -W -Wextra -Wshadow
are good to use and prevent lost of time.例如gcc
标志-W -Wextra -Wshadow
很好用,可以防止浪费时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.