Initializing an array with string values

Hey all - I'm new here, sorry for the dumb question. For some reason I can't see what I am doing wrong here.

I have a const set as
const int size = 10;
to be the maximum index of the array

I have the protocol above main
void intialize (char text[size], int& current);

I call initialize from main
have text declared as: string text[size];
current declared as: int current = 0;
initialize(text, current);

Then my function is
void intialize (char text[size], int& current)
{
int i, current = 0;

for (i = 0; i < size; i++)
{
strcpy(text[i], "*");
}

}


Basically what I want this to do is take an array with an index of 10 and set the character "*" in each element of the array. I must be calling to it incorrectly. Any thoughts would be much appreciated.
first thoughts are: why do you need to pass a reference to an integer to your function? Especially since all your function does is set your reference to 0, then doesn't use it anywhere. If you don't have to, don't code it.
The less code you have to write to achieve something (effectively), the better. Also, naming conventions are a good habit to get into, I would recommend renaming "i" to something like "iIdx" because then anybody else knows that it is an index that is an integer. It also helps everyone greatly if you write one-line-documentation (comments) that tell the intention. Anywho, I think your problem stems from your call of strcpy(), I think strcpy is only:
char* strcpy(char* aDestination, const char* aSource);
and you're passing a const char* to aSource, but a char to aDestination.
This will prevent it from compiling. What you can do is this instead:
1
2
3
4
5
6
7
8
9
10
11
for(size_t iIdx = 0; iIdx < iSize; ++iIdx)
{
    // Make sure we aren't going over the limit!
    if (iSize <= iIdx)
    {
        break;
    }

    // Set the ASCII value to the Asterisk (*).
    text[iIdx] = '*';
}


Here, I have shown you what one-line-documentation looks like along with how to solve your problem. It is also recommended that you place a null terminator ('\0') at the end of the array to ensure that a reading function doesn't read beyond the buffer (SEG_FAULT). The above code can be changed to this to apply a null terminator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
size_t iIdx = 0;
for(iIdx = 0; iIdx < iSize; ++iIdx)
{
    // Make sure we aren't going over the limit!
    if (iSize <= iIdx)
    {
        break;
    }

    // Set the ASCII value to the Asterisk (*).
    text[iIdx] = '*';
}

// Place a NULL terminator to prevent SEG_FAULTs.
text[iIdx] = '\0';


I hope this helps you. :)
Thanks for the advice on strcpy - I ended up going with what you suggested before checking back. The real problem with my initialize was that I spelled it wrong in the function :P The full program is now working, the reason I am passing the current back and forth is because I have a few functions that change only one element of the array. I'll definitely follow your variable naming suggestions, I had never given it much thought but it seems like a good habbit to form.
I'm glad you got it working and was more than happy to help! :)
Topic archived. No new replies allowed.