0

Please let me preface this question by stating that I am an absolute C++ novice and I'm only well-versed in R. Also, I'm working within Xcode.

  1. Let's say I create an int object called 'answers'. Initially, it is set as int. After I assign values to answers[0], answers[1], etc., is it still int or has it become something else because I added multiple values?

    int answers[4];
    answers[0] = 8;
    answers[1] = 6;
    answers[2] = 7;
    answers[3] = 5;
    
  2. Let's say I want to pass 'answers' to a user-defined function (see code below). My problem is that in the line, cout<<"The first value you entered...", I get an error saying that 'Subscripted value is not an array, pointer, or vector". Then, in the line, showAnswers(answers);, I get the error 'No matching function for call to 'showAnswers''. What am I doing wrong here? My first guess is that 'answers' is no longer an int, but I don't know what it would be.

 

void playGame(int input){
   cout << "The first value you entered was: " << input[0];
}

int main() {

    int answers[4];
    answers[0] = 8;
    answers[1] = 6;
    answers[2] = 7;
    answers[3] = 5;

    showAnswers(answers);
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
e7h3r
  • 77
  • 3
  • 5
    **(1)** `answers` was never an `int`. It is an array of 4 `int`s; it was that ever since it was declared with that type. **(2)** What's `showAnswers`, and how, if at all, is it related to `playGame`? Anyway, if you want to pass an array of 4 `int`s as an argument, then declare your function to take an array of 4 `int`s, as in `void playGame(int input[4]) {...}` – Igor Tandetnik Feb 08 '17 at 05:00
  • Your variable `answers` is an array of ints not a single int. You would need to pass a pointer or reference to the function. – Alex Zywicki Feb 08 '17 at 05:02
  • Sorry, I've been editing the names of things in my code and got it switched up. Let it be known that showAnswers and playGame are the same thing. Ok, so if 'answers' is not an int, then it should be playGame(something_other_than_int input), right? – e7h3r Feb 08 '17 at 05:05
  • https://stackoverflow.com/questions/10007986/c-pass-an-array-by-reference https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c – Alex Zywicki Feb 08 '17 at 05:10

1 Answers1

2

Two things to consider:

First, as said in a comment by Igor, answers is not an int but rather an array of 4 int values.

Second, the parameter for playGame is an int not an array of int values, so adding the subscript ([0]) is not valid.

Either you must make a separate call to playGame for each value in your array, or modify the function to accept an array reference, or a pointer to your array.

In the grand scheme of things, neither of those solutions is optimal for larger programs, as they can be prone to problems as described here in the Cpp Core Guidelines: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#a-nameri-arrayai13-do-not-pass-an-array-as-a-single-pointer

Mikel F
  • 3,567
  • 1
  • 21
  • 33