Thread: comparing the address of a pointer to an array address for size

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

    comparing the address of a pointer to an array address for size

    Good afternoon....
    Is it possible to compare the address of a pointer to the address of an array as it is incremented for use as a pointer....so instead of counting with a variable and reaching termination count I was wondering if I could do

    Code:
    while (*(&pTx) == &(mssg[5]));
    So if pTx is getting incremented in code within an ISR as such, pTx++, I wanted to wake up at the tail of the ISR and go back to main and act on the above while loop to see if it had reached the address of array mssg[5] ...Note that pTx = mssg; early on in code.

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Sure, but it's just:
    Code:
    while (pTx == mssg + 5)
        ;
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Great I will try it.....So that compares the lvalues of the two?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    I don't think the term "lvalue" is correct here.
    Addresses are basically just unsigned integers.
    Suppose pTx contains 1020 (i.e., it "points" to address 1020), mssg contains 1000, and the size of the objects to which the pointers point is 4. Then the comparison is just 1020 == 1000 + 5 * 4, which is true.
    I wouldn't call 1020, or the result of the addition an "lvalue", although I'm not entirely sure.

    EDIT: Actually, now that I read the following, maybe it is an lvalue. The details of "value categories" confuse me.
    Value categories - cppreference.com
    Last edited by john.c; 04-17-2020 at 06:08 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    pTx is an lvalue, and from context it is a modifiable lvalue. (mssg + 5) is an rvalue: it is only the "value of an expression", not an object "locator value", i.e., while it is an address, it doesn't necessarily have an address. The cppreference article lists it as a prvalue, but that's a C++ term that is a subset of the set of rvalues.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Passing of an array address to a pointer
    By Shankar k in forum C Programming
    Replies: 2
    Last Post: 04-26-2015, 09:13 AM
  3. Replies: 3
    Last Post: 05-25-2011, 11:30 AM
  4. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM

Tags for this Thread