How Could i end my array of char or my strings in c++?

i have a array like this code

for (k=Start ;k<Start+3;k++)

{ ProcessingCode[k-Start]=FinalMessage[k];

}

ProcessingCode[Start+3]="\n";

And my FinalMessage is so much bigger than Processigcode and i didnt introduce processingcode in dynamic array and i introduce it with processingcode[100] and i want to end of array up to what i fill with data...

but it return me error ,how could i make it? thank you:)

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You need to make the final element "NULL" -- and process until you reach the NULL pointer. Let's take for example processing until the end of a char array:

    //Create our array

    char *aChars;

    //Now allocate our array

    aChars = new char[25];

    //Put values in slots 0, 1 and 2

    aChars[0] = 'a';

    aChars[1] = 'b';

    aChars[2] = 'c';

    //Now terminate our array with the special '\0' character

    aChars[3] = '\0';

    //Now iterate through the array until the end:

    int i=0;

    while( aChars[i] != '\0' ) {

    ProcessData( aChars[i] );

    }

    -------------

    Now, we can apply the exact same logic to an array of character arrays (i.e. an array of strings):

    //Create our array pointer

    char ** aStrings;

    //Now allocate our array of character arrays pointers

    aStrings = new char[25]; //up to 25 strings

    //Populate as many as we want to -- let's go ahead and only do 3 for testing.

    for( int i=0; i < 3; i++ ) {

    aStrings[i] = new char[20]; //Each String is 20 long

    }

    //Put values in the 3 slots we allocated

    strcpy( aStrings[0], "zero" );

    strcpy( aStrings[1], "one" );

    strcpy( aStrings[2], "two" );

    //Now terminate our array with a NULL value

    aStrings[3] = NULL;

    //Now iterate through the array until the end:

    int i=0;

    while( aStrings[i] != NULL ) {

    //This will process until we hit that null terminator.

    ProcessData( aStrings[i] );

    }

    ------

    When you are all done processing free the memory you allocated with a call to delete. i.e.:

    delete aChars;

    In the first example.

    Same logic as the examples above work if you use stack allocation and just use aChars[25] instead of allocating the memory on the heap. In that case, just skip the "new" and the "delete" commands.

  • 1 decade ago

    Your question is difficult to understand , what error are you getting what is your array size ? You need to give all info. C code is pretty difficult and a small mistake can cause major issues.

  • 1 decade ago

    do processingcode[3]='/n';

    in place of

    ProcessingCode[Start+3]="\n";

Still have questions? Get your answers by asking now.