help understanding pointers and arrays

This is a discussion on help understanding pointers and arrays within the C Programming forums, part of the General Programming Boards category; The following few questions are some ideas that my professor said that I should know before class tomorrow. I get ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Pick one and give it your best shot.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    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. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    for number 1:
    address 1019 contains a0
    address 1020 contains b1

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    I have them all figured out except number 3. If someone coule explain number 3 to me that would be awesome.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    What happens to the values pointed to by pINTARRAY1 and pINTARRAY2 if I change certain elements of myarray?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    Yeah, I totally misread that...listen to nonoob.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    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. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Need Help understanding arrays and pointers
    By flicka in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 09:45 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21