Connecting Tech Pros Worldwide Forums | Help | Site Map

Question regarding pointer typecasting and manipulation

Arun Prasath
Guest
 
Posts: n/a
#1: Nov 13 '05
Hi all,

I have the following question regd pointer typecasting. Is the
following type of pointer typecasting valid?


#define ALLOC(type,num) ((type *)malloc(sizeof(type)*num))

/*begin code*/

struct node{
:
} node;

typedef node *nodeptr;
node **nodeptrptr;

nodeptrptr = (nodeptr *)ALLOC(node,NUM_OF_NODES)

/*end code*/

Is the above code valid? If so, can anyone explain how it works? How
can a pointer to a memory chunk be typecast to a pointer to a pointer
to a memchunk?

Thanks,
Arun

James Hu
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Question regarding pointer typecasting and manipulation


On 2003-11-24, Arun Prasath <arunprasath420@yahoo.com> wrote:[color=blue]
> I have the following question regd pointer typecasting. Is the
> following type of pointer typecasting valid?
>
> #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num))[/color]

This cast is unecessary. Remove it.
[color=blue]
>
> /*begin code*/[/color]

Wasn't the #define above code?
[color=blue]
>
> struct node{
>:
> } node;[/color]

This won't compile.
[color=blue]
>
> typedef node *nodeptr;[/color]

Even after fixing struct node above, this won't compile.
[color=blue]
> node **nodeptrptr;[/color]

Ditto here.
[color=blue]
>
> nodeptrptr = (nodeptr *)ALLOC(node,NUM_OF_NODES)[/color]

Since the declaration of nodeptrptr didn't compile, it is undefined
at this point.
[color=blue]
>
> /*end code*/
>
> Is the above code valid? If so, can anyone explain how it works?[/color]

The above code is invalid. It can't possibly work.

It seems you intended to create a typename called node.
[color=blue]
> How can a pointer to a memory chunk be typecast to a pointer to a
> pointer to a memchunk?[/color]

If you remove the cast in the ALLOC macro, you won't need the
cast in the assignment to nodeptrptr. However, the assignment
is likely wrong, depending on how you define struct node.

-- James
Peter Shaggy Haywood
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Question regarding pointer typecasting and manipulation


Groovy hepcat Arun Prasath was jivin' on 23 Nov 2003 22:38:09 -0800 in
comp.lang.c.
Question regarding pointer typecasting and manipulation's a cool
scene! Dig it!
[color=blue]
>I have the following question regd pointer typecasting. Is the
>following type of pointer typecasting valid?[/color]

Here's a thought. Why don't you read the responses you got the first
time you asked this?
FCOL, Usenet is not an instantaneous medium. You have to wait for
responses. They could come within seconds, minutes, hours, days or
even weeks. I've even seen responses come *months* after the original
post was forgotten.
So post once and only once, then *wait*!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Closed Thread