Velocity Reviews > Pointer problem

# Pointer problem

seema
Guest
Posts: n/a

 12-09-2005
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
Seema

Michael Mair
Guest
Posts: n/a

 12-09-2005
seema wrote:
> 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
BTW: Creating a minimal example helps people see your
problem at a glance...

#include <stdlib.h>

> # 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:
{
struct mytips *pDinku = pData;
/* access data via pDinku */
....
}

You can convert between void* and other pointer types without
explicit cast.
there). There is enough background about pointers.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Kleuskes & Moos
Guest
Posts: n/a

 12-09-2005
On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:

> 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 <stdlib.h> instead.

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

Richard Heathfield
Guest
Posts: n/a

 12-09-2005
Kleuskes & Moos said:

> On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:
>
>> 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 <stdlib.h> instead.
>
>> 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? */
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Kleuskes & Moos
Guest
Posts: n/a

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

<snip>
>>> 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? */
> }

Me and my big mouth. .

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

Richard Heathfield
Guest
Posts: n/a

 12-09-2005
Kleuskes & Moos said:

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

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)