| | |
void pointers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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++
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++
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> usingnamespace std; 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; //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; }
DarkCoder+
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.
thank you
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()); DarkCoder+
•
•
•
•
... void* pop(void) {return layer[100];} // replace with "return layer" ...thank youname[1].push(student[0].fname); printf("%s",name[1].pop());
Make the changes and tell me whether it works.
I don't accept change; I don't deserve to live.
Hmm i dont know what you are up to becoz sifting through your code i found this:
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.
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.
I don't accept change; I don't deserve to live.
0
#7 Oct 19th, 2006
#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];
Last edited by ~s.o.s~; Oct 19th, 2006 at 2:04 pm.
DarkCoder+
IF thats the case then maybe you need the
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 "
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. Last edited by ~s.o.s~; Oct 19th, 2006 at 2:13 pm.
I don't accept change; I don't deserve to live.
•
•
•
•
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...
male = 0, female = 1
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- void pointer (C)
Other Threads in the C++ Forum
- Previous Thread: Calculating Avg from a function
- Next Thread: American Idol program with classes
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






