473,385 Members | 1,311 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,385 software developers and data experts.

Weird behavior of a pointer

The program below should outputs 'a' but it doesn't unless the commented line is uncommented.

I use MinGW compiler.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char *cp;
  4.     char a = 'a';
  5.     char b = 'b';
  6.     cp = &b;
  7.     cp+=1;
  8.     //printf("ap=%p\n",&a);
  9.     printf("%c", *cp);
  10.     return 0;
  11. }
  12.  
Jul 24 '11 #1
5 1432
try changing line 7. with cp-=1; ... because you declare a before b, therefore b comes after a.
Jul 25 '11 #2
@Faldahlawi
This is not true actually.
Although a is defined before b in the code, b has a lower address than a. It should be cp+=1 because cp is initially set to &b.

Note: If I changed cp to be initially set to a and changed the operation to cp-=1 , I still have the same weird behavior that the output is not correct (b) unless I print &b somewhere in the code.
Jul 25 '11 #3
alexis4
113 100+
In general it is bad programming to assume where a variable will "seat" and read it with a pointer. Put these variables in an array or struct to organize your memory and then read with the pointer.
Jul 25 '11 #4
This is because &a and &b are not consecutive. (Unless you store it in an array.) Still if you want to proceed, get the difference of address by x = &a-&b. Then do cp+=x. It will work.
But it is not good programing practice. Use array concept.
Jul 26 '11 #5
Banfa
9,065 Expert Mod 8TB
As alexis4 says under no circumstances should you assume anything about the location of a variable that has not be obtained directly using the address of (&) operator and the rules given in the C/C++ standards.

How the compiler/linker organises memory is completely platform dependent, the standard does not even mandate the heap and stack that most programmers talk of when memory operations rear their ugly head. The platform is free to do anything it wants in this respect.

If you do this at best you will produce non-portable code that will be hard to maintain and at worst you will introduce undefined behaviour which will of course work fine up until the point it becomes critical and then fail i.e. it will work during testing and fail in front of the customer.
Jul 26 '11 #6

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

Similar topics

0
by: Tom Dauria | last post by:
I inherited a weird application. Part of it is in VB6 and part in Access 2000. All of the reports and some of the forms are in Access 2000. The database is opened by the VB application. ...
1
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4...
0
by: Shaul Feldman | last post by:
Hello, I have a couple of things: 1) a page contains a button that has some JS code attached to it programmatically. Also the page loads a web user control with LoadControl method. In UC I...
5
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
8
by: Su Y | last post by:
hi all, I can't understand how this code work, its behavior is really weird for me... I want find the first number in extend which is larger than num, so I wrote: def find(num): count=0 for...
4
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors...
4
by: Clint Weisbrod | last post by:
All, I'm creating an STL map object like this: map<double, stringmyMap; I add a few items to the map using the operator. If I iterate over the map like:
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
1
guillermobytes
by: guillermobytes | last post by:
Hi everyone, I need some help here with a situation where I have an array and a call to reset() or even when assigning that array to another variable the internal pointer does not point to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.