Okay; still struggling with understanding much of this. I get the bit about the unrolling of the 2D array, but I'll try and concentrate just on the first example problem for now, and worry about the rest of it once I have my head wrapped around the first one.
So, 1) *p=(char *)t;
Okay, so what are you actually naming this construct in your head? If it was simply "char *p", for example, I'd say it's "a pointer of char-type named p". But I don't know how to express in words what this "*p=(char *)t" is even creating.
Second; it seems a complicated way to create a pointer. Why can't one just say something like (pseudo-code)
char *p=t;
, where *p would presumably point to t[0][0]. And why the need to say "char" again; it was already declared as char at the beginning of the line.
You probably must think I'm as dumb as a rock, but in an effort to figure all this out I spent about three hours in B&N yesterday reading a chapter of a C++ book on pointers, and then looked at some more stuff online. Yeah, this hasn't clicked yet, but hopefully one day!
Anyway, as part of my investigation, I came across a Wiki page which gave an example of:
1 2
|
char *pc[10]; // array of 10 elements of 'pointer to char'
char (*pa)[10]; // pointer to a 10-element array of char
|
So, ever the adventurer, I tried to incorporate this into a tiny little program to help me try and understand this pointer stuff, but everything I try ends up with segmentation errors (which, as I'd already learned, is probably related to the program trying to access memory space which it doesn't have access to):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
int main() {
char *pc[10]; // array of 10 elements of 'pointer to char'
char (*pa)[10]; // pointer to a 10-element array of char
for (int x=0;x<10;x++)
*pa[x]=x;
for (int x=0;x<10;x++)
std::cout << *pa[x]; // same seg fault using either *pa or pa.
return 0;
}
|
I'm obviously missing something entirely in this pointer conversation! Any [more] help appreciated.