Arrays in Functions

Hey Guys,
I had a problem where I needed to use a function with an array as one of the parameters. All I had done was the main() function and a function prototype, but it still gave an error when I tried to compile. Can somebody please help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>

void AddBook(int BookNum, int n, int Book, int Sum);

int main()
{
    int n, c, i, Sum;
    cin >> n >> c;
    int Book[n];
    
    for(i = 0; i < n; i++)
    {
          cin >> Book[i];
    }
    Sum = 0;
    
    for(i = n-1; i >= 0; i--)
    {
          AddBook(i, n, Book[n], Sum);
    }
    
    cin.get();
    cin.ignore();
    return 0;
}
Last edited on
int Book[n];

that line won't compile on most compilers. It would work on gcc and mingw though.
Last edited on
What do you suggest ?
Few notes:

1) You're not passing an array as parameter, you're passing a single int. Specifically, you're passing Book[n], which is the n+1'th element of Book, which doesn't exist. To pass your array as a parameter, you should pass it by pointer.
1
2
void AddBook(int BookNum, int n, int *Book, int Sum);
AddBook(i, n, Book, Sum);


2) The compiler error is caused by line 9. C++ allows two types of declarations, static and dynamic. In terms of arrays, it boils down to this:
- Static allocation is "easier" (more intuitive), but requires the size (here: 'n') to be known at compile time. This means it must be a constant or a literal:
1
2
3
const int n = 6;
int Book[n];
int Book2[10];

-Dynamic allocation is "confusing", but allows you to set the size during runtime:
1
2
3
4
5
int n;
cin >> n;
int *Book = new int[n];
// Code that uses Book, until no longer required.
delete [] Book;
Last edited on
if that doesn't work (mine does) try
int *p = new int[n];
(remember to delete [] p; to avoid leaks!)

even though I'm not sure what you are trying to do, here's my suggestion:

If you want to send an array, you must send its address.
void AddBook(int BookNum, int n, int Book[], int Sum);

AddBook(i, n, Book, Sum); //instead of Book[n], Book is the address

Also you didn't using namespace std; //necessary for cin and cout
Basically, you have 2 options

1. you can use a vector
2. you can use dynamic memory allocation.


using a vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream.h>
#include <vector>

using namespace std;

void AddBook(int BookNum, int n, int Book, int Sum);

int main()
{
    vector<int> Book; // declare the vector
    int n, c, i, Sum, input;
    cin >> n >> c;
    
    for(i = 0; i < n; i++)
    {
          cin >> input;
          Book.push_back(input); // insert values into the vector.
    }
    Sum = 0;
    
    for(i = n-1; i >= 0; i--)
    {
          AddBook(i, n, Book[n], Sum);
    }
    
    cin.get();
    cin.ignore();
    return 0;
}


Something like that or

using dynamic memory allocation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>

void AddBook(int BookNum, int n, int Book, int Sum);

int main()
{
    int n, c, i, Sum;
    cin >> n >> c;
    int *Book = new int[n]; // dynamically allocate memory to a pointer
    
    for(i = 0; i < n; i++)
    {
          cin >> Book[i];
    }
    Sum = 0;
    
    for(i = n-1; i >= 0; i--)
    {
          AddBook(i, n, Book[n], Sum);
    }
    
    delete [] Book; // delete the allocated memory after using
    cin.get();
    cin.ignore();
    return 0;
}


vectors should be simpler and preferable though I believe.
Last edited on
none of the solutions seem to be working ....
@Dacster & Maniax & Gaminic - Your solutions are giving the error:


Error	1	error LNK2019: unresolved external symbol "void __cdecl AddBook(int,int,int *,int)" (?AddBook@@YAXHHPAHH@Z) referenced in function _main	c:\Users\Tejas\documents\visual studio 2010\Projects\Siruseri Book Store\Siruseri Book Store\main.obj


Can someone suggest how I put a dynamic array as a parameter in a function ??

Thanks for thelp.
Well, you haven't defined your function anywhere. It doesn't know what to do. I thought you left it out because it didn't matter, but apparently it's just missing.

A function needs two things: a declaration ('it exists') and a definition ('this is what it does'):
1
2
3
4
5
6
7
// Declare function
void myFunction(T myParam);
// Definition
void myFunction(T myParam) { cout << myParam+2; }
// Calling
T randomParam = 6; // Assume it's an int-ish type.
myFunction(randomParam);

Often, the declaration and definition are done simultaneously (in which case the declaration is simply skipped), but forward-declaration is often useful (or required), for instance when to functions need each other.

If you have an explicit declaration, you need to make sure it's the exact same (name, return type and parameter list) as the definition.
What is
void AddBook(int BookNum, int n, int Book, int Sum);
then ? Isn't this the function declaration ??


Error: undefined reference to `AddBooks(int, int, int*, int, int)'
The function AddBook. Where is its code?

undefined reference means that the compilation worked fine, but now the linker is looking for the compiled code for that function, to make the program, and it can't find it.
Last edited on
Really? Oh ok thanks then. I just wanted to check if the code was error-free before I started putting the code for my function. Thanks a lot Moschops (and all the others who helped) !!!
Topic archived. No new replies allowed.