Passing an array as a function argument in C++

Fly

If I have this function:

void initPoints(sf::Vector2f points[]);

Why can I do this:

sf::Vector2f[] vecs = {sf::Vector2f(0,0), etc};
initPoints(vecs);

But can't do this?

initPoints({sf::Vector2f(0,0), etc});

VS gives me an error on the second one, but not on the first one.

vincentp

By using std::vector or std::array you can resolve your problem easier :) Furthermore, std::vector is RAII-conform, so you don't have to manage the memory by yourself. Generally, STL classes are better than C-type arrays.

#include <vector>
#include <initializer_list>
// ...
std::vector<sf::Vector2f> vecs = { Vector2f(0,0), etc };

Then:

initPoints(const std::vector<sf::Vector2f>& vec) {
    // ...
}
initPoints(vecs);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing array literal as function argument

From Dev

Passing array range as argument to a function?

From Dev

Passing structure as argument in C function

From Dev

passing function as argument in c++

From Dev

passing multidimensional array as argument in C

From Dev

Passing an array as a function argument from within a function which takes it as an argument in C

From Dev

Passing an array into a function in C

From Dev

passing array as javascript function argument from php

From Dev

Passing array to function with generic data type argument

From Dev

passing multi-dimentional array as function argument

From Dev

Go - Passing an array to a function receiving argument list

From Dev

Passing an argument in the function to turn struct values into an array

From Dev

passing array as javascript function argument from php

From Dev

passing multi-dimentional array as function argument

From Dev

Increasing the size of an array by passing it as an argument to function

From Dev

passing an array as function argument to setTimeout acts not like passing a variable

From Dev

Passing any generic function as a C++ argument

From Dev

Passing Cython class object as argument to C function

From Dev

Passing an object as an argument to a function in C++

From Dev

c struct passing itself as an argument into a pointer function

From Dev

Passing any generic function as a C++ argument

From Dev

Passing argument from main to function in C

From Dev

Error when passing function as an argument in C++

From Dev

c++ passing function itself as argument

From Dev

C++: passing unknown union to function as argument

From Dev

passing function pointer in c with a pointer argument

From Dev

Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

Related Related

  1. 1

    Passing a pointer to a char array as an argument to a function - C

  2. 2

    Passing a pointer to a char array as an argument to a function - C

  3. 3

    Passing array literal as function argument

  4. 4

    Passing array range as argument to a function?

  5. 5

    Passing structure as argument in C function

  6. 6

    passing function as argument in c++

  7. 7

    passing multidimensional array as argument in C

  8. 8

    Passing an array as a function argument from within a function which takes it as an argument in C

  9. 9

    Passing an array into a function in C

  10. 10

    passing array as javascript function argument from php

  11. 11

    Passing array to function with generic data type argument

  12. 12

    passing multi-dimentional array as function argument

  13. 13

    Go - Passing an array to a function receiving argument list

  14. 14

    Passing an argument in the function to turn struct values into an array

  15. 15

    passing array as javascript function argument from php

  16. 16

    passing multi-dimentional array as function argument

  17. 17

    Increasing the size of an array by passing it as an argument to function

  18. 18

    passing an array as function argument to setTimeout acts not like passing a variable

  19. 19

    Passing any generic function as a C++ argument

  20. 20

    Passing Cython class object as argument to C function

  21. 21

    Passing an object as an argument to a function in C++

  22. 22

    c struct passing itself as an argument into a pointer function

  23. 23

    Passing any generic function as a C++ argument

  24. 24

    Passing argument from main to function in C

  25. 25

    Error when passing function as an argument in C++

  26. 26

    c++ passing function itself as argument

  27. 27

    C++: passing unknown union to function as argument

  28. 28

    passing function pointer in c with a pointer argument

  29. 29

    Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

HotTag

Archive