void pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

void pointers

 
0
  #1
Oct 19th, 2006
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>
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;
}
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++
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: void pointers

 
0
  #2
Oct 19th, 2006
What exactly is your problem, compilation error, runtime error ?
Post your errors or detail the things which occur when you run your code.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: void pointers

 
0
  #3
Oct 19th, 2006
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
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: void pointers

 
0
  #4
Oct 19th, 2006
Originally Posted by rowly View Post
...
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: void pointers

 
0
  #5
Oct 19th, 2006
nop still getting the same characters هٍ↕
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: void pointers

 
0
  #6
Oct 19th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training
 
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+
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: void pointers

 
0
  #8
Oct 19th, 2006
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.
Last edited by ~s.o.s~; Oct 19th, 2006 at 2:13 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: void pointers

 
0
  #9
Oct 19th, 2006
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...
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: void pointers

 
0
  #10
Oct 19th, 2006
Originally Posted by rowly View Post
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
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC