1

I write a function using arrayfire like that:

int ABC()
{

static const int Q = 5;
double A[]  = { 0.0,  1.0,  0.0, 1.0, 1.0};
double B[]  = { 0.0, -1.0, -1.0, 1.0, 0.0 };
array C (Q, 1, A);
array D (Q, 1, B);

return 0;
}

When I try to call this function as: ABC() in the main program and try to extract the variables C and D and want to print them using af_print(C), gives the error:

error C2065: 'C' : undeclared identifier
error C2065: 'D' : undeclared identifier
IntelliSense: identifier "C" is undefined
IntelliSense: identifier "D" is undefined

The main function is:

#include <cstdio>
#include <math.h>
#include <cstdlib>
#include "test.h" 
// test.h contains the function ABC() and 
// arrayfire.h and 
// using namespace af; 

int main(int argc, char *argv[])

{
ABC(); // function
// here I am calling the variables defined in ABC()
af_print(C);
af_print(D);
#ifdef WIN32 // pause in Windows
if (!(argc == 2 && argv[1][0] == '-')) {
    printf("hit [enter]...");
    fflush(stdout);
    getchar();
}
#endif

return 0;
}

Any solution please.

Regards

TheCoder
  • 55
  • 1
  • 8
  • 1
    *Where* do you try to print the arrays? Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) ad show us? – Some programmer dude Aug 25 '15 at 07:27
  • Added in the question. – TheCoder Aug 25 '15 at 07:42
  • 2
    You *do* know about the concept of *scoping*? That local variables in one function are local to that function and that function only, and can't be used from other functions? It seems that you need to refresh your knowledge of basic C. – Some programmer dude Aug 25 '15 at 07:44
  • yes, you are right, I am new to C. I need to study about _scoping_. Can you guide me for the particular problem? – TheCoder Aug 25 '15 at 07:48

1 Answers1

1

In C there are basically three scopes variables can be defined in:

  • Global scope, when variables are defined outside of any functions.
  • Local scope, when variables are declared in functions, this scope include function arguments.
  • Block scope, this is for variables defined in blocks nested in functions, for example in the body of an if statement.

Variables in one scope are only available in the current scope, and nested scopes. They simply don't exist in parallel scopes or scopes of a higher level.

More "graphically" it could be seen somethig like this:

+---------------------+
| Global scope        |
|                     |
| +-----------------+ |
| | Function scope  | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| +-----------------+ |
|                     |
| +-----------------+ |
| | Function scope  | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| +-----------------+ |
+---------------------+

In the figure above, there are two function scopes. Variables declared in one of the function scopes can't be used by any other function scope, they are local to that function.

Same with block scope, variables declared in a block can only be used in that block and children of that block.


Now for how this relates to your problem: The variables C and D are defined in the function ABC, that means their scope is in the ABC function only, other functions (like your main function) can't see or access the variables defined in the ABC, the variables are local in the scope of the ABC function.

There are many ways to solve the problem of accessing these variables from other functions, and the most common beginner way is to put the definition of those variables in the global scope. Then in the function you assign to the variables, something like

array C;
array D;

void ABC()
{
    ...
    C = array(Q, 1, A);
    D = array(Q, 1, B);
}

Other solutions involve passing arguments by reference and assigning to them. Or by putting the data in a structure (class or struct) and returning an instance of this structure.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you very much. One thing to clear that I am not using **array** header of C++ rather I am using **arrayfire.h**. – TheCoder Aug 25 '15 at 08:24
  • It works. Thank you. I defined variables as global. I think **class or struct** is more elegant. – TheCoder Aug 25 '15 at 08:45