469,270 Members | 1,232 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

void pointers

ben
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;

Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn't it?

bye,
ben
Jul 23 '05 #1
15 2848

"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;

Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn't it?

bye,
ben


void pointers aren't used in C++ really. You generally use inheritance and
objects to avoid their use. If you do use it, then you need to cast your
pointer to (void *) and then cast back when you want to use it.
e.g.

r1.value = (void *)str;
cout<<(char *)r1.value;

Allan
Jul 23 '05 #2
ben wrote:
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?
By creating an enumeration, such as

enum types { integer, string, real };

and assign the correct one to fieldType.
if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;


Then switch based on the type:

switch(r1.fieldType)
{
case string:
cout << static_cast<char*>(r1.value);
...
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 23 '05 #3

"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;

Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn't it?

bye,
ben


I think you mistake void pointers for variants.. Try looking at boost.org
for boost::any or boost::variant
Jul 23 '05 #4
"Jesper Madsen" <ba***@mail.stofanet.dk> wrote in news:423f27fb$0$30764
$b*******@nntp06.dk.telia.net:

"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

A hint which might help.
The prototype of the the compare function in the library function qsort
is :
int (*fcmp)(const void *, const void *)

Below is an example of how to pass arguments.
as void *
Check the functions cmp_age() and cmp_names().

#include <iostream>
#include <stdlib.h>
#include <string.h>

using std::cout ;
using std::endl;
using std::qsort; // Needed for CBuilder4

struct myStruct {
char name[80];
int age;
};

const struct myStruct people[] =
{
{ "Peter", 60 },
{ "Mary", 35 },
{ "Jack", 40 },
{ "Fred", 65 },
{ "Alice", 25 }
};

int cmp_age(const void *lhs, const void *rhs) {
const struct myStruct * a = (struct myStruct *)lhs;
const struct myStruct * b = (struct myStruct *) rhs;

return a->age - b->age;
}

int cmp_names(const void *lhs, const void *rhs) {
const struct myStruct * a = (struct myStruct *)lhs;
const struct myStruct * b = (struct myStruct *) rhs;

return strcmp(a->name, b->name);
}

int main(int argc, char **argv) {
int i;
// List the structure people before sorting
for(i = 0; i < 5; ++i)
cout << people[i].name << "\t\t" << people[i].age << endl;

cout << endl;

// Sort people by age and then list.
qsort((void *)people, 5, sizeof(people[0]), cmp_age);

for(i = 0; i < 5; ++i)
cout << people[i].name << "\t\t" << people[i].age << endl;

cout << endl;

// Sort people by name and list.
qsort((void *)people, 5, sizeof(people[0]), cmp_names);

for(i = 0; i < 5; ++i)
cout << people[i].name << "\t\t" << people[i].age << endl;

return 0;
}

Jul 23 '05 #5
Jesper Madsen wrote:
"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;

Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn't it?

bye,
ben

I think you mistake void pointers for variants.. Try looking at boost.org
for boost::any or boost::variant


Boost isn't part of the language, void * is.
Jul 23 '05 #6
Julie wrote:
I think you mistake void pointers for variants.. Try looking at boost.org for boost::any or boost::variant
Boost isn't part of the language, void * is.


Our job here is to recommend the best technique, not the one that happens to
have ISO standards.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 23 '05 #7

"Phlip" <ph*******@yahoo.com> wrote in message
news:lr******************@newssvr33.news.prodigy.c om...
Julie wrote:
> I think you mistake void pointers for variants.. Try looking at boost.org > for boost::any or boost::variant

Boost isn't part of the language, void * is.


Our job here is to recommend the best technique, not the one that happens
to
have ISO standards.


Well, in this case the history is muddled.

There are many posts saying that we don't discuss "third-party" software
here, but only the C++ language, as defined by the standard.

However, boost has made its way into this newsgroup consistently in the
past, without much complaint. If I understand correctly, much of the boost
library will eventually become part of the standard, so I guess it makes
some sense.

Of course, many of us don't *have* the boost software, so suggestions to use
boost::whatever are, to us anyway, off-topic. We just don't usually say
anything about it. :-)

As for our "job" here...? Well, my job actually is to be doing real work,
so I better get back to it! :-)

-Howard

Jul 23 '05 #8
Phlip wrote:
Julie wrote:

I think you mistake void pointers for variants.. Try looking at
boost.org
for boost::any or boost::variant


Boost isn't part of the language, void * is.

Our job here is to recommend the best technique, not the one that happens to
have ISO standards.


Negative, CLC is about the _language_ and how best to use the standard language
for tasks at hand. Boost is not part of the C++ language.

A programming newsgroup would make recommendations on (third-party) techniques.

I realize that I'm being picky, but I don't like the (sometimes perceived)
notion that Boost is fair game for C++ language questions -- it isn't.
Jul 23 '05 #9
ben
Hi guyz,
Thank u all for helping me out with void pointers.You have
pointed out certain basic things which are usually not known to
all programmers( e.g. u can't dereference a void ptr. directly)
By the way i am totally not aware of this thing called Variants or
Boost.
Thanks again.
bye,
ben

Jul 23 '05 #10
ben wrote:
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;

how do i initialize the field void * value?

if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;

Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn't it?

bye,
ben


Hi Ben,

The other have provide good advice (ignoring the 'don't mention Boost
part). However, one direction which might be worth considering is why
use a structure in the first place and not a class?

Not that it helps your question re void* much, but at least you would
then be able to take full advantage of class properties (encapsulation
etc. ).

Now initially changing your struct to a class would result in a simple
(but dumb) data holder class. However, once you have the class, you'd
be able to move behavior into it as well.

You say that you pass this struct to some function for display. Well,
if it was a class, then one possible example of behavior, you would be
to use a publish/subscriber pattern which is where the class ( the old
struct) informs the display that it has changed, and needs redisplaying.

class Column
{
public:
void setSubscriber(const Subscriber& theDisplay) ( m_subscriber =
theDisplay;}

void setValue(const Value& newValue)
{
value = newValue
publish();
};

private:

void publish() { m_subscriber.update(); }

Subscriber& m_subscriber;
char *fieldName;
int fieldType;
void* value;
bool nullFlag;

};
Jul 23 '05 #11
ben
Well i was asked to use a structure.If i had freedom to do things my
way,i would have probably avoided using void ptrs. in the first
place.Thanks.

Jul 23 '05 #12
ben wrote:
Well i was asked to use a structure.If i had freedom to do things my
way,i would have probably avoided using void ptrs. in the first
place.Thanks.


out of interest, asked by whom?
Jul 23 '05 #13

"Andrew McDonagh" <ne**@andrewcdonagh.f2s.com> wrote in message
news:d2**********@news.freedom2surf.net...
The other have provide good advice (ignoring the 'don't mention Boost
part). However, one direction which might be worth considering is why use
a structure in the first place and not a class?

Why not?
Not that it helps your question re void* much, but at least you would then
be able to take full advantage of class properties (encapsulation etc. ).

Now initially changing your struct to a class would result in a simple
(but dumb) data holder class. However, once you have the class, you'd be
able to move behavior into it as well.


What are you going on about? This isn't Turbo C++ 1.0 or something we're
discussing here.

I seem to recall one of my really early C++ compilers (Turbo C++, I think)
where a struct held only data and a class was much as it is now, but that
was more than twenty years ago. You need a good, up-to-date book on C++, I
think. :-)

In any modern C++ compiler, a struct and a class have ALL the same
abilities! A struct differs from a class in the default visibility of its
members (and I think the default inheritance, but I forget). Members are
public by default in a struct, and private by default in a class.
Otherwise, there is NO difference between them. Anything you can do with a
class, you can do with a struct.

-Howard
Jul 23 '05 #14
Howard wrote:
"Andrew McDonagh" <ne**@andrewcdonagh.f2s.com> wrote in message
news:d2**********@news.freedom2surf.net...

The other have provide good advice (ignoring the 'don't mention Boost
part). However, one direction which might be worth considering is why use
a structure in the first place and not a class?
Why not?

Not that it helps your question re void* much, but at least you would then
be able to take full advantage of class properties (encapsulation etc. ).

Now initially changing your struct to a class would result in a simple
(but dumb) data holder class. However, once you have the class, you'd be
able to move behavior into it as well.

What are you going on about?


Well encapsulation for one thing.

This isn't Turbo C++ 1.0 or something we're
discussing here.
we know.

I seem to recall one of my really early C++ compilers (Turbo C++, I think)
where a struct held only data and a class was much as it is now, but that
was more than twenty years ago. You need a good, up-to-date book on C++, I
think. :-)
I have them :-)

In any modern C++ compiler, a struct and a class have ALL the same
abilities!
A struct differs from a class in the default visibility of its
members (and I think the default inheritance, but I forget). Members are
public by default in a struct, and private by default in a class.
Otherwise, there is NO difference between them. Anything you can do with a
class, you can do with a struct.

We all know this, I assumed I would not have to state the obvious, thats
all. I was referring to encapsulation. As you say, everything is public
by default for structs - which isn't great for encapsulation.

-Howard

Jul 23 '05 #15

"Andrew McDonagh" <ne**@andrewcdonagh.f2s.com> wrote in message
news:d2**********@news.freedom2surf.net...
Howard wrote:
"Andrew McDonagh" <ne**@andrewcdonagh.f2s.com> wrote in message
news:d2**********@news.freedom2surf.net...

Why not?

Not that it helps your question re void* much, but at least you would
then be able to take full advantage of class properties (encapsulation
etc. ).

Now initially changing your struct to a class would result in a simple
(but dumb) data holder class. However, once you have the class, you'd be
able to move behavior into it as well.

What are you going on about?


Well encapsulation for one thing.

This isn't Turbo C++ 1.0 or something we're discussing here.


we know.

I seem to recall one of my really early C++ compilers (Turbo C++, I
think) where a struct held only data and a class was much as it is now,
but that was more than twenty years ago. You need a good, up-to-date
book on C++, I think. :-)


I have them :-)

In any modern C++ compiler, a struct and a class have ALL the same
abilities! A struct differs from a class in the default visibility of
its
members (and I think the default inheritance, but I forget). Members are
public by default in a struct, and private by default in a class.
Otherwise, there is NO difference between them. Anything you can do with
a class, you can do with a struct.

We all know this, I assumed I would not have to state the obvious, thats
all. I was referring to encapsulation. As you say, everything is public by
default for structs - which isn't great for encapsulation.


Sounds like you're referring to "data hiding", not encapsulation
Encapsulation is putting together the data and the methods that deal with
it. Which both the struct and class handle exactly the same. And proper
data hiding can be achieved in a struct simply by using the "private" access
specifier.

Your statements sure didn't sound like you thought classes were better
simply because of their "default" visibility, but because they had abilities
that structs don't.

For example:

"However, once you have the class, you'd be able to move behavior into it as
well."

You can move behavior into a struct just as in a class.

And,

"you would then be able to take full advantage of class properties
(encapsulation etc. )."

I've already addressed encapsulation. And default visibility is simply
that: default. It can be changed at any time with proper access specifiers.
So again, what are you saying that classes provide?

Apparently, *you* know what you meant, but reading your response to the OP,
it seems quite plain that you're saying that classes will give him the
ability to do things structs can't, and that's just not true.

Sorry to pick on you, but I just think the OP needs to know the facts (and
the reasoning behind any opinions).

-Howard


Jul 23 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Stig Brautaset | last post: by
8 posts views Thread by John Hanley | last post: by
6 posts views Thread by MackS | last post: by
188 posts views Thread by infobahn | last post: by
12 posts views Thread by Andreas Schmidt | last post: by
5 posts views Thread by Stijn van Dongen | last post: by
3 posts views Thread by bnoordhuis | last post: by
27 posts views Thread by Erik de Castro Lopo | last post: by
10 posts views Thread by Zero | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.