![]() |
Confusion With Pointers and Arrays
Ok, Pointers confuse the heck out of me, and pointers to arrays only make things worse.
Right now, I've got a number of two dimensional arrays of structures where one of the elements of the structure is a string array. On startup, depending on the configuration of the system, only one of the arrays will be used. I need to handle this by using a pointer to point to that array. Basically I have no clue how to set up that pointer to correctly point to that array. Here is an example of my situation: :
// The structure (INT8U is an unsigned 8 bit character)Any help would be hugely appreciated. Like I said, Pointers confuse me to no end. |
I'd recommend reading either Narue's tutorial or Dawei's tutorial. If you can spare the time, read both ;)
|
I think a quick tutorial is in order. Its not that hard just getting to understand the syntax for a novice can be hard. Here I go.
I will start with a 1 dimensional array. A string will do as I am not feeling too creative tonight. :
#include <stdio.h>So that was one dimensional arrays, but what if you want two dimensions :
#include <stdio.h>So this is all the ways to get at the elements in MyArray MyArray[0][0] = *MyArray[0] = **MyArray MyArray[0][1] = *(MyArray[0]+1) = *(*MyArray+1) MyArray[0][2] = *(MyArray[0]+2) = *(*MyArray+2) MyArray[1][0] = *(MyArray[0]+3) = *(*MyArray+3) = *MyArray[1] MyArray[1][1] = *(MyArray[0]+4) = *(*MyArray+4) = *(MyArray[1]+1) MyArray[1][2] = *(MyArray[0]+5) = *(*MyArray+5) = *(MyArray[1]+2) MyArray[2][0] = *(MyArray[0]+6) = *(*MyArray+6) = *MyArray[2] = *(MyArray[1] + 3) MyArray[2][1] = *(MyArray[0]+7) = *(*MyArray+7) = *(MyArray[2]+1) = *(MyArray[1] + 4) MyArray[2][2] = *(MyArray[0]+8) = *(*MyArray+8) = *(MyArray[2]+2) = *(MyArray[1] + 5) So you can see the pattern and how it works. I will leave it for someone else to make some contrive example for a 2D array. :) If i have made a silly error then please feel free to kill me :p |
Quote:
Pointers are wonderful, and very powerful, but if they're leading you into great confusion, just use them very sparingly. When you're working with a 2d array, pointer notation in your code does NOT help it's clarity, one bit. Array[2]2], is infinitely clearer than any other notation, including pointer. Adak |
Quote:
:
JawaKing00 appears to understand the fact that a 2D array is simply an array of arrays, and he comprehends that a pointer can be assigned to an array. What he slips up on is to assume that arrays and pointers are the same thing. The correct approach can be inferred by DaWei's excellent and knowledgable guide to pointers. However, as far as I can see, the correct way of pointing to a 2D array is ever explicitly mentioned, so I'll mention it now. This is partially for JawaKing00's benefit, partially for mine; if I'm incorrect, I'd like to know about it! :
|
The relationship between pointers and arrays trips everyone up sometime. Heck, I typed a response to a question about passing multi-dimensional arrays to functions a couple of days back, and got it wrong too (that was at the end of a stressful couple of weeks, which started with undergoing knee surgery and then the pain of recovery and physiotherapy to get walking again). I can't find the thread at the moment, but I think Narue corrected my error.
The links to both Narue's and Dawei's tutorials given above are good, and should be read over and over again. They both cover the key issues quite well, although they are written in different styles that may suit different people. As Arevos has said, arrays are different things from pointers. However, C treats pointers and arrays as equivalent (i.e. interchangeable) in some contexts. A pointer is a variable that can contain the address of another variable. For example, a pointer to int can contain the address of an int. For example; :
int main():
int main():
int main()Multi-dimensional arrays are where this breaks down. A multi-dimensional array is an array of arrays, but (as of the 1989 C standard) it is not equivalent to a pointer to a pointer. In some contexts, it can be treated as a pointer to an array. For example; :
int main()Where things get a little squirrelly (and can trip up people - like myself - who used C before there was a standard) is that pre-standard versions of C did not recognise an array as a distinct type, so allowed an implicit conversion of an "array of array" to "pointer to pointer". That caused a lot of headaches for programmers (as compilers would not complain about some of those conversions when they really should have, and the program would simply crash for no obvious reason). One of the great services done by the committee that produced the 1989 C standard was that they disallowed this conversion. The trade-off is that most things relating to multi-dimensional arrays in C are difficult to get right, because compilers complain bitterly if the syntax is not "just so". But a whining compiler is much better for a programmer sanity than code which "looks right, compiles OK, but crashes for the end user": end users tend to complain more loudly than compilers. |
Quote:
I still don't quite understand why, though. I read through both of those tutorials, and I do have a passing knowledge of pointers and arrays, but sometimes they just make my head hurt. I get that that is a pointer to an array of structures, but why is there only one dimension? Wouldn't :
Also, why do we need to use the '.' and not '->' when accessing a member of the structure in that array? I thought when using a pointer, that the '->' notation was used, but again that doesn't work. Finally, why do we initialize the pointer as the whole array and not the address of the array? I would expect something like :
:
I guess my inexperience with pointers to arrays is showing here, but I'd like to know more about the reasoning behind this. Especially since I'll probably need to explain it to the lead engineer on the project, since he doesn't appear to fully understand this implementation either :D Thank you again for all the help. It has been incredibly useful. |
Quote:
:
:
:
Quote:
|
If you want to see the difference between an array name used as a pointer, and the usage of a pointer to the same array, simply write the two instructions (which look identical) and examine the emitted assembly code. In many cases the compiler babies us (at the cost of some confusion). After all, we all know we can't assign to arrays, but we all write char myStuff [] = "ABC";. ;)
|
:D Dawei, an assignment for you: write on the blackboard 100 times "Dawei should not tell lies".
|
| All times are GMT -5. The time now is 12:54 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC