Okay, so I've started to learn C++ and I'm currently going through a "For Dummies" book I grabbed from the local library.
This isn't the first time - It's perhaps the third time I've tried to learn in 6 years. And I'm stubborn, unfortunately to a negative and positive degree; I do not pass over a chapter until I'm 100% sure I've understood 100% of the concepts in it. Now I hit pointers for the third time in 6 years and they're still as vague and ambiguous as ever.
First of all - What is the point of them? (Har har)
I've read reams and reams of text, scanned through my book perhaps ten times, looked through Google; nothing gives me the kind of answer I'm looking for. It's only been half answered. If we use it in the sense of integers and numbers, various examples were given in using it with numbers but I can't find the point of using them with numbers at all.
Say we have:
C++ Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//Initialise a couple of integers and pointers to go with them.
int nA = 20;
int nB = 0;
int nC = 20;
int nD = 0;
int* pnC = &nC;
int* pnD = &nD;
//If we wanted to put the value of nA into nB we could do it as simply as this
nB = nA;
cout << "The value of 'nA' is: " << nA << endl; //Output: 20
<< "The value of 'nB' is: " << nB << endl; //Output: 20
//But if we used pointers, we'd need to do this
*pnD = *pnC;
cout << "The value of 'nC' is: " << nC << endl; //Output: 20
<< "The value of 'nD' is: " << nD << endl; //Output: 20
system("PAUSE");
}
I can't see how it's any better having to initialise two ints to hold your values AND two pointers to perform an operation that isn't really neccessary (because you can just "Put the value of X into Y" instead of having to say "Take the value at the memory address pointed to by *X and put it into the memory address pointer to by *Y")
Now I do understand it's a lot more useful for arrays though. It's certainly made printing out zero terminated strings easier.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char* szString = "A String of Characters";
cout << "The array is '" << szString << "'" << endl;
cout << "Display string using a pointer: ";
char* pszString = szString;
while(*pszString)
{
cout << *pszString;
pszString++;
}
// Final Output: "A String of Characters"
cout << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
Now, I'm trying to create a very, very basic game where you simply have to move your @ character over a $ artifact to win. Just because it's one of my first "real" projects it's naturally very simple. The map will be held in the executable because I havn't done file i/o yet and I intend to hold it in a character array to make printing it a little easier.
I've created a function that takes a length and width value as inputs and outputs a room on screen in a fasion similar to this:
> Length: 6
> Width: 3
>
> ###
> #.#
> #.#
> #.#
> #.#
> ###
But I want the map to be stored in a string-zero array instead so I can store the entire state of the map into the array including where the little @ character is and generating a random location for the $ rare artifact.
Now I want to use pointer to do the writing, but how would I accomplish this?
The following code compiles without error but the program crashes on being executed.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char* szString[101]; // 101 char array.
char* pszString = szString[0]; // Pointer for the array.
// The following loop is intended to fill the entire array with # characters.
int n = 0;
while(n < 100)
{
*(pszString + n) = '#';
n++;
}
// Then zero-terminate the array.
while(n == 100);
{
*(pszString + n) = '\0';
n++;
}
// Then print all one hundred "#" characters to the screen with no formatting.
while(*pszString)
{
cout << *pszString;
pszString++;
}
cout << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
It's intended to fill an array with 100 '#' characters then fill the final one with '\0' before finally printing all one hundred # characters to the screen but it crashes immediately on running.
I have an incling as to why it's doing it though.
If &pszString = 0x0000001, pszString = entire array, *pszString = Individual character located at &pszString and pszString++ increments &pszString by 0x1 (the memory size of a char) then surely *pszString = '#' would write X to location of the memory address held by &pszString yet this seems to break my program.
If I'm correct, how do you use pointers to write individual values into character arrays?