Click here to Skip to main content
15,888,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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;
} 


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
Posted
Updated 22-May-18 0:56am

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[^]
 
Share this answer
 
C++
    struct ret to_func;
// ...
    strcpy(to_func.drive,a);

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.
 
Share this answer
 
v2
Comments
Erebus_22 3-Sep-16 5:20am    
thanks that is resolved.
the problem that i am facing is with GetDiskFreeSpaceEx which is giving incorrect disk size, my D drive is disk drive but the function still shows available space in drive, why is that
Richard MacCutchan 3-Sep-16 6:25am    
Sorry, I don't understand. The GetDiskFreeSpaceEx function[^] is supposed to show available space.
Erebus_22 3-Sep-16 6:31am    
Thanks it was a internal bug

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900