#include<iostream>
using namespace std;

void getdata (int a[], int size);

int main ()
{

int size=10;
int a[size];
getdata (a[], size);

    return 0;
}

void getdata (int a[], int size)
{
    for (int i=0; i<size; i++)
        cin>>a[i];
}

Recommended Answers

All 4 Replies

There are a few mistakes, to which one are you referring?

You would have to add 'const' before int size=10.

Also, please provide the purpose of this program. I assume you want your function to populate the array. Sure, this may happen in the function, but you aren't returning the edited variable nor are you passing it by reference.

Also, please provide the purpose of this program.

It's pretty obvious that the purpose is to populate an array from a function.

but you aren't returning the edited variable nor are you passing it by reference.

Arrays are passed by simulated reference (ie. by pointer), so there's nothing wrong there. The two issues are thus:

  1. Semantic error: size is not a compile-time constant, and C++ doesn't allow array sizes that are not compile-time constants.

  2. Syntax error: The subscript operator is not used when passing an array to a function, just the array name.

The corrections are simple and obvious, provided the OP doesn't want a dynamically sized array:

#include <iostream>

using namespace std;

void getdata(int a[], int size);

int main()
{
    const int size = 10;

    int a[size];

    getdata(a, size);

    return 0;
}

void getdata(int a[], int size)
{
    for (int i = 0; i < size; i++)
        cin >> a[i];
}

You might say that failing to use the populated array is an error as well, since the only way to confirm the code works is by stepping through it in a debugger.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.