【问题标题】:Newbie to C, Pointers giving error ('p1' undeclared first use in this function) [closed]C新手,指针给出错误('p1'未声明在此函数中首次使用)[关闭]
【发布时间】:2015-04-07 20:55:18
【问题描述】:

我刚刚开始指导,我有最简单的练习我希望我不只是厚,它是一个工具,它是 Dev C++。

这是我的代码(应该简单地打印出指针'p1'的内存地址:

Lab7Ex1()
{
    int integer = 100;
    char character = 'D';
    float pies = 3.14;

    p1 = &integer;
    p2 = &character;
    p3 = &pies;

    printf("\nAddress 0x%x", p1);
}

但是尽管一遍又一遍地阅读了关于这个的实验室笔记,它仍然给我一个错误,告诉我当我看它时我没有声明'p1'并且'p1'就像就在那里......声明了.

p1 = &integer;

【问题讨论】:

  • 所有反对票的人应该早点解释原因。这可能是您的问题毫无意义的结果。听起来您有一个问题,您已经有了答案,并且出于未知原因想问它。因此投反对票。
  • 您的最终评论。不,你没有用int p1 = &integer; 修复它。它必须是@abelenky 回答的int *p1 = &integer;
  • 老实说,我认为它会帮助其他新手,但如果您要求,我可以将其删除。

标签: c pointers


【解决方案1】:

看,你知道如何声明整数、字符和浮点数,因为你已经这样做了。

你也必须声明指针:

int*   p1;   // p1 is a Pointer-to-Integer. 
char*  p2;  // p2 is a Pointer-to-Char.
float* p3; // p3 is a Pointer-to-Float. 

p1 = &integer;  // Set p1 to point to a real object.
p2 = &character;// Set p2 to point to a real object.
p3 = &pies;     // Set p3 to point to a real object.

编辑

OP 询问:

int p1 = &integer;  // WRONG: This only declares an int, not a Pointer-to-Int.

* 对制作指针非常重要。

如果你愿意,你可以这样做:

int*   p1 = &integer;  // Create a pointer, and set what it points to on one-line.
char*  p2 = &character;
float* p3 = &pies;

【讨论】:

  • 谢谢,我认为声明你所拥有的指针比:int p1 = &integer; int p2 = &字符; int p3 = &pies;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 2018-10-03
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多