-1

I want to create an array while passing it to a function, like we can do in Java or Python. For example:

class HelloWorld {
    public static void main(String[] args) {
        example(new int[]{1,2,3});   // Like this
    }
    static void example(int[] a){
        System.out.print(a[0]);
    }
}

or in python

def fun(x):
    print(x[0])

fun((1, 2, 3)) #Like this

When I try to do something like this in C++ I get an error

void example(int a[]){
    cout<<a[0]<<endl;
}

int main() {
    // Write C++ code here
    cout << "Hello world!"<<endl;
    example(new int(3){0, 1, 2});
    return 0;
}

This gives the error

error: expected ')' before '{' token

or

void example(int a[]){
    cout<<a[0]<<endl;
}

int main() {
    // Write C++ code here
    cout << "Hello world!"<<endl;
    example({0, 1, 2});
    return 0;
}

Here the compiler takes the array {0, 1, 2} as an initializer list.

error: cannot convert '' to 'int*'

I'd like if there is some way of achieving a function call similar to the 2nd attempt.

function({1, 2, 3, 4});   //An array of any size

I tried searching for it but wasn't able to find a solution that fits the bill. Any and all help is really appreciated and I thank everyone in advance.

Hattori
  • 1
  • 1
  • 3
    PSA: Use `std::vector` and stop slinging C-style arrays around as pointers. Stay away from `new[]` if you can. – tadman Feb 08 '23 at 17:32
  • `example(new int(3){0, 1, 2});` is, syntax aside, allocating, using, and then **failing to release memory**. It is important you never do this. If you allocate you **must** assume responsibility to release the memory. – tadman Feb 08 '23 at 17:33
  • 3
    `int(3)` is not an array. It is a single int with the value 3. – Drew Dormann Feb 08 '23 at 17:33
  • `example(std::array{0, 1, 2}.data());`? – Jarod42 Feb 08 '23 at 17:35
  • 1
    But `void example(std::span*const*/int>)` would probably be better. – Jarod42 Feb 08 '23 at 17:37
  • 2
    It seems you are learning C++ the wrong way. `new` in Java and `new` in C++ do very different things. Don't just code at random and hope for the best and don't use other languages as a templates when learning a new language. Get [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Ted Lyngmo Feb 08 '23 at 17:45

1 Answers1

2

Here's a reworked version that uses more modern C++ conventions:

#include <iostream>
#include <vector>

void example(const std::vector<int>& a) {
    std::cout << a[0] << std::endl;
}

int main() {
    // Using a pre-defined variable
    std::vector<int> list { 0, 1, 2 };

    example(list);

    // Using an inline variable
    example(std::vector<int> { 3, 4 });

    // Letting the compiler figure it out
    example({ 5, 6 });

    return 0;
}

Keep in mind new[] is not automatically released, and if you pass it in as an argument to a function that does not assume responsibility for releasing it, you have a memory leak.

This does not happen with std::vector or other standard containers, these will always release memory when they fall out of scope.

Python will fill in the blanks in many ways that C++ refuses to, but even Python can be made stricter about arguments where it will act a lot more like C++.

tadman
  • 208,517
  • 23
  • 234
  • 262