Respuesta :

tonb
Wow, nice but tricky. First of all, we have to assume an endiannes, because it is not specified. Let's assume the most common little endian. This is important for the integer and pointer storage.

OK, here goes, step by step:
The program initializes variables a,b and c.

a=10;
This causes the values 10,0,0,0 to be written at 5000.

b='A';
This causes 65 to be written at 5004

char c[3];
Has no effect (yet).

Our memory now looks like:

5000: 10 0 0 0 
5004: 65 xx xx xx (xx means we don't know)

Then, the c[] array gets initialized:

c[0]='B';
c[1]='C';
c[2]=0;

66 gets written to 5005
67 gets written to 5006
0 gets written to 5007

Our memory now looks like:

5000: 10 0 0 0 
5004: 65 66 67 00

Now we're going to call the CS262_Function(). It takes two pointers. A pointer is just a memory address. Pointer x will have the address of a, so x=5000.Pointer y will have the address of the first element of c, so y=5005

Our memory around 6000 will look like this:

6000: 136 19 0 0
6004: 141 19 0 0

How did I get this? Well, integer 5000 is 0x00001388 hexadecimal, which when reversed and converted to decimal, gives:

136 19 0 0 (0x88 = 136; 0x13 = 19)

Now the code inside the function will manipulate the memory:
*x = *x + y[0]

The integer originally called a is now called *x. The first element of array y[] gets added, which acutally is array c[].

So *x = 10 + 66 = 76

y[1]=y[0]+1

The memory cell that used to hold 67 now gets overwritten by 66+1 which is... 67 again.

y[0]=y[2]

The zero byte gets copied to the first location. The memory now looks like this:

5000: 76 0 0 0 
5004: 65 0 67 00

The second invocation of the function passes the same integer pointer, but now references b in stead of c. So one pointer address slightly changes:

6000: 136 19 0 0
6004: 140 19 0 0

If you follow the calculations in the same way, you'll find that the memory changes to:

5000: 141 0 0 0 
5004: 67 66 67 00

I hope I didn't make a mistake but if I did you should be able to spot it.