array defined in header file and unknown in c file

205 views Asked by At

I writing code for microcontroller and the program look like this, this is not the real program, it is just an example of it to present the problem.

I wanted to show that if I point to a place in memory and define the pointer in the header file, I cant call to the defined array in source file.

test.h:

#define arr * (BYTE *)(0X10000);

int function(int i);

test.c:

#include "test.h"

int function(int i){
   arr[5] = 1; 
}

and the problem is: undefined identifier "arr"

How could it be that it can't recognize it?

3

There are 3 answers

4
Akira On

Let me assume the 0x10000 is an exact beginning address of a register inside your microcontroller and you wish to write there some bytes. Then I would #define my alias as follows:

#define MY_REGISTER (BYTE*)((void*)0x10000)

In this case you may use the MY_REGISTER macro as an initializer:

BYTE* myRegister = MY_REGISTER;
myRegister[5] = 1;

Note: the MCU and the compiler are not specified and I have no way to test my answer.

2
MarianD On

Directives have to be in the same line as #:

Instead of

#
define arr * (BYTE *)(0X10000);

and

#
include "test.h"

use

#define arr * (BYTE *)(0X10000);

and

#include "test.h"

(It will resolve just your problem with

undefined identifier "arr"

but other problems will emerge.)

0
Secko On

OK, this is your first problem right here, you can't change a constant. Regardless of the bad #define and arr assigment, you are trying to change the arr which you defined above as a constant.

#define arr * (BYTE *)(0X10000) //; - you don't need a semicolon after define

Next, you are trying to assign a new value to arr, which is a no no:

arr[5] = 1; //cannot change a constant

You can assign another array though, but you probably didn't want that:

 int arr[5];
     arr[5] = 1;

However you could do it like this:

#undef arr
#define arr 1