指针问题,分配

时间:2017-03-11 05:46:12

标签: c pointers

我正在尝试为我的函数为变量root赋值,但似乎没有效果。我不明白这个问题。

hw7.c:155:7:警告:赋值从整数中生成没有强制转换的指针[默认启用]   root =加载(& fp,size);

此代码创建一个双向链表,然后在插入排序中添加,然后删除

我得到的问题位于此行的主要位置 root =加载(& fp,size);

struct node * LOAD(FILE *stream, int size){
     int x;
     char *tempLine = NULL;
     size_t length = 0;
     const char delim[] = " ";
     char *token;
     char *name;
     char *ADCheck;
     struct node *root;

     rewind(stream);


     for(x = 0; x < size; x++){
          getline(&tempLine, &length, stream);
          token = strtok(tempLine, delim);
          name = token;
          token = strtok(NULL, delim);   
          ADCheck = token;   

          if(( strcasecmp(ADCheck, "a") == 0) ) {

               root = insertNode(root, name);
          }else{
               root = delete(root, name);
          } 
          length = 0;
          tempLine = NULL;
     }
     free(tempLine);
     return root;
}


int main(){
    FILE *fp;
    int size;
    struct node *root;

    root = NULL;

    fp = fopen("hw7.data", "r");

    size = SCAN(&fp);

    root = LOAD(&fp, size);

    Free(root);

    return 0;
}

1 个答案:

答案 0 :(得分:2)

我认为有两个错误。正如经常发生的那样,一个人掩饰另一个人。

struct node * LOAD(FILE *stream, int size){

定义LOAD,全部大写。当你试着打电话时,

root = Load(&fp, size);

您使用的是Load,这是未声明的。因为它是未声明的,所以C编译器假定它返回int。由于root被定义为指针,因此您将获得 int-assigned-to-pointer 警告。

修复后,您将收到其他错误。 LOAD的第一个参数(您重命名Load)是FILE *。您的变量fpFILE *,但您传递的地址为&fp FILE **。因此,您将收到类型错误。

如果有指针,则传递指针。它和其他任何东西都是一个价值;把它作为一个传递。除非您希望函数更改指针指向的位置,否则不要将指针的地址传递给函数。