473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting Pointers in C - Urgent

1 New Member
I need to get this right ....

suppose I have an array :char p[MAX] //MAX a defined number

we also have a defined structure header,
struct header {...
...}

having the following code

struct header *h;
h = (struct header *) p;

how would this really be reflected in memory;

h and p will be pointing to the same area of memory ?

we can still use p as pointer to the array...

just tell me how are the pointers and the refernced object are affected by casting
??
Jul 4 '07 #1
2 1186
Meetee
931 Recognized Expert Moderator Contributor
h and p will be pointing to the same area of memory ?

Yes, both h and p are pointing to the same area of memory.
You can check it by this way

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. struct header
  3. {
  4. //int i = 5;
  5. }*h;
  6. int main()
  7. {
  8. char p[10]={'a','b','c'};
  9. struct header *h;
  10. h = (struct header *) p;
  11. printf("%u\n",h);
  12. printf("%u\n",p);
  13. printf("%s\n",*h);
  14. printf("%c\n",p[2]);
  15. return 0;
  16. }
  17.  
we can still use p as pointer to the array...

Yes :))

Regards
Jul 4 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
char p[10]={'a','b','c'};
struct header *h;
h = (struct header *) p;
will cause you trouble. The array p points to three chars in memory. That's three bytes.

An h* is a pointer to a struct with an int as a member. That typecast is you lying to the compiler about what is really in memory. In this case the cast says an int (4 bytes).

Now when you use h you will corrupt memory becuse you don't own 4 bytes. Further, you will corrupt the the chars with your int value.

Now you crash.

Please do not code this way.
Jul 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
8
by: rihad | last post by:
Hi, I've this question: suppose we have two differently typed pointers: struct foo *foo; char **array; Becase one can always legally cast any pointer to (void *), and becase (void *) is...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.