不兼容的类型错误 [英] Incompatible type error
问题描述
#include<windows.h>
#include<stdio.h>
#include <ctype.h>
typedef struct ret
{
long long memory;
char* drive;
};
int main()
{
struct ret total_memory;
total_memory=size_show(GetLogicalDrives());
return 0;
}
struct ret size_show(long int n )
{
struct ret to_func;
long long ii;
long int i, k, andmask;
char a[5]="E://";
char c;
printf("%d",strlen(a));
printf("drives available\n");
for ( i = 32 ; i >= 0 ; i-- )
{
andmask = 1 << i;
k = n & andmask;
if(k!=0)
{
k=i+65;
printf("%c:\n",k);
}
}
printf("drive you want to clear\n");
scanf("%c",&c);
a[0]=toupper(c);
printf("%s.\n",a);
printf("%d\n",strlen(a));
i=strcmp("E://",a);
printf("%d",i);
k=GetDiskFreeSpaceEx(a,NULL,NULL,&ii);
printf("%ld",k);
printf("%ld",ii);
to_func.memory=ii;
strcpy(to_func.drive,a);
return to_func;
}
我无法弄清楚上面的代码有什么问题,我希望从中返回两个值函数因此我使用结构,我期待代码中的警告而不是错误。
感谢您提前的帮助
< b>我尝试了什么:
我尝试更改数据类型
I can't figure out the problem the above code has, I want two values to be returned from the function hence I am using a struct, I was expecting warnings in the code instead getting errors.
Thanks for your help in advance
What I have tried:
I tried changing types of the data
推荐答案
一个函数不能返回一个结构,最多它可以返回一个指向结构的指针。
第二个问题,那个结构不能像你那样在堆栈上创建。
以下是一些讲座:
C编程语言 - 维基百科,免费的百科全书 [ ^ ]
HT tps://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]
A function can't return a structure, at best it can return a pointer to the structure.
Second problem, that structure can't be created on stack like you have done.
Here is some lecture:
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
struct ret to_func;
// ...
strcpy(to_func.drive,a);
您在函数 size_show
的顶部声明了您的结构,但您从未初始化它。因此,当您尝试将某些内容复制到 to_func.drive
时,它会失败,因为您有一个未初始化的指针。
另外你的驱动器规格是错误的,你使用双正斜杠而不是反斜杠,应该E:\\
。
如果您告诉我们哪行代码会出错,这会有所帮助。
You declare your struct at the top of the function size_show
, but you never initialise it. So when you try to copy something to to_func.drive
it fails because you have an uninitialised pointer.
Also your drive specifications are wrong, you are using double forward slashes instead of backslashes, should be "E:\\"
.
And it would help if you told us which line of code gives the error.
这篇关于不兼容的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!