473,236 Members | 1,668 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,236 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 3142

"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;
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.