List Info

Thread: Strange shifting variables




Strange shifting variables
country flaguser name
United States
2008-03-28 10:16:23

Hello!

I seem to be having a problem. Why are my struct variables shifting
after exiting a switch statement?

struct s {
int var1
int var2
}

switch (something) {
case 0:
s->var1=4;
s->var2=20;
printf("%d %dn", var1,var2);
break;
default:
break;
}
printf("%d %dn", var1,var2);

This outputs under x86:
4 20 (or in hex: 0x04 0x14)
4 20 (or in hex: 0x04 0x14)

This outputs under arm:
4 20 (or in hex: 0x04 0x14)
262144 1310720 (or in hex: 0x40000 0x140000)

Thank you!
Eric

__._,_.___
.

__,_._,___
Re: Strange shifting variables
country flaguser name
United States
2008-03-28 10:46:17

Hi Eric,

I'm going to try to help but this'll quick so it may not be perfect
but may get you going in the right direction so that I or others on
the list can help out better.

Looks like your sample of your code needs improvement. Did you mean:

struct s {
int var1
int var2
};

void caller()
{
struct s the_struct;

callie(&the_struct);
}

void callie(struct s *s)
{

// switch (something) { // Doesn't matter for troubleshooting
// case 0: // Ditto
s->var1=4;
s->var2=20;
// printf("%d %dn", var1,var2); // Your code
printf("%d %dn", s->var1, s->var2);
// break; // Doesn't matter for troubleshooting
// default: // Ditto
// break; // Ditto
// } // Ditto
// printf("%d %dn", var1,var2); // Your code
printf("%d %dn", s->var1, s->var2);
}

BTW, is is a general programming problem and not specific to any TS
product.

Happy to help.

----
Andy

--- In ts-7000%40yahoogroups.com">ts-7000yahoogroups.com, "berryma4" <berryma4...> wrote:
&gt;
> Hello!
&gt;
> I seem to be having a problem. Why are my struct variables shifting
> after exiting a switch statement?
>
> struct s {
> int var1
>; int var2
>; }
>
> switch (something) {
> case 0:
> s->var1=4;
> s->var2=20;
> printf(&quot;%d %dn", var1,var2);
> break;
&gt; default:
> break;
&gt; }
> printf(&quot;%d %dn", var1,var2);
>
>
> This outputs under x86:
>; 4 20 (or in hex: 0x04 0x14)
&gt; 4 20 (or in hex: 0x04 0x14)
&gt;
> This outputs under arm:
>; 4 20 (or in hex: 0x04 0x14)
&gt; 262144 1310720 (or in hex: 0x40000 0x140000)
>
> Thank you!
>; Eric
>;

__._,_.___
.

__,_._,___
Re: Strange shifting variables
country flaguser name
United States
2008-03-28 11:00:38

berryma4 wrote:
&gt; I seem to be having a problem. Why are my struct variables shifting
> after exiting a switch statement?
>
> struct s {
> int var1
>; int var2
>; }
>
> switch (something) {
> case 0:
> s->var1=4;
> s->var2=20;
> printf(&quot;%d %dn", var1,var2);

I would say it is a bug in the compilers that any of the prints worked at all.

There are several problems with your code. The first one is in the
assignments. s is delared as a struct and not a pointer to a struct. To
initialize the members of the struct you need to use s.var1 and not s->var1.
The print statements should also be using s.var1 and not just var1.

If you reference a structure member only by name, you wouldn't be able to tell
which var1 you would be accessing if you had two different structures that
both contained a member called var1.

--
Cheers!

Kevin.

http://www.ve3syb.ca/ |"What are we going to do today, Borg?";
Owner of Elecraft K2 #2172 |"Same thing we always do, Pinkutus:
| Try to assimilate the world!&quot;
#include <disclaimer/favourite> | -Pinkutus & the Borg

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )