-5

I am a student who is doing an assignment for C++ and I have encountered something I am very unfamiliar with. I have an int array with no size but a list of numbers. After it's creation, there is a function call inside a function that has that array as a parameter with an index in it.

For example:

for (int x = 0; x < CAPACITY, x++)
   functionCall(array[x]);

Now I am supposed to create a function so the call can work. However when I make my function:

void functionCall(int array[]);

It does not work because it cannot turn an int to an int[].

I guess my question is, how am I supposed to get that list of numbers created originally by the array if I have to call it in my function as if it isn't an array.

Right now if I just put as an int but not an array like it wants me to do it just gives me the number 5 but not any of the numbers in the array. For example:

void functionCall(int array);

Sincere thank you for anything and I apologize if this sounds confusing.

Lary John
  • 11
  • 1
  • 5
    Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Feb 08 '18 at 21:16
  • Also, never name a function `functionCall`... unless it returns a structure representing a functional call in some parsing/meta-programming context. – Aluan Haddad Feb 08 '18 at 21:17
  • `array[x]` is an `int`, period. If I show you an apple, can you describe the tree it came from? – aschepler Feb 08 '18 at 21:17
  • 1
    Use `std::vector` to represent arrays of unknown size at compile time. `std::array` otherwise, if you know a certain value for `N`, when writing your code down. –  Feb 08 '18 at 21:18
  • Even if this was theoretically possible, what would the output be for the array if the user entered a literal such as 5? What if the user entered a variable not associated with an array? How would you differentiate between what is part of an array and what isn't? – Arnav Borborah Feb 08 '18 at 21:21
  • 1
    Your question is very confusing. You say _I have an int array with no size but a list of numbers._ (How can it have no size but it's a list?) Anyway, then you ask _how am I supposed to get that list of numbers created..._ Please read about [mcve]. – 001 Feb 08 '18 at 21:24
  • 1
    @JohnnyMopp I think my no size he means something like `int arr[]`, but otherwise yes, an MCVE is needed. – Arnav Borborah Feb 08 '18 at 21:33
  • 1
    You need to at least show us how the array is defined and initialized, and explain what you intend functionCall to do. I have a vague guess what you mean but it's not very well described in your question. Are you intending to modify the array using functionCall? – Morgan Feb 08 '18 at 21:48
  • The question is somewhat silly. Analogy: Every day my teacher gives me a page from a book. How am I supposed to get the whole book if I only get one page at a time? (and your answer must work even if the page wasn't actually from a book in the first place) – user253751 Feb 08 '18 at 22:21

2 Answers2

2
functionCall(array[x]);

This passes the xth element in the array to the function, so a single int.

array[2] = 5;
functionCall(array[2]); // This is the same as functionCall(5);

So in the function, you get the current element of the array. Not the array itself.

You cannot get the list inside the function, because you only give a single element of that list each time you call it.

Khoyo
  • 1,253
  • 11
  • 20
  • 1
    **"I guess my question is, how am I supposed to get that list of numbers created originally by the array if I have to call it in my function as if it isn't an array."** - Your answer doesn't address this. (Even a simple yes or no would do) – Arnav Borborah Feb 08 '18 at 21:22
  • You cannot get the list inside the function, because you only give a single element of that list each time you call it. – Khoyo Feb 08 '18 at 21:30
  • 1
    Don't tell me, write it in your answer instead. – Arnav Borborah Feb 08 '18 at 21:32
0

Taking a wild guess, I suspect you are looking for something like the MCVE below:

#include <iostream>

void functionCall(int v) {
    std::cout << v << " ";
}
void func(int array[], size_t CAPACITY) {
    for (size_t x = 0; x < CAPACITY; x++)
        functionCall(array[x]);
}
int main() {
    int list[] = { 1,2,3,4,3,0, 42 };
    func(list, std::distance(std::begin(list), std::end(list)));
    return 0;
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65