Reputation: 31
I thought I understood basic concepts of pointers as per C tutorials, but I really get confused when actually coding. I have some questions:
1 - Lets say I have:
customList arrayOfLists[3]; //global
//...
void someMethod()
{
int i;
for (i=0; i<3; i++)
{
customList l = arrayOfLists[i];
//METHOD1
//Next time I come back to this function, element is still there!
removeFirstElement(&l);
//METHOD2
//Next time I come back to thsi function, element is gone!
//removeFirstElement(&(arrayOfLists[i]));
}
}
Why would both Method1 and Method2 not work the same way? In both cases I am basically saying remove the first element of the list located at addressX. Does it create a copy of the array?
2 - What is the difference between:
struct1.ptr1->ptr2->someIntValue //1
&(struct1).ptr1->ptr2->someIntValue //2
The 2nd way doesn't make sense to me and I don't really know what's going on there, but it seems to work. I was expecting the 1st way to work but it is not giving me the right answer.
3 - Lets say I do this:
ptr2 = ptrToStruct;
ptr1 = ptr2;
ptr2->intProp = 5;
ptr2 = ptrToStruct2;
Is ptr1 pointing to the initial memory location still, or is it the same ptr2? What is ptr1->intProp?
Thanks.
Upvotes: 3
Views: 232
Reputation: 215
Pointers are hard to grasp because they are abstracting the memory of the computer without referring to that memory in the description of the operation you can do with pointers.
Pointers are much easier to understand if you learn a bit of assembler.
Upvotes: 1
Reputation: 134601
For question 1, method 1 is taking the address of the variable l
. l
is initialized to hold a copy the value of the i
'th item of the array. This is a completely different memory location. Note that if the removeFirstElement()
function depends on the the argument being a pointer to the array, it will fail miserably using method 1.
struct1.ptr1->ptr2->someIntValue
. Note that you did not take the address of the struct, but the member of the member of the member it was pointing to. You probably meant it to be as:
(&struct1).ptr1->ptr2->someIntValue
This would fail miserably.
The member access operator (.
) is used on struct values to access a member. What you have on the LHS is a pointer to a struct and should be using the arrow operator (->
) to access the member. Or, dereference the pointer first, then use the dot (what the arrow operator is equivalent to).
(*(&struct1)).ptr1
/* is equivalent to: */
(&struct1)->ptr1
ptr1
and ptr2
are pointers to whatever struct ptrToStruct
is pointing to, then yes, ptr1
is still pointing to whatever it was pointing to. You never modified the ptr1
variable, only the struct that it was pointing to. Since both ptr1
and ptr2
are pointing to the same structure (in other words, aliased), you should see the same value when accessing the member ptr1->intProp
.
Upvotes: 0
Reputation: 108986
customList
is a local pointer; arrayOfLists
is a global array. They live in different memory regions.
The &
has lower precedence than .
or ->
. If I were to simplify the 1st expression to j
, the 2nd expression would be simplified as &j
.
Pointers are just like any other type. In the code below
int j, k;
k = 5;
j = k;
k = 12;
does j have 5 or 12?
Upvotes: 0
Reputation: 34655
Q3)
ptr2 = ptrToStruct;
ptr1 = ptr2;
ptr2->intProp = 5;
ptr2 = ptrToStruct2;
Is ptr1 pointing to the initial memory location still, or is it the same ptr2?
Yes, ptr1
points to the memory location where ptrToStruct
is pointing to. Affecting ptr2
to point to a different location (ptrToStruct2
) doesn't affect ptr1
.
What is ptr1->intProp?
5.
Upvotes: 0
Reputation: 39194
customList l = arrayOfLists[i];
//METHOD1
//Next time I come back to this function, element is still there!
removeFirstElement(&l);
This is wrong: you need something like:
customList *l = &arrayOfLists[i];
removeFirstElement(l);
For the second question:
struct1.ptr1->ptr2->someIntValue // this will give you someIntValue
&(struct1).ptr1->ptr2->someIntValue // this will give you the address of memory where someIntValue is stored
Upvotes: 0