How to send Macro to function with poinetr

Hi,

I have function with one input parameter, and I want to send there macro, how should be it done?

1
2
3
4
5
6
#define FRAM  0b11

void function(unsigned char * data)
{
//some code
}


So how should I send my macro?

 
 function(FRAM);

or
 
 function(&FRAM);

or anything else posibilities?

Thanks a lot.
Last edited on
You can't. One of the many reasons why you should not use macros unless there is no other option.

Macro is a naive text replacement. That is... when you type function(&FRAM) the preprocessor replaces the macro and the compiler sees function(&0b11)

Which is nonsense.


Instead of using macros, you should use a constant variable:

1
2
3
4
5
6
7
8
9
const unsigned char FRAM = 0x11;

// ...
void function(const unsigned char* data)
{
}

//...
function(&FRAM);  // now this will work 


Note a few things:

1) If the constant is const... the pointer to it must also be const. Though why does 'data' need to be a pointer if there's only 1 element?

2) '0b11' is nonsense and will give you a compiler error. Did you mean '0x11'?
Thanks a lot!
My function is little bit complicated nad I write it for Xmega procesor in Atmel studio so there is 0b as binary number:
1
2
3
const unsigned char READ_FRAM=0b00000011;
const unsigned char WRITE_FRAM=0b00000010;
const unsigned char WREN_FRAM=0b00000110;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void WriteToFram(uint8_t *data, uint8_t Length)
{
	uint8_t *out;
	uint8_t Temp=0;
	out = (uint8_t *) &CounterAdress;

	spi_select_device(SPI_FRAM.spi, &SPI_F);			// Select radio IC by pulling its nSEL pin low
	spi_write_packet(SPI_FRAM.spi,&WREN_FRAM,1);		// Send data array to the radio IC via SPI
	spi_deselect_device(SPI_FRAM.spi, &SPI_F);			// De-select radio IC by putting its nSEL pin high

	spi_select_device(SPI_FRAM.spi, &SPI_F);			// Select radio IC by pulling its nSEL pin low
	spi_write_packet(SPI_FRAM.spi,&WRITE_FRAM,1);		// Send data array to the radio IC via SPI


	spi_write_packet(SPI_FRAM.spi,&out[2],1);		// Send data array to the radio IC via SPI
	spi_write_packet(SPI_FRAM.spi,&out[1],1);		// Send data array to the radio IC via SPI
	spi_write_packet(SPI_FRAM.spi,&out[0],1);		// Send data array to the radio IC via SPI

	spi_write_packet(SPI_FRAM.spi,out,Length);		// Send data array to the radio IC via SPI

	spi_deselect_device(SPI_FRAM.spi, &SPI_F);			// De-select radio IC by putting its nSEL pin high


}
Last edited on
Topic archived. No new replies allowed.