i want to initialize an array(set all elements to 0) . The array is to be used by different functions which r defined outside the class. obviously i cant initialize it in the class. and if i do it in a constructor or main(), it isnt recognised by all the functions outside the class using it.

what do i do ? where do i initialize the array. Please help. this is just the thing holding me back. :sad:

Dani AI

Generated

Building on points from , and , here are practical, compile-ready patterns to provide one zero-initialized array that functions defined outside a class will operate on. Each pattern shows how to guarantee a single instance (no accidental copies) and how to initialize it to zeros in a safe, idiomatic way.

Pass a container or the real array by reference (recommended). This keeps ownership local and gives functions direct access without globals.

#include <array>
#include <algorithm>

void process(std::array<int,5>& a) {
    // operate on the same array instance
}

int main() {
    std::array<int,5> arr = {}; // zero-initialized
    process(arr);
}

For C-style arrays and preserving size, use a reference template:

template<std::size_t N>
void process(int (&arr)[N]) { /* N available, arr not decayed */ }

Use a single shared definition when a truly global or shared storage is required (multi-file projects). Put the extern declaration in a header and one definition in a source file:

/* shared.h */
extern int sharedArr[100];

/* shared.cpp */
#include "shared.h"
int sharedArr[100] = {}; // one definition + zero-init

Alternatively, a class-level static member (defined once in a cpp) or C++17 inline static in the header accomplish the same thing without passing pointers around:

struct Holder { static int arr[10]; };
// in a cpp file:
int Holder::arr[10] = {};

Quick tips and cautions: prefer std::array/std::vector for safety and clarity (std::array). Use std::fill to re-zero; avoid memset for non-POD types. For modern C++ (C++11+), in-class member initializers and inline static (C++17) simplify header-only designs. Overall, prefer passing containers/references for good encapsulation; use globals/static only when design truly requires shared process-wide state.

Recommended Answers

All 4 Replies

i want to initialize an array(set all elements to 0) . The array is to be used by different functions which r defined outside the class. obviously i cant initialize it in the class. and if i do it in a constructor or main(), it isnt recognised by all the functions outside the class using it.

Sure it is. You probably did it wrong if it's not working. Define what you mean by "it isnt recognised"

Sure it is. You probably did it wrong if it's not working. Define what you mean by "it isnt recognised"

if i initialize the array in main() or constructor, the other functions which use that array give error : 'array not defined'.

it shouldn't happen. try

#include <iostream>
int main () {
    int test [5]={0};
    cout<<test[0];
}

if i initialize the array in main() or constructor, the other functions which use that array give error : 'array not defined'.

Thats because they can't see the array or the array is out of scope for them. Post the code which displays this peculiar behaviour and we will see what can be done...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.