-6

I am getting 3 errors:

  1. line 12:-invalid conversion from int* to int
  2. line 17:-x was not declared in this scope
  3. line 16:- expected primary expression before',' token

Please help me to fix this. Here is the code:

#include<iostream>
int power(int x[5])
{
    x[0]=12;
    x[1]=23;
    x[2]=234;
    x[3]=344;
    x[4]=232;

    return x;
}

int main()
{
    int action[5]={1,2,3,,4,5};
    std::cout<<x[0]<<std::endl;
    x();
    std::cout<<x[0]<<std::endl;
    return 0;
}

your help would be appreciated!

underscore_d
  • 6,309
  • 3
  • 38
  • 64
learner
  • 1
  • 1
  • 2
    given your function's `ankit()` declaration, it returns an `int`, not a pointer to `int` or `int[]` (which btw is not possible). You try to `return x`, where `x` is a `int[]`. Why would you want to return that? Also - you did not declare any `x()` function, so why would you expect calling it to compile? – Fureeish Dec 20 '17 at 13:01
  • 2
    `int x[5]` is not the same as `int`. how do you expect this to work? (btw anyhow it doesnt work, you cannot return a c array, use a `std::vector` or `std::array` instead) – 463035818_is_not_an_ai Dec 20 '17 at 13:01
  • 2
    you declared x as an argument to the function power. you declared power as a variable in the main function, and you are calling a non existant x function in that main function. Read up about functions and scope first, – StarShine Dec 20 '17 at 13:02
  • 1
    Possible duplicate of [Return array in a function](https://stackoverflow.com/questions/3473438/return-array-in-a-function) –  Dec 20 '17 at 13:02
  • Why do you think `main()` can use `x`? And why do you try to call it as a function? Is it a function or an array? Pick one. Overall, this is very bad code, that demonstrates a haphazard approach of writing code instead of thinking about it. Don't try to learn a language by trial and error, throwing random code at the wall then asking the internet when, of course, you get compiler errors. Start with a good book or tutorial, then you won't make such errors in the first place. – underscore_d Dec 20 '17 at 13:03
  • here's the actual code:-void passArray(int prime[5]) // prime is the actual array { prime[0] = 11; // so changing it here will change the original argument! prime[1] = 7; prime[2] = 5; prime[3] = 3; prime[4] = 2; } i was just trying to use int instead of void function. – learner Dec 20 '17 at 13:06
  • 2
    Don't post essential info in comments. [Edit] your post to include it. – underscore_d Dec 20 '17 at 13:09
  • 1
    Fixed-size arrays in C++ are normally defined with `std::array`. Legacy C style arrays should not be used unless there is a specific need to avoid `std::array`. – n. m. could be an AI Dec 20 '17 at 13:10
  • Ok, Thanks you all for answering – learner Dec 20 '17 at 13:13

2 Answers2

2

First of all, the errors are pretty self explanatory.

invalid conversion from int* to int

Your function is declared in such a way it is expected to return an int. Not an array of ints, just an int. Furthermore, you cannot return an array from a function. You can, however, return a pointer to the first element, which in your case is unnecessary.

Guessing that you want your function to simply set values in an array, you can achieve that by declaring the function as void returning:

void power(int x[5])
{
    x[0]=12;
    x[1]=23;
    x[2]=234;
    x[3]=344;
    x[4]=232;
}

x was not declared in this scope

Well, given your main:

int main()
{
    int action[5]={1,2,3,,4,5};
    std::cout<<x[0]<<std::endl;
    //         ^
    ...
}

Here you attempt to use a variable x, which was never declared inside main or as global variable, thus the compiler has no idea what you are referring to. Simply aliasing an argument as x in some unrelated function won't make it visible to all the code. Your can't use it like this.

expected primary expression before',' token

Take a close look at your main function and at action declaration. The part:

int action[5]={1,2,3,,4,5};
//                  ^^

is illegal. Notice the ,,. You either should put an integer inbetween them, or delete one of them.

What you probably wanted to achieve, was to first declare the array, print out the first element, apply the power() function and print the first element again, hoping it to change. Given the declaration of power() that I have written, you could achieve it by doing it like so:

#include <iostream>

void power(int x[5])
{
    x[0]=12;
    x[1]=23;
    x[2]=234;
    x[3]=344;
    x[4]=232;
}

int main()
{
    int x[5] = {1,2,3,4,5};

    std::cout << x[0] << ' ';
    power(x);
    std::cout << x[0] << std::endl;
}

That outputs: 1 12.

Fureeish
  • 12,533
  • 4
  • 32
  • 62
0

The first error is a simple mis-declaration: The function receives a pointer and returns a pointer, so the return type is int *, not simply int. By the way, I like it that the function returns the pointer because it allows for function nesting like in the code below (the result of a function call can be the direct argument for another).

Note that the parameter x you declare is a pointer, despite appearances. One can declare a pointer parameter in two ways:

f ( int *p );

or

f2( int p[] );

The two are completely identical; it is impossible to pass arrays to functions. They always are "adjusted" to pointers to the first element.

In fact, one could even write

f2( int p[100] );

which I demonstrate in the code below. The number is irrelevant and ignored. In particular, the compiler does not think there is an array, and it does no type or index checking of the actual arguments.

Now the original function you present is unsafe; it's entirely up to the caller to provide an array with 5 elements.

C++ has the possibility to pass references, and even true references to arrays. Arrays of different sizes have different types, even if the elements are of the same type. This makes normal functions taking references to arrays pretty useless (unless you have an application like a screen buffer where all arrays are of the same size, known at coding time).

But C++ has also templates, which are a facility to construct functions at compile time from a provided pattern, depending on type and int parameters provided by the caller at compile time (but not necessarily at template coding time!). For functions these template parameters can be inferred by the compiler from the function arguments which makes template functions handy to use: The compiler creates the proper one almost magically.

A function iterating over all elements of a fixed-size array is a prime candidate for a template. Check out the last function in the code below.

#include<iostream>

using namespace std; // for brevity in the example

/** An unsafe function based on passing pointers
    without element counts.
*/
int *power_5_elems(int x[100]) {
  // This is somewhat scary.
  // There is no way to check whether the array
  // starting at x has indeed 5 elements. It's
  // like writing assembler: All is up to the caller.

  // Another thing: It would be nice to use a loop.
  // We know there are 5 elements, right?
  x[0] = x[0] * x[0];
  x[1] = x[1] * x[1];
  x[2] = x[2] * x[2];
  x[3] = x[3] * x[3];
  x[4] = x[4] * x[4];

  return x;
}

// A reference to an int array with 5 elements.
// Remember, a typedef is written like a variable declaration; the
// type name takes the syntactic place of the variable.
// To decipher it, solve the expression in the parentheses first:
// "I must first de-reference the variable".
// The next operator is the square brackets index operator:
// "I must index the result of the dereferencing" (i.e. it is an array).
// There are 5 indices (0..4).
// The last information is: "The resulting type is int."
// Summed up: This is a reference to an array with 5 elements of type int.
typedef int(&intArr5)[5];

/** This is a nicer way to declare the function below. */
intArr5 power_ref5Arr(intArr5 arrRef);

/** A safe function with very limited usability.
    It works only for int arrays with 5 elements.
    This is the raw declaration of the same function.
*/
int (&power_ref5Arr( int (&x)[5] ))[5] {
  // This is not scary at all.
  // Only arrays with 5 ints can be supplied.
  for (int i = 0; i < 5; i++)
  {
    x[i] = x[i] * x[i];
  }

  return x;
}

/** A versatile function which squares arrays
    of arbitrary length of any type that isn't on
    a tree on the count of three. It's very similar to the
    function above with 5 int elements.
*/
template<int N, typename T>
T(&refPower( T (&arr)[N] ))[N]
{
  // Note the use of the template parameter N
  // in place of the "5" above.
  for (int i = 0; i < N; i++)
  {
    arr[i] = arr[i] * arr[i];
  }
  return arr;
}

int main() {
  int x[5] = { 2,3,4,5,6 };

  int *p = &x[0]; // make clear: this is a pointer

  power_5_elems(power_5_elems(power_5_elems(x)));
  cout << x[0] << endl;

  // The function actually expects a pointer!
  // The pointer points to x.
  power_5_elems(p);
  cout << x[0] << endl;

  int y[2] = { 2,3 };

  refPower(refPower(refPower(y)));
  cout << y[0] << endl;

  double z[2] = { 0.2 , 0.3 };
  refPower(refPower(refPower(z)));

  cout << z[0] << endl;

  return 0;
}
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62