Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > void * to struct * casting ..please help!

Reply
Thread Tools

void * to struct * casting ..please help!

 
 
Neha
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      11-30-2006
Neha wrote:
> 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

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      11-30-2006

Neha wrote:

> #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);

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-30-2006
Neha wrote:
> 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>

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-30-2006
Neha wrote:
> 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.

> return 0;
> }


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-30-2006
Neha wrote:
> 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;
}
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-30-2006
"Tom St Denis" <(E-Mail Removed)> writes:
> Neha wrote:
>> 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.

[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;
}

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Neha
Guest
Posts: n/a
 
      12-01-2006
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 Thompson wrote:
> "Tom St Denis" <(E-Mail Removed)> writes:
> > Neha wrote:
> >> 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.

> [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;
> }
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2006
Neha wrote:
>
> 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:
<news:news.announce.newusers
<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/>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
[Slightly OT] Casting non-void function results to void in modern compilers. David M. Wilson C Programming 8 01-07-2004 07:32 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments