-1

I am unable to understand some portion of code in a header file and it's usage. I have a header file called xxx.h with a type defined structure and some member variables inside it like this :

typedef Struct{ 
int x,y,z;      //members inside structure
}myStruct;

#define base 0x02300  // must be a RAM address

#define access ((myStruct *) base)

I am unable to find out what the definition #define access ((myStruct *) base) actually does in my code. also in my source file yyy.c, I could access the structure variables using this line:

#include "xxx.h"    
void main()
  {
    access->x = 23;
  }

What i really understand after referring the "pointer to structure " section in C language is I could create a new pointer variable of type myStruct and access the variables inside the structure with that variable using the "->" operator. Please help me to understand this code. thank you

  • 2
    This is just mapping a struct to a hard-coded memory address in the usual way (quite common for small embedded applications) - which part are you having difficulty understanding ? Do you have [a good book on C](http://stackoverflow.com/a/562377/253056) ? – Paul R Feb 20 '17 at 10:57
  • `typedef Struct` is invalid in C. Your compiler should complain. – too honest for this site Feb 20 '17 at 11:48
  • For a very basic understanding, this creates a `struct` of the type `myStruct` at the address `0x02300`. `access` is a pointer to that struct. – Toby Feb 20 '17 at 12:51
  • @Paul R Hello Paul, "base" is an address of a particular GPIO Port, and members inside the structure are register names. I find it difficult in understanding how that Typecasting actually works. Is this line **((myStruct *) base)->x = 23 ;** an implicit declaration of a new pointer variable of type myStruct at the address of "base"? – Rakesh Menon Feb 20 '17 at 14:53

1 Answers1

2

The C preprocessor simply does textual replacements before the actual compile process takes place:

With these defines:

#define base 0x02300  // must be a RAM address
#define access ((myStruct *) base)

this code:

access->x = 23;

will actually be transformed into this by the preprocessor:

((myStruct *) base)->x = 23 ;

which will end up in this code (because of #define base 0x02300)

((myStruct *) 0x02300)->x = 23 ;

I suggest you read the chapter dealing with the preprocessor in your C textbook.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thank you Michael for responding, First of all, it's not about the pre- processor directives that I don't understand, I am unable to understand that "casting" of memory address to a structure pointer. – Rakesh Menon Feb 20 '17 at 14:34
  • @RakeshMenon there is actually not much to understand here. At address `0x02300` you have a `myStruct` structure, this seems to be dictated by your underlying hardware/software/controller or whatever. So "casting" of that memory address to a pointer to `myStruct` is actually just telling the C compiler that you would like to access the `mystruct` structure that is located at address `0x02300`. – Jabberwocky Feb 20 '17 at 15:46