hello guyz iv got problem with void pointers i cant work this out !
i'm trying to read from a binary file using a struct and pass this struct arrays to class object and push them to stack and display them on screen while poping them out !
but the problem that i have a strict code and i cant change it which is a void pointers for push and pop
i know the whole code is messy coz i'm using C but i'm trying ma best to learn c++
anyways here is my code i'm using vc++

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
enum sex {male, female}; 
typedef struct data { 
char fname[30]; 
sex MF;
int age;
double wit; 
}records;
records student[10];
class s { 
private:
int top; 
int current;
void *layer[100]; 
public: 
stack(void)
{
top=0; //dont worry about the top and the current its //only for push and pop error checking
current=0;
}
int push(void *myobject) //my whole problem is in here because i cant change the int or even the void and the same for the pop()
{
records * layer= (records *) myobject;
}
void* pop(void) 
{
 
 
return layer[100];
 
 
}
};
int _tmain(int argc, _TCHAR* argv[])
{
stack name[10],age[10],weight[10];
FILE *fp;
if (( fp = fopen ( "datab.data", "rb" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
fread(student,200,3,fp);
name[1].push(student[0].fname); // so here i create stack 
//called name wel it was a test just to push one records to stack 
//well the push method was successful but i couldnt pop them out i got totaly different characters
name[1].pop();
/* for(int i=0; i<10; i++)
{
printf("%s %s %d %2.2f\n", student[i].fname,student[i].MF == 0 ? "Male" : "Female",student[i].age,student[i].wit);
}*/
fclose ( fp );
return 0;
}

i'll appreciate any help and once again i know its totally messy code for using C but still trying ma best to hit C++

What exactly is your problem, compilation error, runtime error ?
Post your errors or detail the things which occur when you run your code.

well when i run the code i got this output : هٍ↕هٍ↕ and i should get "fredy" instead !
i tried to put printf("%s",layer) in the push function, it works fine i got fredy as myobject (just for test if the push is successful)
but the problem is poping out this layer coz all what i'm trying to do is to push student[0].fname into stack ("fredy") and pop them back into the screen using printf("%s",name[1].pop()); so i'll get "fredy" as output.

...
void* pop(void) {return layer[100];}
...
name[1].push(student[0].fname);
printf("%s",name[1].pop());

thank you

...
void* pop(void) {return layer[100];} // replace with "return layer"
...
name[1].push(student[0].fname);
printf("%s",name[1].pop());

thank you

layer[100] stands for the character at position 99 and not the entire C Style array pointer. So what you are returning is the value of the 99th character as an addr of the string which is wrong.

Make the changes and tell me whether it works.

Hmm i dont know what you are up to becoz sifting through your code i found this:void *layer[100]

What are you trying to achieve here? Do you know what it stands for?
It stands for layer as an array of 100 void pointers ?
Is that what you wanted ?

And please post the code with all the formatting and removing the commenting. It really is a big problem trying to find something suspicious from that code dump.

Edited 3 Years Ago by Dani: Formatting fixed

#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
enum sex {male, female}; 
typedefstruct data { 
char fname[30]; 
sex MF;
int age;
double wit; 
}records;
records student[10];
class s { 
private:
int top; 
int current;
void *layer[100]; 
public: 
stack(void)
{
top=0;
current=0;
}
int push(void *myobject) 
{
records * layer= (records *) myobject;
}
void* pop(void) 
{
return layer;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
stack name[10],age[10],weight[10];
FILE *fp;
if (( fp = fopen ( "datab.data", "rb" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
fread(student,200,3,fp);
name[1].push(student[0].fname);  
printf("%s",name[1].pop());
fclose ( fp );
return 0;
}

actually i dont know ! from previous pracs it was meant to carry strings but it was char* layer[100];
Hints he gave us:
"Understand the concept of passing void * and of casting from a void * to the type of your choice. (You already did this when writing your comparison function for qsort!). 2. What would happen if you changed all your char * to void * in your s class? 3. You only need to implement a constructor and the push and pop methods (plus your test driver code) to test the concept. 4. You do not need to display the entire contents of the stack as you did in prac 6. This additional functionality is not required to answer the question. (However, if you really want to try to display the entire contents of the stack, then a good starting point is a search the net for "function pointers in C++". Beware ! It's verrrryyy tricky code!)
"
the push and pop in prac 6 was only to carry characters now he's saying modify your constructors so it can carry anyobject so thats why i think he put void* layer[100];

IF thats the case then maybe you need the void* layer and not void* layer[100] . This way you can make layer point to any type of structure instance or any primitive data type var as you want.

And done that, you cant just print out the name of the structure instance. YOu need to write a custom function like you did back in the " qsort() " function probably a " display() " function.

aw ok !
is this the right conversion from int double to void ?!
... = * (int *) ...;
...= * (double*)...;
the hardest part is enum hahah lol how i'm gonna convert from sex to void* heheh thats pretty hard ! coz i faced alota of difficulties printing the sex struct...

aw ok !
is this the right conversion from int double to void ?!
... = * (int *) ...;
...= * (double*)...;

Dont get what you are trying to say, quote an eg.

the hardest part is enum hahah lol how i'm gonna convert from sex to void* heheh thats pretty hard ! coz i faced alota of difficulties printing the sex struct...

*hint* enum values are in the end integer values.
male = 0, female = 1

Anyways MAte THX aLOta :) ya da champ !
i'll work on that tomorrow ! :)
its 4 am in here cheers gnit !

I thik below is the correct implentation of that class. One problem with this implementation is that the class is attempting to manage a list of objects that it does not own, and if some other function decides to delete one of the objects it will invalidate the pointer in the stack class. This can potentially cause a lot of grief, cursing and hair pulling by the programmer.

You will want to add some error checking in the push and pop functions to make sure variable current is within range.

class stack { 
private:
	int top; 
	int current;
	void *layer[100]; 
public: 
	stack(void)
	{
		top=0; 
		current=0;
	}
	int push(void *myobject)	{
		layer[current++] = myobject;
		return current-1;
	}
	void* pop(void) 
	{
		return layer[current--];
 
	}
};

Hello guyz i'm still getting an error about conversion from double to void*
i'll appreciate any Help :)
BTW i tried to push the name and age and sex it works but when i try to push the double struct element (weight) it gives an error...

#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
enum sex {male, female}; 
typedefstruct data { 
char fname[30]; 
sex MF;
int age;
double wit; 
}records;
records student[10];
class s { 
private:
int top; 
int current;
void *layer[100]; 
public: 
stack(void)
{
top=0;
current=0;
}
int push(void *myobject) 
{
records * layer= (records *) myobject;
}
void* pop(void) 
{
return layer;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
stack name,age,weight;
FILE *fp;
if (( fp = fopen ( "datab.data", "rb" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
fread(student,200,3,fp);
weight.push(student[0].fname); //i'm still getting error cannot convert form double to void *
printf("%s",weight.pop()); 
fclose ( fp );
return 0;
}

your program has at least two problems I can spot

int push(void *myobject) 
{
records * layer= (records *) myobject;
}

What is the above supposed to do? all it is doing is typcasting myobject from void* to records*, then does nothing with it. Its a "do-nothing" function.

void* pop(void) 
{
return layer;
}

Above does not do what you probably hope it does -- variable layer is an array of pointers and that pop function is returning the entire array. It is probably only supposed to return the current element in the array.

Look at the pop() function I posted to see how that should be accomplished.

#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
enum sex {male, female}; 
typedefstruct data { 
char fname[30]; 
sex MF;
int age;
double wit; 
}records;
records student[10];
class s { 
private:
int top; 
int current;
void *layer[100]; 
public: 
stack(void)
{
top=0;
current=0;
}
int push(void *myobject) 
{
if (current >20)
{
return -1;
}
layer[current++]= myobject;
}
void* pop(void) 
{
if (current <0)
{
return NULL;
}
return layer[current--];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
stack name,age,weight;
FILE *fp;
if (( fp = fopen ( "datab.data", "rb" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
fread(student,200,3,fp);
weight.push(student[0].fname); //i'm still getting error cannot convert form double to void *
printf("%s",weight.pop()); 
fclose ( fp );
return 0;
}

The problem occurs when you try to use that stack class to maintain doubles instead of pointers. It doesn't work because push() expects a pointer and not a double. It would make more sense for the stack class to maintain pointers to instances of the entire records structure instead of its individual members. Then whoever calls pop() will get a pointer to a records object and can do with it whatever it wants.

But he says thatst the way his assignment is supposed to be.
Othewise the implementation of stack will never contain an array of viod poitners in its class defination.

What he posted does compile ok after fixing minor posting errors. But the problem lies when attempting to pass a double to the push method instead of a pointer, such as

weight.push(student.weight); // error

wel pushing the whole string into the stack is a better idea when it comes to double(or probably compiler coz ~s.o.s~ got it right ) ! actually i searched the whole net for converting from double to void no way to do that...
but for further information i tried this
weight.push((void*) (int) student.weight)
it works even if i dont know what does that mean wel as an overview its conversting student.weight to void * and straight away convert to int ( as i understand it and ITS PROBABLY WRONG)
but still have a little problem is the decimal number
for example i have in student[1].weight = 80.12
if use the conversion of integer i only get the 80 without the 0.12....
i tried to put float instead of int, it doesnt work at all... as you said ~s.o.s~ thats the compiler problem coz i'm using vc++
do u think guyz that a function pointer will solve the problem in case of dividing the string ?!
Ancient dragon ~s.o.s~ i really appreciate everything, ya really helped me guys in this subject .

Haha :) i did it !!!! just push and address and pop the contents hahha :)

weight.push(&student[i].weight) 
printf("%2.2f",(*(double *)weight.pop()));

Ancient Dragon hehe it was your idea ;) and i stole it
thx champ

Dont get what you are trying to say, quote an eg.


*hint* enum values are in the end integer values.
male = 0, female = 1

u should work hard

Comments
You should check post dates before bumping year old threads with meaningless one-liners

Haha :) i did it !!!! just push and address and pop the contents hahha :)

weight.push(&student[i].weight) 
printf("%2.2f",(*(double *)weight.pop()));

Ancient Dragon hehe it was your idea ;) and i stole it
thx champ

oh so sad

This question has already been answered. Start a new discussion instead.