This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Getting the address of a literal value in C



Hi,

I want to pass the address/pointer of a literal value  to a function taking pointer to a char but I don't know how to get that address. I want to do sth like that:

WriteTX(   ADDRESS_OF( 'a'), 1);

The function definition is:

typedef unsigned char uchar;

uchar WriteTX(uchar * pBuf, uchar bytes)
{
uchar status, i;
CSN(0);
SPI_send(FLUSH_TX);
SPI_send(WR_TX_PLOAD);
for(i=0; i<bytes; i++)
status = SPI_send(pBuf[i]);
CSN(1);
return(status);
}
Thanks for  help
  • A literal value of a simple type has no memory address. it is an immediate value and put directly into any instruction. Or, in case of a function call, it is passed by value. Which isn't what you want.

    A string constant, however, is stored in constant memory and referenced by a pointer. When passing a string constant as a funciton parameter, it is always passed by reference.
    So you can write WriteTX("a",1); Wasting  a second byte for the string delimiter. Well, won't make a difference as mso tobjects in flash are word-alinged anyway.

    alternatively, you can put the char value into a variable:
    unsigned char data = 'a'
    WriteTX(&data,1); // pass value 'data' by reference

    There's a way you can change WriteTX so that it accepts both, a value or a pointer, but this is rather weird stuff :)

**Attention** This is a public forum