C - getchar和动态创建的数组

问题描述:

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

#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 
} 

这里是从该代码输出得出:

[[email protected]] hw4 $ ./hw4Test 
g 
g 
g▒▒[[email protected]] hw4 $ ./hw4Test 
j 
j 
j▒~[[email protected]] hw4 $ ./hw4Test 
q 
q 
q▒[[email protected]] hw4 $ ./hw4Test 
r 
r 
r0▒ 

,当我试图实现这个代码到我的代码,以便far,它表现更加不规律:

[[email protected]] hw4 $ ./hw4 

Enter the Name : kl 
flag 
flag 
ptr[2] = (null) 
ptr[2] = (null) 
[[email protected]] 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动态创建的阵列中发挥很好?

+0

'getchar' ** **没有很好地发挥。这是你的负责任的类型。你为什么用指针指向char_? – Olaf

+0

而不是'char ** ptr; ptr = calloc(10,sizeof(char));',我会使用'char ** ptr; ptr = calloc(10,sizeof * ptr);''sizeof(char)'错误或者'ptr'类型错误'。 – chux

+0

似乎有些误解。 'C'没有'class'es。它具有“功能”。 – user3629249

如果你得到的类型正确,它的工作原理。

#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 *。

+0

_“匹配类型正确”_这就是为什么当使用'malloc'&co时,最好使用指针本身来推断类型的大小('calloc(10,sizeof * ptr);'保证是正确的) –

问题是最后printf()


PTR [0]代表指针焦炭,所以如果你想打印它是你必须取消对它的引用内容。

所以,你必须改变printf(“%s”,ptr [0]);printf(“%c”,* ptr [0])

你看,它是%c。这又是,因为ptr [0]指针字符,所以* ptr [0]char

最后,更改您的代码这一点,你会得到你想要的结果:

#include <stdio.h> 
#include <stdlib.h> 
int main(){ 
    char **ptr; 
    ptr=calloc(10,sizeof(char)); 
    char c = getchar(); 
    printf("%c\n", c); 
    ptr[0] = &c; 
    printf("%c",*ptr[0]); 
    return 0; 
}