Returning an array from a function with arguments in c++

ANIRUDDHA GUIN

I am trying to return an array from a local function to the main function but every time it shows a compilation error of

invalid conversion from ‘int’ to ‘int*’ [-f permissive]

#include <bits/stdc++.h>

using namespace std;

int* GCDstrings(int a,int b,int *F)
{
    int i;
    if(a%b==0)
    {
        F[0]=1;

        for(i=1;i<a;i++)
            F[i]=0;
    }

    return F;
}

int main()
{
    int T;
    cin>>T;

    while(T--)
    {
        int x,y;
        cin>>x>>y;

        int f[x];
        int* p;
        p=GCDstrings(x,y,f[x]);

        for(int i=0;i<=x;i++)
            cout<<*(p+i);
    }

    return 0;
}

What is the mistake I am doing here?

anand_v.singh

You error lies in this part of your code

int f[x];
int* p; 
p=GCDstrings(x,y,f[x]);

You are trying to create a dynamic array and then pass it to the function.

When you pass an array you should only pass the location to its first value.

You can either do

p=GCDstrings(x,y,f);

during your function call.

Or you can go with,

p=GCDstrings(x,y,&f[0]);

For more information, check out this link https://www.programiz.com/cpp-programming/passing-arrays-function.

Also you might want to look into dynamic memory allocation for future, however it looks like you are beginning so allocating an array of length x like this is okay, but do check that out later. https://www.geeksforgeeks.org/new-and-delete-operators-in-cpp-for-dynamic-memory/

If any more problems, comment and we will see,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning an Array from a Function in C

From Dev

Returning an array from function in C

From Dev

Returning positional arguments from a function

From Dev

returning array from function in c++

From Dev

Returning integer array values from a function in c

From Dev

Returning an item from an array in a C function

From Dev

Returning array to main from a function in C

From

Returning values through the arguments of go function, which is called from C

From Dev

Returning a C array from a C function to Python using ctypes

From Dev

Cannot resolve the errors with returning an array using pointer from a function in C?

From Dev

Array elements change after returning from a function in C

From Dev

Returning a 2D array from c function

From Dev

Returning an array address from a function to a pointer (C++)

From Dev

Returning a mutable array from a function

From Dev

Returning an array from a function in VBA

From Dev

Returning an array of pointers from a function

From Dev

Returning an array from function in nodejs

From Dev

Returning vector array from a function

From Dev

Javascript returning array from the function

From Dev

C - Returning array as function argument

From Dev

Returning char array in C function

From Dev

Returning array of chars in C function

From Dev

Returning Array and assign to array from calling function

From Dev

returning this from function in c++

From Dev

Returning string from C function

From Dev

Returning malloc from a function in C

From Dev

Boolean function arguments and returning

From Dev

function returning pointer vs function returning array in C

From

Golang: Use one value in conditional from function returning multiple arguments

Related Related

HotTag

Archive