Thread: how to de-reference from a pointer of values

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

    how to de-reference from a pointer of values

    Code:
    const char *configCmd[] = {r_Spread1, r_PowerSet5, r_bandSet, r_USAFCC, r_radioRst, r_getBandPlan, r_serialNumber, r_getPwr, r_spreadFactor};//, r_serialNumber};
    Code:
    const char *pTx;
    question 1: each of the items in configCmd is a "String"....I want to cycle through the items, stopping at each one and then cycling through the "String"...how do I do this using pointers to de-reference?

    I find if I do pTx= configCmd[0] points to the 1st item then *(pTx + x) cycles through the string itself. What I am not sure of is how to have the pTx reference move to the next item without directly doing a reassignment ie pTx = configCmd[1]???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    An array is converted to a pointer to its first element, so if you have an array of pointers, you need a pointer to a pointer to iterate over its elements. For example:
    Code:
    for (const char **p = configCmd; p != configCmd + sizeof(configCmd) / sizeof(configCmd[0]); ++p)
    {
        printf("%s consists of these characters: ", *p);
        for (const char *q = *p; *q != '\0'; ++q)
        {
            printf("%c ", *q);
        }
        putchar('\n');
    }
    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. Submenus and passing values by reference
    By thinkabout in forum C Programming
    Replies: 1
    Last Post: 06-05-2017, 11:51 PM
  2. Replies: 2
    Last Post: 04-20-2015, 09:22 PM
  3. Returning values by reference.
    By Kitt3n in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2010, 09:16 AM
  4. Replies: 22
    Last Post: 11-13-2009, 05:51 PM
  5. A reference to separate pairs of values
    By SevenThunders in forum C++ Programming
    Replies: 22
    Last Post: 04-03-2008, 05:06 PM

Tags for this Thread