Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 字符串在那个里,但并没有打印出来_C_Gcc_Stack_Printf - Fatal编程技术网

C 字符串在那个里,但并没有打印出来

C 字符串在那个里,但并没有打印出来,c,gcc,stack,printf,C,Gcc,Stack,Printf,我想我有一个很有趣的问题。我正在尝试用C实现堆栈。这是我的头文件和实现文件(我只实现了Push): my.h: 我的c部分: 最后,这是我的主要观点。c: int main(int argc, char const *argv[]) { struct stackNode* stackHead=malloc(sizeof(struct stackNode)); BizarreNumber_t a={"sa",1,1}; BizarreNumber_t b={"as",2,

我想我有一个很有趣的问题。我正在尝试用C实现堆栈。这是我的头文件和实现文件(我只实现了Push):

my.h:

我的c部分:

最后,这是我的主要观点。c:

int main(int argc, char const *argv[]) {
    struct stackNode* stackHead=malloc(sizeof(struct stackNode));

    BizarreNumber_t a={"sa",1,1};
    BizarreNumber_t b={"as",2,2};

    stackHead->data=a;
    stackHead->nextPtr=NULL;

    printf("%s\n", stackHead->data.type);
    push(stackHead,b);

    printf("%s\n", stackHead->nextPtr->data.type);//HERE!!!
    return 0;
}
总而言之,我写的“HERE!!!”一行没有正确地给出真实的输出。实际上它什么也没给。有趣的是,whis给出了正确的输出:

printf("%c\n", stackHead->nextPtr->data.type[0]);

我试着打印出字符串中的每个字符,结果显示字符串很好。但是我看不见。为什么会这样?

stackHead
是在
main()
函数中创建的局部变量。对
push()
方法中的
stackHead
所做的任何修改或更改都不会影响
main()
方法,因为它只是按值调用

stackHead
的地址传递给
push()
方法

push(&stackHead,b); /* pass the address of stackhead */
并相应更改
push()
的定义

 void push(struct stackNode **topPtr, BizarreNumber_t info){
        struct stackNode *newTop = malloc(sizeof(struct stackNode));
        newTop->data = info;
        newTop->nextPtr = *topPtr; /*new node next make it to head node */
        *topPtr=newTop; /*update the head node */
 }

这并没有影响。我已经发送了一个指针,虽然我尝试了你的方法,但它仍然没有出现。我想保留可以通过以下方式看到的字符串:printf(“%c”,str[0]);是的,它应该是“sa”,我在LinuxMint中使用gcc编译器。这是我的‘gcc-v’:‘gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.9)’@H.Durmaz编译器不是这方面的问题。我修改了我的答案。检查一下。非常感谢,现在很好用!但我可以问一下,一开始它对你有用吗?我的意思是我的代码应该可以工作,因为我也在发送指针?不是指针指向指针而是指针本身?
push(&stackHead,b); /* pass the address of stackhead */
 void push(struct stackNode **topPtr, BizarreNumber_t info){
        struct stackNode *newTop = malloc(sizeof(struct stackNode));
        newTop->data = info;
        newTop->nextPtr = *topPtr; /*new node next make it to head node */
        *topPtr=newTop; /*update the head node */
 }