Jump to content

I know I can't return an array from a function though I can't use pointers.

I need to create a function that create an array and another function that create a second array with the values of the first but inverted.

 

What's the best way to do it? 

 

 

 

 

I forgot to write that the language is C

Link to comment
https://linustechtips.com/topic/149917-return-an-array-from-a-function/
Share on other sites

Link to post
Share on other sites

I know I can't return an array from a function though I can't use pointers.

I need to create a function that create an array and another function that create a second array with the values of the first but inverted.

 

What's the best way to do it? 

Language?

Link to post
Share on other sites

I am not sure I really understand what you are asking, but I will show a few ways of dealing with arrays in functions

//"Returning an array" of 0 to 9int* getIntArray() {    int * res = (int*)malloc(sizeof(int) * 10);    int i;    for(i = 0; i < 10; ++i) {        res[i] = i;    }}//"Returning" an arrayvoid getIntArray2(int res[], int count) {    int i;    for(i = 0; i < count; ++i) {        res[i] = i;    }}//"Returning" an arrayvoid getIntArray3(int* res, int count) {    int i;    for(i = 0; i < count; ++i) {        res[i] = i;    }}int main() {    int myarray[10];    int* myresult;    myresult = getIntArray(); //Notice I need to free it    free(myresult);    getIntArray2(myarray); //Effectively getting the return    getIntArray3(myarray);//Notice they are the same, even though one uses a pointer and the other doesn't    return 0;}

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×