473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 3157

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
8
by: John Hanley | last post by:
I working in C. I haven't paid much attention to void pointers in the past, but I am wondering if I can use them for my various linked lists to save work. I have two different linked lists that...
6
by: MackS | last post by:
Hi FAQ 4.9 states that void** cannot be used portably "to pass a generic pointer to a function by reference". I would like to hear from you whether there is anything wrong with the following...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg =...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
3
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
10
by: Zero | last post by:
Hello all, I wonder about void? To which category in the C programming language does it belong? Of how many bits consits void? Is it possible to define a varibale called void a;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.