2017-09-14 54 views
-2
#include <stdio.h> 

#include <stdlib.h> 

static void get_string(char *ac) 
{ 
    ac = (char *)malloc (1 * sizeof (char)); 
    printf("Enter the string to count vowels and consonants in a string using pointers: "); 
    scanf("%s", ac); 
} 

main(int argc, char *argv[]) 
{ 
    char *ac = NULL; 
    get_string(ac); 
    printf("The Entered string is:%s", ac); 
    for(;;); 
} 

无法从函数堆栈中获取输入的字符串。返回null.Can任何人都可以帮助我进行调试?无法从函数堆栈中获取输入的字符串

+0

如果您只为** 1 **字符分配空间,那么您希望得到的字符串究竟是什么? – StoryTeller

回答

1

C中的函数参数是按值传递的。对被调用函数内部的参数所做的任何更改都不会反映到函数调用中提供的实际参数。

在你的情况,你改变ac本身(而不是它所指向的内存位置的内容),因此,这将需要一个指针-TO-ac

这就是说,

+0

那么我需要在代码中进行哪些修改? – jiju

+0

为你的输入malloc一个合理的长度,(256,说),在你的scanf()格式的字符串中使用[length-1]的宽度,然后从char_string()返回字符串作为char *或者使用double - 指针参数/参数允许分配的指针传回给调用者。最简单的就是返回它。 –

+0

哦 - 并在main()中打印后释放()char *。 –