473,767 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding pointer typecasting and manipulation

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,NU M_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
Nov 13 '05 #1
2 4179
On 2003-11-24, Arun Prasath <ar************ @yahoo.com> 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,NU M_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
Nov 13 '05 #2
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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1819
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP. I have looked for a python web framework to build a web site, a site that I had start in php (and quite finish), but for some reason I wont explain, I lost everything. I have started writting app with python 6
3
2214
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace myErrorHandler
7
2458
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to *(MyArray+x), what statement is functionally identical to MyArray? I ask this question because when I create a dynamic array with one
6
1555
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ?? ....... }
8
2051
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\ {\
2
1822
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
2
3800
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long Align; union header { struct {
10
1910
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
5
2238
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.