2

I want to pass array to function in C/C++ without declaring and assigning it previously. As in C# we can:

fun(new int[] {1, 1}); //this is a function call for void fun(int[]) type function

I am not very sure about the correctness of the above code. But I had done something like this in C#.

How can we pass array to function using dynamic declaration (on the spot declaration)??

I'd tried it using following way. But no luck

fun(int {1,1});
fun(int[] {1,1});
fun((int[]) {1,1});

can't we do so..??

4
  • Why the turbo-c and turbo-c++ tags? Do you use those compilers? Commented Jul 21, 2012 at 10:21
  • yes @RedX. I am using Turbo C++ Commented Jul 21, 2012 at 10:22
  • 2
    Do you want a C or a C++ solution? This makes a significant difference here. Commented Jul 21, 2012 at 10:32
  • It should work in Turbo C++ compiler @BjörnPollex Commented Jul 21, 2012 at 10:34

4 Answers 4

4

In C99 and later, this:

foo((int[]){2,4,6,8});

is approximately the same as this:

int x[] = {2,4,6,8};
foo(x);

Prior to C99, this is not possible. C++ does not support this syntax either.

Sign up to request clarification or add additional context in comments.

5 Comments

@ShashwatTripathi: C99 is the second most recent version of the C language, which Turbo-C probably doesn't support.
and what about Borland C/C++ compiler..??
@ShashwatTripathi: That's a very ancient compiler as well. I suggest moving to a modern development environment.
Actually I was solving some previous year Question Paper for my practical exam of C++. They provide Turbo and Borland compilers only. That's why i was finding a solution for that. But now I'd know, I have to declare it before passing.
You can download several C++ IDEs for free. I recommend CodeLite.
3

In C++11, you can use an initializer list:

#include <initializer_list>
#include <iostream>

void fun(std::initializer_list<int> numbers)
{
    for (int x : numbers)
        std::cout << x << ' ';
    std::cout << '\n';
}

int main()
{
    fun( {2, 3, 5, 7, 11, 13, 17, 19} );
}

Comments

2

This is possible in ANSI C99 and C11, but not in C89.

Comments

1

May I suggest using a variable argument list?

The declaration syntax would be as follows:

void Function(int Count, ...) {
     va_list va;
     va_start(va, Count);
     for(int i = 0; i < Count; ++i) {
         int nextVar = va_arg(va, int);
         //Do some stuff with the array element
     }
     va_end(va);
}

This function is called with:

Function(4, 2, 3, 1, 5);

2, 3, 1 and 5 are the 'array elements'.

Alternatively, if you 'need' an array to go through within that function, it's easy to move the variable list to a proper (dynamic) array, by using new and delete.

Within the va-function:

 int *Array = new int[Count];
 for(int i = 0; i < Count; ++i) {
      Array[i] = va_arg(va, int);
 }
 //Do Stuff with Array
 delete[] Array;

3 Comments

It should work. It's standard C, as far as I know and is the only way that comes to mind, to pass an 'array' without initializing one first. Why don't you just give it a go and see if it works as you expect it to?
@alinsoar Actually it's not a different problem...the problem is, that he wants to pass a variable number of elements of the same type, without having to initialize an array of that type prior to the function. As such, the variable argument list is a valid solution for the problem.
Yes. The problem is * pass a parameter without having declared it *. The problem is clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.