void pointers

B

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
 
A

Allan Bruce

ben said:
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
 
P

Phlip

ben said:
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);
...
 
J

Jesper Madsen

ben said:
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
 
P

Peter Gordon

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.name << "\t\t" << people.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.name << "\t\t" << people.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.name << "\t\t" << people.age << endl;

return 0;
}
 
J

Julie

Jesper said:
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.
 
H

Howard

Phlip said:
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
 
J

Julie

Phlip said:
Julie wrote:




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.
 
B

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
 
A

Andrew McDonagh

ben said:
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;

};
 
B

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.
 
A

Andrew McDonagh

ben said:
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?
 
H

Howard

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
 
A

Andrew McDonagh

Howard said:
Why not?




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.
 
H

Howard

Andrew McDonagh said:
Well encapsulation for one thing.



we know.


I have them :)



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top