Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VS 2008 Hex display selection not working AND a tip on X++; increments by 4 ! 1

Status
Not open for further replies.

Yorkshireman2

Programmer
Jan 21, 2005
154
CA
I'm using vs2008 programming in C code.
First my question then a discovery that I hope will help the rusty c programmers out there.

In debugging C code with VS2008, the tool tip over the variable (or in a watch window) is in hex! I tried selecting the right-click hex display option to on and off again but it still shows hex whatever I select.
Despite the numerous tips on these forums to change the hex display option it does nothing in my VS2008. Why? ...and how do I change it to decimal?

NOW the TIP...
I was puzzled by the discovery that addition and subtraction changed my variable by 4 instead of 1.

I tried x++; and it increased it by 4
I tried x+=1; it increased by 4
I tried x=x+1 it increased by 4

What was wrong? Well, this is the first time I have done some c code for a long while although I'm not new to it- just first time in vs2008

Why was it adding 4 instead of 1?
Well my variable was created as a pointer to a pointer but I was using the variable name and NOT the value pointed to.
So It finally came back to me about dereferencing pointers and I tried *x++ and that correctly points to the value that is pointed to by the variable.
(before it was changing the address of the pointer ! and thus it was changing by 4 each time because these addresses do move by 4.

Not the end of it though... then I found the ++ operator was not working with the new statement. So then I figured out by trial and error that you cannot use ++ with a pointer.
I changed it to *x = *x +1 and it works as I expect. So anyone else who is rusty note this.. the nice short-cuts like ++ do not work on the values when you are using pointers.




 
You can use ++ with pointers: it moves the pointer by the size of the item that it points to.
Code:
int* iptr;
int ia[20];

iptr = ia;  /* points to ia[0] */

int a = *iptr++; /* returns ia[0], then moves iptr to point to ia[1] */
int b = *++iptr; /* moves iptr to point to ia[2] and returns the value of ia[2] */
int c = ++(*iptr); /* increments value of ia[2] and returns the value of ia[2] */
int d = (*iptr)++;  /* returns the value of ia[2] then increments ia[2] */
 
Thanks xwb, my training never included those methods of parentheses around a pointer as you show. So I was incrementing the address and not the value.
I just tried ++(*iptr); and (*iptr)++ to increment the value and they do work!

I had begun to wonder if the difference is that I am accessing a dll with unknown contents; I have never done this before.
Rather than a character array in my own code, I create a pointer to a pointer to a variable inside this dll (the recommended way by the manufacturer to access their internal values).
The final product will run on an embedded system but this first stage is to test my code in a simulation. My code compiles into a dll that is called by the main software.
It works though- thanks again.

Have you ever come across the Hex/decimal display option for debugging not working?
 
Some items like addresses are always displayed in hex. Others, like IP addresses, you'd normally display in decimal.

If a pointer value on a 16 bit machine was FB2E, and you knew that F000-FFFF was in ROM, then you'd know that it was pointing in the ROM. If you got -1234, it would be quite difficult to decipher.

If the IP address was 5001a8c0, it wouldn't be as recognizable as 192.168.1.80.

All forms have their uses.
 
Ah yes this makes sense now, together with the fact that I was originally seeing the address.
Now that I use it properly I do see that the tooltip has both the pointer address AND a plus sign to expand and show the value!

Thanks again xwb. The "murky cloud" is lifting again. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top