LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Return Char Arrays (not with pointers) (https://www.linuxquestions.org/questions/programming-9/c-return-char-arrays-not-with-pointers-411138/)

sadarax 02-03-2006 12:51 AM

C++ Return Char Arrays (not with pointers)
 
Hello, I would like to return an array of chars from a function so I can read/write to it. I cannot send the original array and edit it as a pointer, because I need to create a new array with some modifications in my program.

Code:

void main()
{
        int size = 10;
        char str [ size ];

        char newStr[ size ];
        *newStr = copyArray (str, size);
}

char* copyArray( const char str[], const unsigned int size)
{
        char newStr[ size ] = "\0";
       
        for(unsigned int i=0; i < size; i++) // copy the array
        {
                newStr[i]=str[i];
                // other modifications to the new array
        }
               
        return *newStr;
}

I know I have the pointers messed up with that code example, but that's the best I know. I want to be able to use the newStr array in main, how can I have it actually send a copy of the array in a way that won't be deleted?

jtshaw 02-03-2006 02:54 AM

Typically to do this you would pass the original and the new string by reference (similar to how strncpy or any of the other string.h functions work).

Here is a quick example I cooked up (using your example). This is hardly "good" code, just threw it together real quick...

Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void copyArray( const char str[], const unsigned int size, char *newStr)
{
    for(unsigned int i=0; i < size; i++) // copy the array
    {
        newStr[i]=str[i];
        // other modifications to the new array
        newStr[i]-=32;
    }
}

void printArray (char str[], int len)
{
    for (int i = 0; i < len; i++)
    {
        printf ("%c",str[i]);
    }
    printf ("\n");
}

int main()
{
    int size = 10;
    char str [ size ];
    strncpy(str,"abcdefghij",size);

    char newStr[ size ];
    memset(newStr,0,size);
    copyArray (str, size, newStr);
    printf ("Original = ");
    printArray(str,size);
    printf ("New = ");
    printArray(newStr,size);

    return 0;
}

Just to illustrate that they are different I subtracted 32 from each char in the original... providing they are all lowercase letters (like in the example) this effectively changes them to upper case.

ta0kira 02-03-2006 11:44 AM

Standard C++ explicitly prohibits functions from returning arrays; you'll need to use a pointer or a class which embeds an array. Is the problem that you don't want to use heap memory (i.e. new[])?
ta0kira

dogpatch 02-03-2006 04:39 PM

Quote:

Originally Posted by sadarax
Hello, I would like to return an array of chars from a function so I can read/write to it. I cannot send the original array and edit it as a pointer, because I need to create a new array with some modifications in my program.

Code:

void main()
{
        int size = 10;
        char str [ size ];

        char newStr[ size ];
        *newStr = copyArray (str, size);
}

char* copyArray( const char str[], const unsigned int size)
{
        char newStr[ size ] = "\0";
       
        for(unsigned int i=0; i < size; i++) // copy the array
        {
                newStr[i]=str[i];
                // other modifications to the new array
        }
               
        return *newStr;
}

I know I have the pointers messed up with that code example, but that's the best I know. I want to be able to use the newStr array in main, how can I have it actually send a copy of the array in a way that won't be deleted?


OK, here's your basic problem: In your example, you have defined the char array newStr within the main function, and then within your copyArray() function, you define another, local char array with the same name. This is NOT the same char array. The array defined within copyArray() is locally defined, i.e, it exists on the stack, so when copyArray() returns to main, its memory allocation is released, and its contents may be overwritten. To correct this, the array must be defined within main or outside a function, in the common area. Here's one way to do this:

Code:

void main()
{
        int size = 10;
        char str [ size ];

        char newStr[ size ];
        copyArray (newStr, str, size);
}

copyArray(char newStr[], const char str[], const unsigned int size)
{
       
        for(unsigned int i=0; i < size; i++) // copy the array
        {
                newStr[i]=str[i];
                // other modifications to the new array
        }
               
        return;
}


sadarax 02-03-2006 04:49 PM

Thank you all for your replies. Now that I see what you mention, it makes perfect sense and I am surprised I did not see it before (based on what I know about parameters and pointers).


All times are GMT -5. The time now is 04:19 AM.