Pointer problem

S

seema

I have a struct and I want to access its values, I am new to pointers
so I have written something like this,
# define COLLATE "Beetle"
struct mytips
{
short inverse;
long mass;
char *c;
char *m;
};
int main(){
struct mytips store[10] = {
{0,0x00000001L,COLLATE,"playing one"},
{0,0x00000002L,COLLATE,"playing two"},
{0,0x00000004L,COLLATE,"playing three"},
{0,0x00000008L,COLLATE,"playing four"},
{0,0x00000008L,COLLATE,"playing five"},
{0,0x00000008L,COLLATE,"playing six"},
};
void *dinku;
dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);
}
Now how can I access the values of store using void pointer? Can
somebody explaing
how to do it
Thanks in advance,
Seema
 
M

Michael Mair

seema said:
I have a struct and I want to access its values, I am new to pointers
so I have written something like this,

No. Nononononono!
Please copy and paste the actual code.
"something like this" may differ in important points
from the posted code. This makes it unnecessarily hard
to help you.
BTW: Creating a minimal example helps people see your
problem at a glance...

Your code lacks

#include said:
# define COLLATE "Beetle"
struct mytips
{
short inverse;
long mass;
char *c;
char *m;
};
int main(){
make that
int main (void)
struct mytips store[10] = {
{0,0x00000001L,COLLATE,"playing one"},
{0,0x00000002L,COLLATE,"playing two"},
{0,0x00000004L,COLLATE,"playing three"},
{0,0x00000008L,COLLATE,"playing four"},
{0,0x00000008L,COLLATE,"playing five"},
You probably wanted 0x00000010L
{0,0x00000008L,COLLATE,"playing six"},
};
void *dinku;
dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);

Wrong idiom.
Usually, you do
struct mytips *dinku;
dinku = malloc(NumberOfMytips * sizeof *dinku);

Apart from that: sizeof(char) == 1, always.
I do not understand in the least what you are trying to achieve
with the above.
You forgot:
if (dinku == NULL) {
/* error handling */
}
.... /* use dinku */
free(dinku);

and finally
return 0;
}
Now how can I access the values of store using void pointer? Can
somebody explaing
how to do it

No. Yes.
If you passed a pointer to an object as a void *, access
it as the right type:
int ireceivedinkus(void *pData)
{
struct mytips *pDinku = pData;
/* access data via pDinku */
....
}

You can convert between void* and other pointer types without
explicit cast.
Best, you just read the comp.lang.c FAQ (google will point you
there). There is enough background about pointers.

Cheers
Michael
 
K

Kleuskes & Moos

I have a struct and I want to access its values, I am new to pointers
so I have written something like this,
# define COLLATE "Beetle"
struct mytips
{
short inverse;
long mass;
char *c;
char *m;
};
int main(){
struct mytips store[10] = {
{0,0x00000001L,COLLATE,"playing one"},
{0,0x00000002L,COLLATE,"playing two"},
{0,0x00000004L,COLLATE,"playing three"},
{0,0x00000008L,COLLATE,"playing four"},
{0,0x00000008L,COLLATE,"playing five"},
{0,0x00000008L,COLLATE,"playing six"},
};
void *dinku;
dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);

Casting after malloc is unneccesary. Include said:
Now how can I access the values of store using void pointer?

You must cast the void pointer, otherwise there's no way to access any of
the fields, since the compiler does not know what is actually referenced.
 
R

Richard Heathfield

Kleuskes & Moos said:
I have a struct and I want to access its values, I am new to pointers
so I have written something like this,
# define COLLATE "Beetle"
struct mytips
{
short inverse;
long mass;
char *c;
char *m;
};
int main(){
struct mytips store[10] = {
{0,0x00000001L,COLLATE,"playing one"},
{0,0x00000002L,COLLATE,"playing two"},
{0,0x00000004L,COLLATE,"playing three"},
{0,0x00000008L,COLLATE,"playing four"},
{0,0x00000008L,COLLATE,"playing five"},
{0,0x00000008L,COLLATE,"playing six"},
};
void *dinku;
dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);

Casting after malloc is unneccesary. Include said:
Now how can I access the values of store using void pointer?

You must cast the void pointer, otherwise there's no way to access any of
the fields, since the compiler does not know what is actually referenced.

{
struct mytips *p = dinku; /* cast? what cast? */
}
 
K

Kleuskes & Moos

On Fri, 09 Dec 2005 17:04:13 +0000, Richard Heathfield wrote:

{
struct mytips *p = dinku; /* cast? what cast? */
}

Me and my big mouth. :).

This is better, of course. Use 'void *' only if you have no choice.
 
R

Richard Heathfield

Kleuskes & Moos said:
Me and my big mouth. :).

This is better, of course. Use 'void *' only if you have no choice.

....or rather, use it when it's the Right Thing. Sometimes void * is not the
*only* choice, but is nevertheless the most natural choice that leads to
the most elegant code. The only real problem with it is that, by its very
nature, it sidesteps type-checking. That, of course, is also its great
asset.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top