void * to struct * casting ..please help!

N

Neha

Hi all


Its seems bit silly que but pleasee explan me why this error is coming
? and what will be the solution if i want void * to be intilaize by
struct * and this code is puerly in C.

--
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>


typedef struct AAA
{
unsigned char str;
}Abbb;

typedef struct BBB
{
Abbb ab;
}Bbbb;

int main()
{
void *a;
a= (Bbbb *) malloc(sizeof(Bbbb));
a->ab.str='c';
printf("%c",a->ab.str);

return 0;
}

erorr----------
test.c
D:\C-C++\test.c(27) : error C2223: left of '->ab' must point to
struct/union
D:\C-C++\test.c(29) : error C2223: left of '->ab' must point to
struct/union

Thanx in Advance
Neha
 
T

Tom St Denis

Neha said:
typedef struct AAA
{
unsigned char str;
}Abbb;

typedef struct BBB
{
Abbb ab;
}Bbbb;

int main()
{
void *a;
a= (Bbbb *) malloc(sizeof(Bbbb));
a->ab.str='c';
printf("%c",a->ab.str);

What type is "a"?

Casting changes the interpretation in an expression, not the data type.
You could do

((Bbbb *)a)->ab.str = 'c'

See the difference?

If you wanted a to be a pointer to Bbbb then make it a pointer to Bbbb,
e.g.

Bbbb *a;

Also please don't cast the return value of malloc.

Tom
 
M

mark_bluemel

Neha said:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>


typedef struct AAA
{
unsigned char str;
}Abbb;

typedef struct BBB
{
Abbb ab;
}Bbbb;

int main()
{
void *a;

a is a "pointer to void" and cannot be dereferenced.
a= (Bbbb *) malloc(sizeof(Bbbb));

malloc() returned a "pointer to void" which you cast to "pointer to
Bbbb" and then stored in a "pointer to void". The cast is unnecessary,
unhelpful and could in some cases hide a bug...

a->ab.str='c';

The compiler complains because a is still a "pointer to void" and still
cannot be dereferenced. This is not a dynamically typed language, once
you declare a as "pointer to void" that's what it stays.
printf("%c",a->ab.str);

As before...
return 0;
}

It would seem most appropriate to declare a as a pointer to Bbbb
(horrid names you choose).

If you really need to use "void *" (why?) then you'll need to cast it
before using it, like this abomination:-

((Bbbb *)a)->ab.str='c';
printf("%c\n",((Bbbb *)a)->ab.str);
 
S

santosh

Neha said:
Hi all


Its seems bit silly que but pleasee explan me why this error is coming
? and what will be the solution if i want void * to be intilaize by
struct * and this code is puerly in C.

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>

Get rid of this non-standard header. malloc() is declared in stdlib.h.
typedef struct AAA
{
unsigned char str;
}Abbb;

typedef struct BBB
{
Abbb ab;
}Bbbb;

Rewrite these as:
typedef struct {
unsigned char str;
} Abbb;

typedef struct {
Abbb ab;
} Bbbb;
int main()

Change to int main(void)
{
void *a;
a= (Bbbb *) malloc(sizeof(Bbbb));

Don't use the cast here and check the return value of malloc().
a->ab.str='c';
printf("%c",a->ab.str);

To deference void pointers you must apply the appropriate cast. So:
((Bbbb *)a)->ab.str = 'c';
or
(*(Bbbb *)a).ab.str = 'c';

Similarly:
printf("%c\n", ((Bbbb *)a)->ab.str);
or the other form.
return 0;
}
<snip>
 
S

santosh

Neha said:
Hi all

Its seems bit silly que but pleasee explan me why this error is coming
? and what will be the solution if i want void * to be intilaize by
struct * and this code is puerly in C.

Not purely standard C or even correct C.
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>

This is a non-standard header.
typedef struct AAA
{
unsigned char str;
}Abbb;

typedef struct BBB
{
Abbb ab;
}Bbbb;

Rewrite these as:
typedef struct {
unsigned char str;
} Abbb;

typedef struct {
Abbb ab;
} Bbbb;
int main()

Use int main(void)
{
void *a;
a= (Bbbb *) malloc(sizeof(Bbbb));

The cast is not needed here. Moreover check the return value of
malloc().
a->ab.str='c';
printf("%c",a->ab.str);

To deference a void pointer you must cast it to the appropriate type.
So:
((Bbbb *)a)->ab.str = 'c';
printf("%c\n", ((Bbbb *)a)->ab.str);

Note: None of the parenthesis are redundant.
 
M

Martin Ambuhl

Neha said:
Hi all


Its seems bit silly que but pleasee explan me why this error is coming
? and what will be the solution if i want void * to be intilaize by
struct * and this code is puerly in C.

#include <stdlib.h>
#include <stdio.h>
#if 0
/* mha: <memory.h> is not a standard header */
#include <memory.h>
#endif

typedef struct AAA
{
unsigned char str;
} Abbb;

typedef struct BBB
{
Abbb ab;
} Bbbb;

int main(void)
{
void *a;
a = malloc(sizeof(Bbbb)); /* mha: removed superfluous cast */
((Bbbb *) a)->ab.str = 'c'; /* mha: fixed bogus use of void * here
and below */
printf("%c\n", ((Bbbb *) a)->ab.str); /* mha: fixed lack of '\n'
on last line of output */
return 0;
}
 
K

Keith Thompson

Tom St Denis said:
What type is "a"?

Casting changes the interpretation in an expression, not the data type.
[snip]

That's an odd way of putting it.

A cast is an expression that specifies a conversion. The operand of
the cast is of some type (void* in this case); the result is of the
type specified in the cast (Bbbb* in this case). In both cases, we're
talking about types of expressions, not of objects.

Assigning a value to an object *never* changes the type of the object;
it can only change its value. The fact that that value happens to be
the result of a conversion to some type doesn't change that.

So let's look at what happens in the assignment statement

a= (Bbbb *) malloc(sizeof(Bbbb));

malloc() returns a result of type void*. You convert that result from
void* to Bbbb*. You then assign the result of the conversion (which
is of type Bbbb*) to an object of type void*. This is allowed because
there's an implicit conversion from void* to any pointer-to-object
type, and vice versa. So the result of malloc() is converted
explicitly from void* to Bbbb*, and then implicitly back to void*.

Here's how I'd write it:

#include <stdio.h>
#include <stdlib.h>

struct AAA {
unsigned char str;
};

struct BBB {
struct AAA ab;
};

int main(void)
{
struct BBB *a;
a = malloc(sizeof *a);
if (a == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
a->ab.str = 'c';
printf("%c\n", a->ab.str);
return 0;
}
 
N

Neha

Hi all


I am very thank full to all of u to giving me a clear picture of the
void * and convertion to struct *. :)

Now i undersatnd what and where is my fault also how to solve it .. !

Actualy i am just trying this as test exp thats why naming convetion
are wrong. And specialy i wana use void * bcaz i want to malloc it for
diffrent diffrent struct * according the condition in my actual code.

Now i can arrange my code much better way .. !
Thanx a lot for ur valuable inputs .. !

Neha

Keith said:
Tom St Denis said:
What type is "a"?

Casting changes the interpretation in an expression, not the data type.
[snip]

That's an odd way of putting it.

A cast is an expression that specifies a conversion. The operand of
the cast is of some type (void* in this case); the result is of the
type specified in the cast (Bbbb* in this case). In both cases, we're
talking about types of expressions, not of objects.

Assigning a value to an object *never* changes the type of the object;
it can only change its value. The fact that that value happens to be
the result of a conversion to some type doesn't change that.

So let's look at what happens in the assignment statement

a= (Bbbb *) malloc(sizeof(Bbbb));

malloc() returns a result of type void*. You convert that result from
void* to Bbbb*. You then assign the result of the conversion (which
is of type Bbbb*) to an object of type void*. This is allowed because
there's an implicit conversion from void* to any pointer-to-object
type, and vice versa. So the result of malloc() is converted
explicitly from void* to Bbbb*, and then implicitly back to void*.

Here's how I'd write it:

#include <stdio.h>
#include <stdlib.h>

struct AAA {
unsigned char str;
};

struct BBB {
struct AAA ab;
};

int main(void)
{
struct BBB *a;
a = malloc(sizeof *a);
if (a == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
a->ab.str = 'c';
printf("%c\n", a->ab.str);
return 0;
}
 
C

CBFalconer

Neha said:
I am very thank full to all of u to giving me a clear picture of the
void * and convertion to struct *. :)

Now i undersatnd what and where is my fault also how to solve it .. !

Actualy i am just trying this as test exp thats why naming convetion
are wrong. And specialy i wana use void * bcaz i want to malloc it for
diffrent diffrent struct * according the condition in my actual code.

Now i can arrange my code much better way .. !
Thanx a lot for ur valuable inputs .. !

Don't top-post. See the links below. Do snip immaterial material
from your quotes. Don't use confusing childish abbreviations, such
as 'u', 'bcaz', 'wanna', 'ur'. Spelling counts.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top