Thread: simple array question

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    simple array question

    Given :
    Code:
    char RXData[20] = { [ 0 ... 19 ] = 0xFF }, *rxMssg;
    Why is the following incorrect?

    Code:
    rxMssg = &RXData;
    I understand that
    Code:
    rxMssg = RXData;
    and
    Code:
    rxMssg = &RXData[0];
    work.....I just don't quite get why &RXData does not work

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because &RXData is a pointer to the whole array, whereas RXData and &RXData[0] are pointers to the first element of the array.

    Write char (*rxMssg)[20] = &RXData;
    if you want to point to the whole array.

    Compare with
    Code:
    struct foo {
      int bar;
    };
    
    struct foo var;
    struct foo *p = &var;
    int *q = &var.bar;
    p and q point to the same address, but the types are different.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    I am not sure I understand.....How can an address point to "the whole array"?....An address points to a single location... Your comment seems to imply that all 20 elements of the array can be pointed to by one address

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, and that's the location of the array.

    After all, an address doesn't "point" to anything. It's just a number used to conceptually label some byte in memory. A pointer, on the other hand, conceptually points to some object by having both the address of the starting byte of that object, as well as having a type from which the number of bytes occupied by that object can be derived (and hence the "stride" in the sense of how many bytes are skipped when incrementing the pointer).

    Consequently, a pointer can point to an element of an array, and another pointer of a different type can point to the entire array, even though the numeric address assiciated with both of them are the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    But isn't the address of an array defined by it's 0th element? I understand an address is a 'placeholder'. I also understand that the right hand side of a pointer is an address. In my expressions window if I put in array....I get 0x2020 and all the values. This 0x2020 is the element of the 0th element. If I put in to the expressions window &array I get the same thing.

    Can you supply an example to show me ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's right. Hence you need to look at the type of the pointer, not just the numeric address associated with it.

    Given an array x of 10 ints, x is converted to a pointer to the int object at index 0, so this pointer is of type int* and is equivalent to &x[0]. But x itself is of type int[10], so &x is a pointer to the entire array, i.e., int (*)[10]. This is basically what Salem told you in post #2.

    If it helps, think about what happens when you have an array of arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Thank you....That last line really helped!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array question
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 05-05-2008, 10:00 AM
  2. Another Simple Array Question
    By deadhippo in forum C Programming
    Replies: 26
    Last Post: 03-29-2008, 09:04 AM
  3. Simple Array Question
    By deadhippo in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 11:28 PM
  4. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 10:43 PM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM

Tags for this Thread