Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Returning an array in C++

Name: Anonymous 2009-10-26 20:39

PLEASE /prog/ help me out here. I've read and tested at least 5 "answers" to my question that I've found on google, but I can't get any of them to work. You guys are better than them though, right?

I wanted to call an array by reference in a function. Then, I learned that that was impossible. I tried returning the array, but you can't do that either.

Apparently, I need to do some shit with pointers to return the array. How the fuck do I do this?

Name: Anonymous 2009-10-26 23:54

>>1

char** array(){
    char* array = new char[8];
    for(int c = 0;c < 8;++c){
        (array)[c] = '\\';
    }
    return &array;
}

int main(int argc, char*argv[]){
    char*strnig = *array();

    return 0;
}


This is a little wrong, but it's right enough. Think of an array as a pointer(if you don't know what a pointer is, think of it as a variable which points to a specific memory location), to change the value of a regular variable in a function in c you'd pass it's memory location to the function like so,


void edit_value(int *point){ //'declaring that point is pointing to an integer
    *point = 3; //'here the '*' value changes a little, to refer to the value which point is pointing to.
}


For an array, which is already a pointer, we need to pass a pointer of a pointer, **. And the rest is just the same as editing point's value.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List