指针问题,分配

问题描述 投票:-2回答:1

我试图为我的函数赋值变量root,似乎没有工作。我不明白这个问题。

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

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

我得到的问题是位于此行的主要根=加载(&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;
}
c pointers
1个回答
2
投票

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

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

这定义了LOAD,都是大写的。当你试着打电话的时候

root = Load(&fp, size);

你使用未宣布的Load。因为它未声明,C编译器假定它返回int。因为root被定义为指针,所以你得到了int-assigned-to-pointer警告。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.