C中不兼容的指针类型。何时使用*或&;?在引用指针的概念上有问题

C中不兼容的指针类型。何时使用*或&;?在引用指针的概念上有问题,c,pointers,C,Pointers,我的指针有问题。我知道我应该在上班时间去,但我急需帮助。现在最大的问题是调试这个程序。根据我的理解,我应该在void函数中声明一个地址。之后,我必须使用&for readfile(%testarray)。我做错了什么?我的程序的目标是读取一个数字文件并将它们存储在一个数组中。然后,我将打印数组中的所有数字。任何帮助都将不胜感激 sort.c:11:3: warning: passing argument 1 of 'read_file' makes pointer from integer wi

我的指针有问题。我知道我应该在上班时间去,但我急需帮助。现在最大的问题是调试这个程序。根据我的理解,我应该在void函数中声明一个地址。之后,我必须使用&for readfile(%testarray)。我做错了什么?我的程序的目标是读取一个数字文件并将它们存储在一个数组中。然后,我将打印数组中的所有数字。任何帮助都将不胜感激

sort.c:11:3: warning: passing argument 1 of 'read_file' makes pointer from integer without a cast [enabled by default]
sort.c:3:6: note: expected 'int **' but argument is of type 'int'
sort.c: In function 'read_file':
sort.c:27:3: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat]
代码:

#包括
#包括
无效读取文件(int*myList[]);
int main()
{
int testarray[20];
read_文件(&testarray[20]);
返回0;
}
无效读取文件(int*myList[])
{
文件*填充;
int i;
infle=fopen(“data.txt”,“r”);
if(infle==NULL)
{
printf(“无法打开文件”);
出口(1);
}
i=0;
while(fscanf(infle,“%d”,*myList[i])!=EOF)
{
printf(“%d”,*myList[i]);
i=i+1;
}
printf(“\n”);
printf(“%d\n”,i);
}//无效
您应该使用:

read_file(&testarray);

当您尝试传递指向整个数组的指针时。您所做的只是获取数组中第20个元素的地址(顺便说一句,它超出了边界)。

表达式
&testarray[20]
是指向数组中最后一个条目之外的一个元素的指针(即与
int*
相同),而不是指向数组的指针。此外,现在将参数声明为指针数组,而不是指向数组的指针。(在
int*arr[]
int(*arr)[]
之间的差异)

此外,您不需要将指向数组的指针传递给函数,只需让函数具有普通数组(或指针)参数,并按原样传递数组:

void read_file(int myList[]);

int main(void)
{
    int testarray[20];
    read_file(testarray);
    /* .... */
}

read\u文件中
使用数组时,不需要解引用操作符
*
。您可以将其用作普通数组。

这是您的固定代码

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


void read_file(int myList[20]);

int main()
{
    int testarray[20];
    read_file(testarray);
    return 0;
}

void read_file(int myList[20])
{
    FILE* inFile;
    int i;
    inFile = fopen("data.txt","r");
    if (inFile == NULL)
    {
        printf("Unable to open file");
        exit(1);
    }
    i = 0;
    while (fscanf(inFile,"%d", &myList[i]) != EOF)
    {
        printf("%d ", myList[i]);
        i = i+1;
    }
    printf("\n");
    printf("%d\n", i );
} 
#包括
#包括
无效读取文件(int myList[20]);
int main()
{
int testarray[20];
读取文件(测试阵列);
返回0;
}
无效读取文件(int myList[20])
{
文件*填充;
int i;
infle=fopen(“data.txt”,“r”);
if(infle==NULL)
{
printf(“无法打开文件”);
出口(1);
}
i=0;
while(fscanf(infle、%d、&myList[i])!=EOF)
{
printf(“%d”,myList[i]);
i=i+1;
}
printf(“\n”);
printf(“%d\n”,i);
} 

当您在函数头中的名称后使用方括号时,您会告诉编译器您正在传递数组

  • int testarray[]
    表示整数数组
  • int*testarray[]
    表示整数指针数组
由于传递一个整数数组,函数签名应为

void read_file(int myList[]);
或其等价物

void read_file(int *myList);
fscanf(inFile,"%d", myList+i)
呼叫应如下所示:

read_file(testarray);
接下来,关于
&
*
的主题:符号和从具有地址的值表达式生成指针,而星号从指针表达式生成值
scanf
接受一个指针,因此您需要使用其中一个来调用它

fscanf(inFile,"%d", &myList[i])
或同等品

void read_file(int *myList);
fscanf(inFile,"%d", myList+i)

在文本文件中关联10个数字,请参考以下代码:

    #include <stdio.h>
    #include <stdlib.h>
    void read_file(int *);

    int main()
    {
     int *List=(int *)malloc(10*sizeof(int));// Allocate memory for 10 integers
     read_file(List); 
     return 0;
    }

/* Passing a pointer to the contiguous locations, just the starting location is enough*/
    void read_file(int *ptr)
    {
     FILE *inFile;
     inFile=fopen("Numbers.txt","r");
     int i=0;
     if(inFile!=NULL)
      while(fscanf(inFile,"%d",ptr)!=EOF)
      {    
       printf("%d ",*(ptr));
       i++;
      }
      free(ptr);// Free the memory Locations
     } 
#包括
#包括
无效读取文件(int*);
int main()
{
int*List=(int*)malloc(10*sizeof(int));//为10个整数分配内存
读取文件(列表);
返回0;
}
/*传递一个指向相邻位置的指针,只要开始位置就足够了*/
无效读取文件(int*ptr)
{
文件*填充;
infle=fopen(“Numbers.txt”、“r”);
int i=0;
if(infle!=NULL)
而(fscanf(infle,“%d”,ptr)!=EOF)
{    
printf(“%d”,*(ptr));
i++;
}
释放(ptr);//释放内存位置
} 

不是我,但他们可能希望你在回答中至少添加几个词。解释等等。例如,到目前为止,你是唯一一个发现(并修复)代码中的另一个错误的人,但是因为你没有说任何关于它的事情,…其他3个答案做得非常好,我刚刚得到了整个代码,为什么不发布它。非常感谢。我真的很感谢你的帮助。这是我改变的。编辑:看来我不能在这里发布没有正确格式的代码。但是我听从了dasblinkenlight的建议,所有的警告都没有了。你可能希望澄清你的循环。虽然fscanf读取1项转换为
,而(fscanf(infle,“%d”&integer)==1)
,因为返回值0不是EOF,但表示未读取任何项。