Reputation: 1000
Sry, this must be answered somewhere but I haven't find it.
I have a code:
const int * tableCards[9];
int hand1card1 = 0;
tableCards[0] = & hand1card1;
int hand1card2 = 1;
tableCards[1] = & hand1card2;
int * flop1 = & tableCard[0];
tableCards[2] = flop1;
cout << * flop1 << endl;
cout << * tableCards[2] << endl;
flop1++;
cout << * flop1 << endl;
cout << * tableCards[2] << endl;
tableCard[48] is an array of pointers to ints
This code works, but I want to change the line tableCards[2] = flop1;
to make the * tableCards[2]
return the actual value of element pointed by * flop1
. In this code the *flop1 returns the value I want but *tableCards[2] remains the same after incrementing the flop1.
I thought that tableCards[2] = & (* flop1);
will assign the address of the value pointed by *flop1 to int pointer tableCards[2] but after incrementing flop1, the tableCards[2] value remains the same. I don't want to use array of pointers on pointers instead array of pointers, because 4/9 values needed are just ints, not pointers on ints.
Edit: Note that tableCards[9] is different array than tableCard[48]. And sry, this question must be confusing when I see the comments and answers.
Upvotes: 0
Views: 1964
Reputation: 63461
This shouldn't compile:
int * flop1 = & tableCard[0];
The type of tableCard[0]
is int*
, so when you reference it with &
it becomes int**
. You are assigning that to an int*
. At the very least it should be generating a compiler warning.
So, flop1
points to the address tableCard[0]
, *flop1
gives you the value (a pointer) stored at tableCard[0]
, and **flop1
gives you the value that tableCard[0]
points to. But because you have given flop1
the wrong type (int*
instead of int**
), you can't do this.
I base this on the comment you made that you have another array:
int * tableCard[48];
I think what you meant to do is:
int * flop1 = tableCard[0];
tableCards[2] = flop1;
Now, when you increment flop1
it just changes the pointer. The pointer at tableCards[2]
will still point to the same value as tableCard[0]
points to.
But THIS is very naughty:
flop1++;
cout << * flop1 << endl;
flop1
is the pointer stored at tableCard[0]
. When you increment that and then use it, you are treating it as an array. Maybe it is? But probably it's not. You do not know what memory flop1
now points to.
[edit] After having more of a think about what this code might be trying to achieve...
So indeed, if you are expecting flop1
to now point to the second array element, you need to use a double-pointer (int**
). This means that flop1
is now an iterator in your tableCard
array, rather than an element... So when you increment it, it now points to the pointer stored at tableCard[1]
while tableCards[2]
still contains the value of (but doesn't point to) tableCard[0]
.
int ** flop1 = & tableCard[0];
tableCards[2] = *flop1;
cout << **flop1 << endl;
cout << *tableCards[2] << endl;
flop1++;
cout << **flop1 << endl;
cout << *tableCards[2] << endl;
Further edits, based on inference once again... I suspect that maybe you want the tableCard
array to be defined as:
int tableCard[48];
In that case, your code should otherwise be correct with no other changes required. Well, presumably. The whole lot seems a bit suspect.
Upvotes: 1