Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question regarding pointer typecasting and manipulation

Reply
Thread Tools

Question regarding pointer typecasting and manipulation

 
 
Arun Prasath
Guest
Posts: n/a
 
      11-24-2003
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
 
Reply With Quote
 
 
 
 
James Hu
Guest
Posts: n/a
 
      11-24-2003
On 2003-11-24, Arun Prasath <(E-Mail Removed)> wrote:
> 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))


This cast is unecessary. Remove it.

>
> /*begin code*/


Wasn't the #define above code?

>
> struct node{
>:
> } node;


This won't compile.

>
> typedef node *nodeptr;


Even after fixing struct node above, this won't compile.

> node **nodeptrptr;


Ditto here.

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


Since the declaration of nodeptrptr didn't compile, it is undefined
at this point.

>
> /*end code*/
>
> Is the above code valid? If so, can anyone explain how it works?


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

It seems you intended to create a typename called node.

> How can a pointer to a memory chunk be typecast to a pointer to a
> pointer to a memchunk?


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
 
Reply With Quote
 
 
 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      11-26-2003
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!

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


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"?
 
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
regarding typecasting venkat C++ 9 06-06-2008 09:29 AM
what is typecasting a pointer to the type (void *)p mean? Abhishek C Programming 16 01-26-2006 04:47 AM
typecasting towards a pointer to a K&R style declared function dis C Programming 2 06-11-2004 10:36 PM
Advanced pointer typecasting Robert Street C++ 3 02-21-2004 01:33 AM
Typecasting char pointer to structure venkatesh C++ 1 12-06-2003 06:46 AM



Advertisments