简体   繁体   English

第二个printf无法正常工作,需要帮​​助。 (C lang)

[英]The second printf isn't working, need help. (C lang)

What is the problem here? 这里有什么问题? It prints the first printf but it doesn't print the next statement Help appreciated. 它打印第一个printf,但不打印下一个语句。

Code: 码:

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

struct node
{
  int Data;
  struct node *Link;
};

//close struct
void insertAtEnd(struct node *Itcstd, int iData)
{
  while (Itcstd->Link != NULL )
    Itcstd = Itcstd->Link;

  Itcstd->Link = (struct node*) malloc(sizeof(struct node));
  Itcstd->Link->Link = NULL;
  Itcstd->Link->Data = iData;
} //close insertAtEnd

int main()
{
  struct node *EHead;

  EHead = (struct node*) malloc(sizeof(struct node));
  EHead->Link = NULL;
  EHead->Data = 4;

  printf("EHead link: %p Ehead Data: %d\n", EHead->Link, EHead->Data);
  insertAtEnd(EHead, 10);
  printf("EHead link: %p Ehead Data: %d\n", EHead->Link, EHead->Data);

  return 0;
} //close main

Actually it's a good code, it works for me perfectly. 实际上,这是一个很好的代码,它对我来说非常完美。 (I can see two lines.) (我可以看到两行。)

The code runs fine, without any memory issues. 代码运行正常,没有任何内存问题。

The only point to mention is that the conversion specifier for printf() ing a pointer ( %p ) is defined for void * only. 唯一要提及的是,仅针对void *定义了用于将printf()用作指针( %p )的转换说明符。 Having told the compiler to be pedantic (option -pedantic for gcc) it would have notified you about this. 告诉编译器是-pedantic对于gcc,选项-pedantic )将通知您。

So this 所以这

printf("EHead link: %p Ehead Data: %d\n", EHead->Link, EHead->Data);

shall be this 应该是这个

printf("EHead link: %p Ehead Data: %d\n", (void *) EHead->Link, EHead->Data);

This, btw, is one of the rare cases in C where casting can not be avoided. 顺便说一句,这是C语言中无法避免强制转换的罕见情况之一。

code is elegant , and is working fine for me i checked it on windows (codeblocks MinGW gcc 4.7 , turbo c++) linux (gcc 4.8.1 ) and it works perfectly . 代码优雅,并且对我来说工作很好,我在Windows(代码块MinGW gcc 4.7,turbo c + +)linux(gcc 4.8.1)上对其进行了检查,并完美地工作了。 which environment are you using ? 您正在使用哪个环境?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM