警告:赋值时将整数指针,未作投 [英] Warning: assignment makes pointer from integer without a cast

查看:142
本文介绍了警告:赋值时将整数指针,未作投的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做从一个指针,然后它让我乳宁这样的警告(赋值时将整数指针,未作投)投。
这里的code:

I'm doing cast from a pointer then it keeps me runing this warning (assignment makes pointer from integer without a cast). here's the code:

#include<stdio.h>
#include<stdbool.h>



typedef int TipoChave;

typedef struct TipoRegistro {
  TipoChave Chave;
  /*outros componentes*/
} TipoRegistro;

typedef struct TipoPagina* TipoApontador;

typedef struct TipoPagina {
  int registros;
  TipoRegistro *r;
  TipoApontador *p;
} TipoPagina;

TipoApontador NovaSubArvore(int ordem){
    TipoApontador A;
    A=malloc(sizeof(TipoPagina));
    int i;
    A->registros=0;
    A->r=malloc((2*ordem)*sizeof(TipoRegistro));
    A->p=malloc((2*ordem+1)*sizeof(TipoPagina));
    for (i=0;i<(2*ordem+1);i++){
        A->p[i]=NULL;
        if(i!=2*ordem){
            A->r[i].Chave=0;
        }
    }
    return (A);
}

这是我主要的电话:

TipoApontador Raiz;

然后

Raiz=NovaSubArvore(ordem); //Warning happens here

如果我做的:

if (Raiz!=NULL)
    free(Raiz);

它运行一个invallid免费(奇怪,因为如果Raiz为NULL的免费不应该有运行。
谁能帮我这一问题,好吗?我认为这个警告,让我从解放了。

it runs a invallid free (strange, because if Raiz is NULL the free shouldn't had run. Can anyone help me with that problem, please? I think that this warning is the problem that keeps me from "freeing" too.

编辑:华林关于解决好问题。但是,如果我做免费2次运行一个无效的自由(我有一个做了免费出头,其他倍不是一个函数。如果我做了免费的如果(Raiz!= NULL)应该阻止从其他自由乳宁。但它不是

OK problem about waring solved. But if I do the free 2 times it runs a invalid free (I have a function that does a free somethings, other-times not. If I do the free the "if(Raiz!=NULL)" should block the other free from runing. but it isn't.

推荐答案

一个问题是的malloc()调用和你没有声明的事实的malloc()通过包括&LT;文件stdlib.h方式&gt;

One problem is the calls to malloc() and the fact that you've not declared malloc() by including <stdlib.h>.

在默认情况下,假定函数返回一个在$ INT 点$ P-C99 code - 在C99 code,你应该使用它之前声明的函数。

By default, functions are assumed to return an int in pre-C99 code — in C99 code, you're supposed to declare a function before using it.

您需要更多的警告选项进行编译。如果你使用GCC,我建议:

You need to compile with more warning options. If you use GCC, I recommend:

gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
    -Wold-style-definition ...

(),如的malloc()使用

这pretty太多确保你没有未申报的功能。根据你使用GCC的版本,你可能会得到默认启用或多或少的警告。一般情况下,新版本是fussier,虽然它不是这么简单。

This pretty much ensures that you don't have undeclared functions (such as malloc()) used. Depending on the version of GCC you use, you may get more or less warnings enabled by default. In general, newer versions are fussier, though it isn't quite that simple.

另一个问题似乎是,你有一个源文件,其中包含类型定义和功能定义,例如(在问题没有给予名称):

The other problem appears to be that you have a source file (name not given in the question) containing type definitions and function definitions such as:

typedef struct TipoPagina* TipoApontador;
typedef struct TipoPagina { ... } TipoPagina;

TipoApontador NovaSubArvore(int ordem) { ... }

在该文件中的类型是已知的。在你的主code,你有:

Within this file the types are known. In your main code, you have:

TipoApontador Raiz;

...

Raiz = NovaSubArvore(ordem); //Warning happens here

类型名称 TipoApontador 必须在此文件是已知的,但现在看来,你的code不包括 NovaSubArvore的声明( )

The type name TipoApontador must be known in this file, but it appears that your code does not include a declaration for NovaSubArvore().

有关类型和将要在多个源文件也可以使用的功能,应该有一个头限定的种类和声明的功能。头应能在两个定义函数的源文件,并在使用的类型和功能的源文件中使用。

For types and functions that are going to be used in multiple source files, there should be a header defining the types and declaring the functions. The header should be used in both the source file that defines the functions and in the source files that use the types and functions.

例如,标题可能是 tipopagina.h

#ifndef TIPOPAGINA_H_INCLUDED
#define TIPOPAGINA_H_INCLUDED

typedef int TipoChave;

typedef struct TipoRegistro {
  TipoChave Chave;
  /*outros componentes*/
} TipoRegistro;

typedef struct TipoPagina* TipoApontador;

typedef struct TipoPagina {
  int registros;
  TipoRegistro *r;
  TipoApontador *p;
} TipoPagina;

extern TipoApontador NovaSubArvore(int ordem);

#endif /* TIPOPAGINA_H_INCLUDED */

头警卫很重要;他们避免重新定义类型(但C11在的typedef 取值处理重新定义比任何C99 C89或更灵活)的问题。使用的extern的之前函数名不是绝对必要的,虽然我preFER看到它 - 如果只为对称的<一个href=\"http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/\"><$c$c>extern这必须是在头声明的任何变量之前present。(如果有的话 - 全局变量应尽可能避免)

The header guards are important; they avoid problems with redefining types (though C11 has more flexibility than either C99 or C89 in handling redefinitions of typedefs). The use of extern before the function name is not strictly necessary, though I prefer to see it — if only for symmetry with the extern that must be present before any variables that are declared in the header (if there are any — global variables should be avoided whenever possible).

然后实现文件 tipopagina.c 可能会启动:

Then the implementation file tipopagina.c might start:

#include "tipopagina.h"
#include <stdlib.h>

TipoApontador NovaSubArvore(int ordem)
{
    TipoApontador A = malloc(sizeof(TipoPagina));
    ...
    return (A);
}

有一个很好的理由以把 tipopagina.h 头第一;它保证了报头可以在它自己的(这是很重要的)一起使用。

There's a good reason for putting the tipopagina.h header first; it ensures that the header can be used on its own (which is important).

主要code还包括 tipopagina.h ,而且由于功能 NovaSubArvore()声明在标题,您避免编译器警告。

The main code also includes tipopagina.h, and because the function NovaSubArvore() is declared in the header, you avoid the compiler warning.

这篇关于警告:赋值时将整数指针,未作投的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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