473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Why the prgram crashes when somebody tries to add two pointers?

Why the prgram crashes when somebody tries to add two pointers.Is C language built like that or adding two pointers will cause the problem somehow?Please answer to this I am bit confused.
Jul 29 '09 #1
3 2419
JosAH
11,448 Expert 8TB
@malojinaik
You can't add two pointers; you can add integers to pointers or you can subtract two pointers.

kind regards,

Jos
Jul 29 '09 #2
donbock
2,426 Expert 2GB
@malojinaik
Do you mean that you tried to add two pointer values together (like pab below)?
Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int b;
  3. int *pa = &a;        // The value of pa is the address of a.
  4. int *pb = &b;        // The value of pb is the address of b.
  5.  
  6. int *pab = pa + pb;  // What does this mean?
  7. int c = *pab;        // !!! Danger !!!
  8.  
I suppose you can add two pointer values together, but to what end? What do you think that is going to accomplish?

Suppose the address of a is 0x0200 (that's the value of pa) and the address of b is 0x0204 (that's the value of pb). In that case, the value of pab is 0x0404. What is stored there? What happens when I try to assign a value to c? Either address pab is outside the bounds of my program and we crash with an access error; or worse, some arbitrary garbage value is pulled from somewhere in my program and written to c.
Jul 29 '09 #3
JosAH
11,448 Expert 8TB
@donbock
No you can't: the compiler (neither C nor C++) won't let you unless you explicitly cast at least one of the pointer values to an int. As you showed already it doesn't make sense.

kind regards,

Jos
Jul 29 '09 #4

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

Similar topics

5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
12
by: Paul | last post by:
Hi, Global operator new and delete are overloaded and I am using stl map to store pointers, but this code crashes, can some one shed some light??? Compiler: MS VC++ 6.0 STL: Shipped with...
9
by: greeningster | last post by:
I have written an application in Visual C++ for a customer but it seems to crash randomly. Could anyone give me any help on how I could track this down ? Also, there appears there might be...
1
by: Sri | last post by:
When I try to set a value to a bound control in the Form_current event, Access crashes and tries to create a report to Microsoft. If I comment out this code, it works fine. Any help would be great....
10
by: Steven | last post by:
The following program runs alright until the return statement, where it crashes, and consequently, Dr. Watson (drwtsn32.exe) also crashes. Deleting the call to fgets() and everything runs fine. I...
0
by: I Decker | last post by:
Hi all, Hope this is the right group. I am writing a program in c# to open create an excel document, enter some data, save it and then email it as an attachment. I have successfully created...
8
by: gk245 | last post by:
This is part of a bigger program, but i made it simple (basically the OT_hours function is supposed to determine if the number entered is over 40 hours or not and return the appropriate answer): ...
1
by: powersof2 | last post by:
Hello, I need some help with SQL Server 2000 SP4. I've been using the developer product for a few years now on my PC with very few problems. Today when I try to start up Enterprise Manager, it...
0
by: Jameson | last post by:
Visual Studio 2008 (90 day trial, team suite edition) crashes with "Microsoft Visual Studio 2008 has encountered a problem and needs to close...." message when I try to create a new wpf application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...
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,...
0
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...

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.