-3

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?

Akira
  • 4,385
  • 3
  • 24
  • 46
Roy Ancri
  • 119
  • 2
  • 14

3 Answers3

1

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.

Akira
  • 4,385
  • 3
  • 24
  • 46
  • this is not the real program, it is just an example of the of it the present the problam. I wanted to show that if if point to a place in memory and define the pointer in the header file, I cant call to the defined array in source file. – Roy Ancri May 11 '17 at 11:26
  • 2
    you don't define any variable with `arr`. `arr` is just a macro. – zoska May 11 '17 at 11:54
  • Preprocessor macros are just named fragments of code, see my answer [here](http://stackoverflow.com/a/43862568/7893951) about that issue. Anyway if you include a header contains a macro there is nothing to prevent you to use it, except the `#undef` directive. – Akira May 11 '17 at 13:58
  • 1
    `#define` does not "define" a variable of any kind. It introduces a preprocessor macro. –  May 11 '17 at 14:32
0

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.)

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • I know it shuld be at the same line. it just happened here – Roy Ancri May 11 '17 at 11:36
  • But I didn't get your error about undefined identifier "arr" when I fix these 2 directives. (I got other errors but they contain notes about *"expansion of macro ‘arr’"*, so `arr` is no further considered as an (unknown) identifier. – MarianD May 11 '17 at 11:47
0

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
Secko
  • 7,664
  • 5
  • 31
  • 37