C - getchar 和动态创建的数组

标签 c malloc dynamic-arrays getchar calloc

我正在使用 C 语言开发 vector 类,但在使用 getchar() 时遇到了一些问题。虽然我知道 getchar() 将返回整数/无符号字符,但当将 getchar() 中的值分配给 c 中动态创建的数组时,它的行为似乎有所不同。我正在测试一些代码来表明:

#include <stdio.h>
#include <stdlib.h>
int main(){ 
    char **ptr;
    ptr=calloc(10,sizeof(char));
    unsigned char c = getchar();
    printf("%c\n", c); //this prints a normal value
    ptr[0] = &c;  //dereference the value to assign it to the dynamic array
    printf("%s",ptr[0]); // This prints an odd value
}

这是此代码的输出结果:

[jake@shell] hw4 $ ./hw4Test
g
g
g▒▒[jake@shell] hw4 $ ./hw4Test
j
j
j▒~[jake@shell] hw4 $ ./hw4Test
q
q
q▒[jake@shell] hw4 $ ./hw4Test
r
r
r0▒

当我尝试将此代码实现到我的代码中时,far ,它的行为更加不稳定:

[jake@shell] hw4 $ ./hw4

Enter the Name : kl
flag
flag
ptr[2] = (null)
ptr[2] = (null)
[jake@shell] hw4 $ ./hw4

Enter the Name : jkla
flag
flag
flag
flag
ptr[4] = (null)
ptr[4] = (null)
ptr[4] = (null)
ptr[4] = (null)
*** glibc detected *** ./hw4: free(): invalid next size (fast):                     0x0000000000705010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x33cae75e66]
/lib64/libc.so.6[0x33cae789b3]
./hw4[0x400691]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x33cae1ed5d]
./hw4[0x400529]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:13 7473045                                    /home/jlongar/Desktop/cStuff/hw4/hw4
00600000-00601000 rw-p 00000000 00:13 7473045                            /home/jlongar/Desktop/cStuff/hw4/hw4
00705000-00726000 rw-p 00000000 00:00 0                                      [heap]
33ca600000-33ca620000 r-xp 00000000 fd:00 130889                         /lib64/ld-2.12.so
33ca81f000-33ca820000 r--p 0001f000 fd:00 130889                         /lib64/ld-2.12.so
33ca820000-33ca821000 rw-p 00020000 fd:00 130889                         /lib64/ld-2.12.so
33ca821000-33ca822000 rw-p 00000000 00:00 0
33cae00000-33caf8a000 r-xp 00000000 fd:00 130934                         /lib64/libc-2.12.so
33caf8a000-33cb18a000 ---p 0018a000 fd:00 130934                         /lib64/libc-2.12.so
33cb18a000-33cb18e000 r--p 0018a000 fd:00 130934                         /lib64/libc-2.12.so
33cb18e000-33cb18f000 rw-p 0018e000 fd:00 130934                         /lib64/libc-2.12.so
33cb18f000-33cb194000 rw-p 00000000 00:00 0
33cca00000-33cca16000 r-xp 00000000 fd:00 131173                         /lib64/libgcc_s-4.4.7-20120601.so.1
33cca16000-33ccc15000 ---p 00016000 fd:00 131173                         /lib64/libgcc_s-4.4.7-20120601.so.1
33ccc15000-33ccc16000 rw-p 00015000 fd:00 131173                         /lib64/libgcc_s-4.4.7-20120601.so.1
7f816bdde000-7f816bde1000 rw-p 00000000 00:00 0
7f816bdf5000-7f816bdf9000 rw-p 00000000 00:00 0
7fff85e61000-7fff85e76000 rw-p 00000000 00:00 0                          [stack]
7fff85f62000-7fff85f63000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                      [vsyscall]
Aborted (core dumped)

如何使 getchar 与 C 中动态创建的数组完美配合?

最佳答案

如果类型正确,它就可以工作。

#include <stdio.h>
#include <stdlib.h>

int main(){
    char *ptr = calloc(10, 1);  // sizeof(char) is ALWAYS 1
    unsigned char c = getchar();
    printf("%c\n", c); //this prints a normal value
    ptr[0] = c;  //dereference the value to assign it to the dynamic array
    printf("%c", ptr[0]); // This prints an odd value
    free(ptr);
}

编程的第一条规则,不要让事情过于复杂。当您只能使用一级间接寻址 - 指针 (*) 时,为什么要使用指向指针 (**) 的指针。

编程第二条规则,正确匹配类型。查看与上面代码的差异。另请注意,在 printf 格式字符串中,ptr[0] 的内容是 char,而不是 char*。

关于C - getchar 和动态创建的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35709663/

相关文章:

c# - 如何将结构向量从 Rust 返回到 C#?

c++ - 动态数组输出问题

将 uint32 浮点表示形式转换为 uint8

c - 在c中克隆avl树时出现总线错误

c - 如何打印指针指向的字节的二进制内容?

c - malloc 时表达式必须是可修改的左值

安卓动态数组

c - C语言的一段代码应用尾递归?

c - 一次打破许多 for 循环的巧妙方法是什么?

c - 为什么它的 c 指针在这里不起作用?