Declaring pointers to an array... or something?

I've been working on a little home project to increase my skill as a programmer.
It's a basic inventory simulator, however there's one section of code in the program that I used from a book, and I'm not entirely sure what's going on...

1
2
3
4
5
6
    string* pszArr = new string[11];
    for(int i = 0;i < 10; ++i) //initializes inventory default to be "---"
    {
            pszArr[i] = "---";
    }
    


Now pszArr is the virtual inventory, it is an array, 11 long consisting of string elements. The program is very basic, so the items (like a sword, helmet etc, typical RPG stuff :) ) are simply string objects, assigned to elements in the array when a command "give" is given.

Basically I'm wondering if there is a way to streamline this snippet of code so that it initializes all elements in the array to "---" without having this iterative loop that I think is quite unwieldy. (Sorry if i'm terrible at explaining my problem, I'm new to all of this troubleshooting stuff )
You can do that with vectors. But this will internally do that loop, anyway. Because all reserved memory locations contain garbage values when the space is reserved, so it has to iterate over all of them to correct the values 1 by 1.

 
vector<string> pszArr(11,"---");


now pszArr is a vector with 11 elements 0 to 10: [0,10], with all values initialised to "---"
Last edited on
Okay, i'll look into that
but could you explain to me what this part does?

string* pszArr = new string[11];

I wrote the iterative loop myself so I understand that part, but pointers just confuse me :)
If you know the string array is of size 11, then you can declare it string pszArr[11];.

I think that is pretty streamlined, you could do string pszArr[11] = {"---", "---" ...};

The code you have doesn't actually initialize that 11th position.

I would do:
1
2
3
4
const int SIZE = 11;
string pszArr[SIZE];

for (int i =0;i < SIZE; pszArr[i++] = "---");


Putting a lot of stuff in the update portion confuses me quickly, but for something like this I think it is okay.
Okay, i'll look into that
but could you explain to me what this part does?

string* pszArr = new string[11];
I wrote the iterative loop myself so I understand that part, but pointers just confuse me :)


A rule in C++ says: "Every array is a pointer, but not every pointer is an array".

string* pszArr defines a pointer called pszArr, and new string[11] reserves 11 slots for 11 array elements, and assigns the address of the first element to pszArr. So you have 2 ways to access this array:

1
2
3
4
5
6
7
8
//to access element number 6 (remember, first element is numbered 0):

pszArr[5] = "Hello, this is element number 6";

//or you can dereference the pointer (call its value) after moving the pointer 5 positions from the beginning:

*(pszArr + 5) = "Hello, this is element number 6";
To add, the pointer allows you to create the size of the array dynamically.

1
2
3
4
5
6
7
8
9
10
int i;
string *pszArr;

cout << "How many: ";
cin >> i;

pszArr = new string[i];

//Delete the array when you are done with it
delete [] pszArr;


But like I said before, you only want to do this if you don't know how big the array is going to be.

1
2
3
4
5
6
7
8
9
string* pszArr = new string[11]; 
 ^    ^    ^      ^     ^   ^
 |    |    |      |     |   |
 |    |    |      |     |   ~~~~ it will reserve the size of a string * 11
 |    |    |      |     ~~~~~~~~ it will be the size of a string
 |    |    |      ~~~~~~~~~~~~~~ reserves space specified by the number on the right and returns the adress of this space
 |    |    ~~~~~~~~~~~~~~~~~~~~~ The object name
 |    ~~~~~~~~~~~~~~~~~~~~~~~~~~ Pointer to a ____
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It's a string 
Last edited on
Topic archived. No new replies allowed.