Pointers have several uses. Their notable properties are that they hold addresses. They're distinguished by the type of data of which they hold the address of. They're variable as opposed to array names or references. When incremented they
A declared array represents the address of the first element of the array.
int myInts [10];
Using
myInts in any expression returns the address of the [0] element.
int *intP; // pointer to an integer
intP = myInts; // assigns the address of myInts to intP
intP++; // increments the address stored in intP to point to the second ([1]) element
intP = new int [10];
// asks the system to allocate room for ten ints and return the address of the allocated
// space which is assigned to intP
// you can now reference intP as if it were an array of ints
int abc = intP[4];
delete [] intP; // tells the system to reclaim the previously allocated space
// the address stored in intP is no longer valid for use
When using a pointer to dynamically allocated space you should not change the value of the pointer, though you can change the value of the space pointed to by the pointer.
*intP = 436; // changes the integer pointed to by intP
*(intP + 3) = 634; // changes the fourth element of the dynamically assigned array space.
The same can be done for characters. Pointers are often used to traverse nul terminated strings because it's usually faster to increment the pointer than it is to index the array.
dbStr() internally gets dynamically allocated space and returns the address of that space. You're duty is to assign it to a char *.
char *p = dbStr(xxxx);
The thing you need to keep in mind, and which isn't very obvious, is that you're also responsible for telling the system it can return the space back to the heap memory.
delete [] p;
Each call to dbStr() can potentially give you a different address and certainly will if you don't give back the memory you received previously. Never change or re-use a pointer to dynamically allocated memory until you've de-allocated the memory. If you need to track more than one memory space use additional pointers.
It's also a good practice to initialized these pointers to NULL and to set them to NULL when you've de-allocated the memory they referenced. Then you can use this value to test if the pointer is available for use.
Lilith, Night Butterfly
I'm not a programmer but I play one in the office