### Complete Non-programmer; Can you explain what this code is doing?

Hello,

I'm in need of fixing a script for my friend who currently isn't available; unfortunately I don't have any C++ experience, he does.

I've been reading over the tutorial all day and whilst I now understand the definitions of a lot of parts of the script, I still don't really understand what's happening in a few parts.

Take this for example:

 ``123456789`` ``````const uint32 runelements= {20, 21, 22}; bool RunnerScript::FunctionFive() { for(uint8 int i = 0; i < (sizeof(runelements) / sizeof(uint32)); i++) { RegisterElements(runelements[i]); } return true; }``````

I understand what const does for example but I have no idea what the part in parenthesis is (I assume its the different IDs of the "runelements", but I have no what the script is doing with them) - ive not seen a syntax like this in the tutorial unfortunately.

Likewise I have the definition of the for loop thoroughly engrained in me but I have no idea why the initialisation uses uint8 AND int? Ive only ever see in with int? I assume uint8 is an unsigned 8-bit integer?

I also don't really understand what it's doing with sizeof? The explaination I've received was a little brief and I was wondering if anyone could explain it using this loop as and example?

Finally I see there's some sort of array being set up here using i? Is i a constant value then if so how can the for loop be increasing it? I really don't understand how that works.

Would really, really be grateful if someone could explain to me this tiny part of the code; would help me to no end. Thanks!
After looking over the Dynamic Memory section of the tutorial I have a slightly better idea of what the array is doing, but I'd still really appreciate an explanation of what's going on here from someone. Thanks.
The for loop is registering each element with the RegisterElements() function. sizeof() gets the number of bits the data type or variable occupies in memory. sizeof(runelements) would return the size of the 'runelements' array, and dividing it by the size of its data type, uint32, would give you the number of elements in the array. In this case that number would be three. It then runs the loop, incrementing 'i' each time until 'i' is no longer less than the number of elements in the array.
Hey there, cheers for the reply.

The above is sort of what I'd fathomed out already. What I dont understand is line one, and what runelements equals on line one? What do the multiple values enclosed by parenthesis do?

What would the size of runelements be therefore? And what does sizeof(uint32) give? 32?

If it makes sense, understand my reasoning; Im trying to follow the process through numerically in my head to make sense of it and in order to do that I need to know what the values of some of these things are.

How else can I make this easier... hmm.

Would this be equivalent code for example then?

 ``12345`` ``````const uint32 runelements= {20, 21, 22}; bool RunnerScript::FunctionFive() { RegisterElements(20, 21, 22[3]); }``````

Or... hold on...

Could I change it to this:

 ``123456789`` ``````const uint32 runelements= {200, 210, 220, 230}; bool RunnerScript::FunctionFive() { for(uint8 int i = 0; i < (sizeof(runelements) / sizeof(uint32)); i++) { RegisterElements(runelements[i]); } return true; }``````

And the code still function properly but instead with different identifiers for the elements and with an array of size four this time?

If I understand correctly, the purpose of the loop is to make it so that no matter how many different "runelements" values there are declared by the const; each of them gets registered by the function and stored into an array of exact size (the size being exactly the number of "runelements" values that were originally declared?).

Thank you very much so far, think I'm a little closer to being happy with my understanding.
Last edited on
 What I dont understand is line one, and what runelements equals on line one? What do the multiple values enclosed by parenthesis do?

The first line is initializing the array. The values in the parenthesis are the initial values. Equivalent code would be:
 ``1234`` ``````const uint32 runelements[3]; //Initializes the array with a size of 3 runelements[0] = 20; //Sets the first index of the array, index 0, to 20 runelements[1] = 21; //Sets the second index to 21 runelements[2] = 22; //Sets the third index to 22 ``````

 What would the size of runelements be therefore? And what does sizeof(uint32) give? 32?

The size of runelements would be the size of its data type (in this case, uint32) multiplied by the number of elements in the array (in this case, 3), which is why dividing it by the size of uint32 gives you the number of elements in the array.

 Would this be equivalent code for example then?

Unless there's something I don't know, it wouldn't. The RegisterElements() function would have to be set up to take any number of parameters and it most likely isn't.

 If I understand correctly, the purpose of the loop is to make it so that no matter how many different "runelements" values there are declared by the const; each of them gets registered by the function and stored into an array of exact size (the size being exactly the number of "runelements" values that were originally declared?).

You are exactly right.
Last edited on
This is excellent. Thank you so much.
Topic archived. No new replies allowed.