ruskin23
ruskin23

Reputation: 155

Arrays in headers

The following problem is a small part of my project. I have simplified the problem a lot.

I have declared both the array size and the array globally.

int N;
double array[N];

Now using a function I am filling the array.

void arrayfill()
{
    for(int i=0;i<N;i++)
    {
        array[i] = i;
    }
}

Next, I have a function which sums over the elements of all the array depending upon a variable var.

double sum(int var)
{
    double s;
    for(int i=0;i<var;i++)
    {
        s+=array[i];
    }
    return s;
}

Now, what I want is to create a header file which will have the fill function, another header which will have the sum function and finally from the main function I want to call sum(). The reason I am doing this is because I want to fill the array once and keep using sum() at many different places with different values of var.

Can anybody help?

Upvotes: 1

Views: 503

Answers (3)

cdonat
cdonat

Reputation: 2822

You'll have to declare the array in the header and have a .cpp file, where you define it:

whatever.h:

extern const size_t N = 42;
extern int array[N];

whatever.cpp:

include "whatever.h"

int array[N];

When the compiler just reads the include file, it will just note, that there is an array somewhere of size N. Only when you compile whatever.cpp the compiler will actually generate array in the data section. So then there is exactly one array in your linked application, though you reference it from many different places.

Upvotes: 0

legalize
legalize

Reputation: 2253

In your header you wrote that you declared the array:

// header.h
int N;
double array[N];

Neither of these is a declaration. Both of them are definitions. To declare variables in a header you use the extern keyword:

// header.h
extern int N;
extern double array[N];

Furthermore, N can't be used as the size of an array unless it is a constant, so we need to declare N both extern and const:

// header.h
extern int const N;
extern double array[N];

In exactly one source file, the storage for N and array must be defined:

// source.cpp
int const N = 10;
double array[N];

In your header, you add declarations for the functions you intend to use:

// header.h
// ...as before
void arrayfill();
double sum(int var);

These are function declarations, not definitions. In exactly one source file you would put their definitions as you've written them.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

This is one way to do it.

array.h:

extern const int N;
extern double array[];

arrayfill.h:

void arrayfill();

arraysum.h:

double sum(int var);

main.cpp:

#include "arrayfill.h"
#include "arraysum.h"

int main()
{
   arrayfill();
   double s = sum(10);
   return 0;
}

array.cpp:

#include "array.h"

const int N = 200;
double array[N];

arrayfill.cpp:

#include "array.h"
#include "arrayfill.h"

void arrayfill()
{
    for(int i=0;i<N;i++)
    {
        array[i] = i;
    }
}

arraysum.cpp:

#include "array.h"
#include "arraysum.h"

double sum(int var)
{
   double s = 0.0;
   for(int i=0;i<var;i++)
   {
      s+=array[i];
   }
   return s;
}

Build the program using g++:

g++ -c array.cpp
g++ -c arrayfill.cpp
g++ -c arraysum.cpp
g++ -c main.cpp
g++ -o arraytest array.o arrayfill.o arraysum.o main.o

Upvotes: 2

Related Questions