# Thread: help understanding pointers and arrays

1. ## help understanding pointers and arrays

The following few questions are some ideas that my professor said that I should know before class tomorrow. I get really confused with pointers so some help would be great!

1. Assume an array is declared as follows: int myarray[12];. Assuming that int is represented with two bytes, and that array starts in the memory at address 1000, show a memory layout indicating the addresses for this array elements. Let myarray[10] contain the value 0xa0b1. Show the contents of memory at addresses corresponding to the two bytes of myarray[10] for Big-endian convention.

2. In addition to the array above, let us also declare a couple of pointers:

int *pINTARRAY1, *pINTARRAY2; //Line 1

pINTARRAY1 = myarray+4; //Line 2

pINTARRAY2 = &myarray[10]; //Line 3

*pINTARRAY1 = 0x1013; //Line 4

*pINTARRAY2 = 0x1d2e; //Line 5

What is the L-value and R-value of the variables pINTARRAY1 and pINTARRAY2 after Line 3? (If you do not have enough information to answer the question, make assumptions and state them clearly). Which myarray values are written in Lines 4 & 5 and with what values?

3. In Problem 2 above, assuming there is more of a program following it, what can you say about the potential values the pointers pINTARRAY1 and pINTARRAY2 can hold during the program execution? Explain your answer.

4. Given the following program fragment:

unsigned int num_array = {100, 200, 300, 400, 500, 600, 700};

unsigned int *ptr_array;

unsigned int x, y;

ptr_array = num_array;

x = *ptr_array++;

y = *ptr_array++;

What are the values of x, y, ptr_array through this execution? Explain.

5. Compile a program similar to the Problem 1 and 2 above for AVR Atmel Mega128. Declare an array of 12 integers. Declare a pointer to integers. Make it point to the starting address of this array added to a long variable. Should the compiler be able to detect an error for this pointer assignment? Compile it and see if the compiler complains?

2. Pick one and give it your best shot.

3. Ah, I remember when pointers confused the heck out of me too.

Code:
```pINTARRAY1 = myarray+4; //Line 2
pINTARRAY2 = &myarray[10]; //Line 3```
- First line takes the address of myarray and, assuming int is 2 bytes, incrementing myarray by 4 will increment the pointer by 8 addresses.
- Second assigns the address of the 11th element of myarray (not the value) to pINTARRAY2

4. for number 1:

5. I have them all figured out except number 3. If someone coule explain number 3 to me that would be awesome.

6. What happens to the values pointed to by pINTARRAY1 and pINTARRAY2 if I change certain elements of myarray?

7. No. The values pointed to will change... that's redundant.
"what can you say about the potential values the pointers pINTARRAY1 and pINTARRAY2 can hold ..." refers to values of the pointers themselves. They can hold any values pointing all over the place, including NULL.

8. Yeah, I totally misread that...listen to nonoob.

9. The question is forcing the student to think about pointers in general, I think. The fact that they can point to any arrays, not just 'myarray', but rather before it, or beyond it, or any other variable in memory. But as to what values they can hold? It's a strange question. Any value at all. Maybe it's a trick question.

10. I was thinking it was any value at all. Then for some reason I was thinking that it could only point to myarray. I'm kind of confused on this

11. It can hold any address. Whether or not it points to a legitimate value is your problem. There are no compiler checks. It's one of the main sources of programming errors that causes crashes.

12. You're right. The pointers can store any value at all. An address (what you put in a pointer variable) is really just a number, so you can put in any number you want. In fact, an address could point to something in myarray or some other int variable, or they could have the value NULL, or they could point to some arbitrary address that doesn't actually contain space to store an integer. If they point to NULL or some bogus address, you will likely have major problems if you try to read or write the value pointed to, like *pINTPOINTER1 = 42. This could result in a seg fault or other program crash, bizarre behavior, overwriting some other data, or no symptoms whatsoever.

And going back to your answer to question one, I think you have an off by one error. myarray[0] should be at addresses 1000 and 1001, myarray[1] at 1002 and 1003, etc.