-2
\$\begingroup\$

I want to get the value of an integer from within a struct pointer. Simply: int cool = *(*(&(coolPtr->map)) + x); can this be simplified? The rest of the code is context. X being the index of the array.

struct Cool {
    int map[5];
};


int value (Cool* coolPtr, int x) {

    return *(*(&(coolPtr->map)) + x); // Here
} 


int main() {

// Test
Cool* foo = new Cool;

// Fill
for (int i = 0; i < 5; i++) foo->map[i] = i*4;

// Check
cout << value(foo, 3); cout << endl; // 12, correct
cout << value(foo, 4); cout << endl; // 16, correct
\$\endgroup\$
5
  • \$\begingroup\$ @200_success that was not hypothetical code, that is my exact code. I just trimmed off the last bracket, namespace and including <iostream>. Try it, it compiles, I did not think those were needed, as anyone looking at that would see that it is functioning code. That is why I had a question, I had done it that way (posted code,) and I was wondering if there was a better way to do that. That, being the chain of pointers and references, commented with // here. \$\endgroup\$ Commented Sep 14, 2015 at 1:05
  • \$\begingroup\$ What is the Cool struct for? \$\endgroup\$ Commented Sep 14, 2015 at 4:12
  • \$\begingroup\$ @200_success just the name of an example struct with an array within. I needed to do this exact thing in my project, only with other names. This was to see how I could do this, before implementing it in my project. So this example stands alone... \$\endgroup\$ Commented Sep 14, 2015 at 4:58
  • \$\begingroup\$ We don't review example code here. If we can't tell what the task is that the code accomplishes, then the simple answer is going to be "well, don't do that". Hence our insistence that you tell us what you are really trying to accomplish. \$\endgroup\$ Commented Sep 14, 2015 at 5:00
  • \$\begingroup\$ Makes sense, I misunderstood "hypothetical" with "pseudo." Just to add, it's implementation would be: Cool changed to Map, map[5] with map[1024]. Value function the same (the part I was wondering.) In main I declare Map almost at the beginning. Fill would be filled with 0's and 1's. Then within the controls I call Value() to check for a 0 or a 1. The main project is a game. \$\endgroup\$ Commented Sep 14, 2015 at 5:07

2 Answers 2

4
\$\begingroup\$
  1. You should use one of the myriad accepted standard formatting-conventions.

    The things which stand out like a sore thumb are the newlines you have at the beginning of your two functions, the body of your for-loop being on the same line, and of course the content of main (which somehow lost it's closing bracket) having lost one level of indentation.
    Maybe the last is an artifact of not checking how it looks after posting here? Having taken a look at the source, that explanation seems unsatisfying...

  2. You should avoid using namespace std; (The other line you omitted is obviously #include <iostream>). It's a plague and just biding its time to bite you later.
    Why is “using namespace std” considered bad practice?

  3. Use '\n' instead of std::endl unless you might need to flush there.

  4. You can chain uses of the stream-inserter.

  5. Don't ever allocate something dynamically without a really good reason, and then use smart-pointers.

  6. Your value-function isn't complex enough to be usefully extracted into a function in general.
    Still, let's dissect the expression:

    *(*(&(coolPtr->map)) + x);
    

    Which is:

    1. dereference `coolptr` and get member `map`
    2. take the address
    3. dereference (undoing last step)
    4. add `x`
    5. dereference (equal to indexing with `x` together with step 4)
    

    Equivalent:

    coolptr->map[x];
    
\$\endgroup\$
1
  • 3
    \$\begingroup\$ I took the liberty to undelete this nice answer, I hope you don't mind! \$\endgroup\$ Commented Sep 20, 2015 at 9:36
4
\$\begingroup\$

Yes, that can be simplified at lot.

*(&(coolPtr->map))

The * and & "cancel out" (assuming the expression is well-formed and both are otherwise legal), so you can drop them both, which leaves you with:

*(coolPtr->map + x)

Which people will recognize as the expanded form of array access, so you can put it back into its usual form:

coolPtr->map[x]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.