PDA

View Full Version : Array Question


Uleat
11-19-2012, 12:49 AM
I'm working on reducing some redundant code segments and ran across something that I'm curious about.


In my mod, I have 5 elements and initialize my array as <type> myarray[5] = {arg1, arg2, arg3, arg4, arg5}.

I reference it using a zero-based index and everything works just fine.


However, I ran across this snippet and can't figure out why it's coded this way.

const char *message_arg[9] = {0};

<...>

i = 0;
message_arg[i++] = message1;
message_arg[i++] = message2;
message_arg[i++] = message3;
message_arg[i++] = message4;
message_arg[i++] = message5;
message_arg[i++] = message6;
message_arg[i++] = message7;
message_arg[i++] = message8;
message_arg[i++] = message9;

Does this method assign the first value at index [0] or [1]? (If [1], will adding the string value to message_arg[9] throw an exception, or am I missing something?)

Each message# is passed as a parameter into the function.


Thanks!

lerxst2112
11-19-2012, 01:05 AM
i++ is known as a post-increment. That means it saves the initial value, increments it, then returns the initial value. By comparison, ++i simply increments the value and returns it. This is why the pre-increment is preferred when using iterators, since it potentially avoids a copy.

Based on that description you can probably figure out what that code does. Of course, you could also figure out what it does based on the fact that it doesn't corrupt the stack which it would if it was starting at index 1.

Uleat
11-19-2012, 01:16 AM
That makes sense! Thanks!

I'm still trying to convert what little I know about VB into VC++, so I miss some basic concepts more often than not...