冲突的类型在C指针变量(错误) [英] Conflicting types for pointer variable in C (Error)

查看:126
本文介绍了冲突的类型在C指针变量(错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(1)

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

int a = 10, b = 20 , c = 30, d, e, *pa, *pb, *pc;

d= 10;
e= 100;

pa = &a;
pb = &b;

int main()
{
    printf("%i, %i, %i, %i", pa, pb, d, e);
    return 0;
}

(2)

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

int a = 10, b = 20 , c = 30, d, e, *pa, *pb, *pc;

d= 10;
e= 100;

int main()
{
    pa = &a;
    pb = &b;

    printf("%i, %i, %i, %i", pa, pb, d, e);
    return 0;
}

为什么我得到一个错误,当我初始化指针变量PA和PB之外的主要功能的(1)?当PA和PB是主要的功能在里面工作完全正常的(2)。我为什么能正常初始化变量的主要功能(D,E)之外,但没有指针变量?

Why do I get an error when I initialize the pointer variables pa and pb outside of the main function (1)? When pa and pb are inside the main function it works perfectly fine (2). Why can I initialize normal variables outside of the main function (d,e) but not pointer variables?

该错误消息我$ C $个cblocks得到的是:冲突的文件类型PA。每年的previous声明在这里:4号线

The error message I get in CodeBlocks is: Conflicting file types for pa. Previous declaration of pa was here: line 4.

推荐答案

可执行code一定要去里面的功能。这是因为一个C程序的执行流程与呼叫开始的main()

Executable code must go inside functions. This is because the flow of execution of a C program begins with calling main().

线,如 int类型的= 10; 被称为声明的,他们可能被视为发生的节目开始之前。通常情况下,编译器将生成所有的全局变量数据的阵营,并加载了当它启动程序。

Lines like int a = 10; are called declarations and they may be considered to "happen" before the program starts. Typically the compiler will generate a bloc of all of the global variables' data and load that up when it is starting up your program.

当你写 D = 10; 在文件范围内,这被视为 INT D = 10; 。由于声明没有在文件范围内允许的,它不是一个赋值语句。编译器认为这是你的意思是写一个声明 INT ,但希望留出来,以节省打字。

When you write d = 10; at file scope, this is treated as int d = 10; . Since statements are not permitted at file scope, it is not an assignment statement. The compiler thinks it is a declaration where you meant to write int but wanted to save typing by leaving it out.

这就是所谓的隐式int 的C89和有它,虽然它是在C99中删除。

This is called implicit int and C89 had it, although it was removed in C99.

所以,当你写 PA =安培; A; ,隐式int使得它 INT PA =安培; A; ,你会得到一个错误,因为你已经声明每年类型为int * ,然后再次与 INT ,这是不同的类型。

So when you write pa = &a;, implicit int makes it int pa = &a; and you get an error because you have declared pa with type int * and then again with int, which are different types.

不过变量声明为 INT ,然后重新声明为 INT (像你一样的 D 电子)是好的,只要第一个没有一个初始化。这就是所谓的暂定定义

However declaring a variable as int and then re-declaring it as int (as you did for d and e) is fine so long as the first one didn't have an initializer. This is called tentative definition.

要避免这一切,确保任何code即并不意味着要声明的推移函数内。你可以在文件范围写:

To avoid all this, make sure that any code that isn't meant to be a declaration goes inside a function. You could write at file scope:

int a = 5;
int *pa = &a;

等。

此外,的printf(%I,%I,%I,%I,PA,PB,D,E); 引起的未定义行为。在%I 说明必须匹配到 INT 。为了解决这个问题,你要么需要通过(INT)PA 而不是每年等,或使用%p 符。

Also, printf("%i, %i, %i, %i", pa, pb, d, e); causes undefined behaviour. The %i specifier must match to an int. To fix this you either need to pass (int)pa instead of pa, etc., or use the %p specifier.

这篇关于冲突的类型在C指针变量(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆