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.

TI Example Code Syntax Problem



Hello all, 

I'm trying to understand how to code for the RFL430CL331H NFC tag by going through the example code that was provided for it. In the example code there is often this syntax used:

typedef struct NdefFile_Type

{

unsigned char FileID[2]; // The NFC file ID

unsigned char * FilePointer; // the location in MCU memory where it is located

unsigned int FileLength; // the length of the file

}NdefFileType;


NdefFile_Type file;
file.FilePointer= (uint8_t *) data;

I understand what pointers are, but what does the "(uint8_5 *) data;"  part of the line actually mean?

I would think that the value of file.FilePointer should be set to an address like "&data", but why is it set to "(uint_8 *) data"? If I assume the parenthesis does nothing, then it makes less sense because this would be using the data value as an address and using a random value that happens to be stored at the address that matches the data value. 

Thanks, 

Victor Wang

  • Hi Victor,

    This syntax means that file.FilePointer is being set to a pointer to an 8-bit integer that points to the value of data. So, because it is a pointer, it is being set as an address. The parentheses ensure that the * is applied to the data type (uint8_t).

    Regards,

    Nathan

  • We could tell you more if you tell us how data is declared and used, but I would guess that data is a pointer to a char array containing the file name. Your filesystem expects an array of uint8_t's for the same thing. While they are probably equivalent (chars *could* be signed), they cast data - a pointer to some other type - to an uint8_t to shut the compiler up.
  • Hello Nathan,

    Sorry about the late response! I'm still a bit confused about this; do you mean that "(uint8_t *) data" is another pointer that points to the value of an 8-bit integer called data?

    Wouldn't it be easier to just do the following instead?
    file.FilePointer=&data;

    Is there a difference between the result of the two?

    Thanks,
    Victor
  • Hello Keith,

    I have attached a chunk of code that I am looking at. I'm new to utilizing NFC and making a file system, so this is all a bit confusing...

    In the original post, I used data instead of FileTextE104. FileTextE104 seems to be an array containing NFC values that are being transmitted later. Why is the same name also being used as a pointer afterwards with the (uint8_t *) line? I'm still not understanding the whole syntax of (uint8_t *). It makes more sense to me to just use: NdefFile[1].FilePointer=&FileTextE104[0]; Is there a difference?

    /****this is from the header file****/
    typedef struct NdefFile_Type
    {
    unsigned char FileID[2]; // The NFC file ID
    unsigned char * FilePointer; // the location in MCU memory where it is located
    unsigned int FileLength; // the length of the file
    }NdefFileType;
    /********************************/

    extern unsigned char FileTextE104[]; //NFC NDEF File
    uint8_t FileTextE104[9000] = {
    0x00, 0x16, /* NLEN; NDEF length (3 byte long message) */
    0xD1, 0x01, 0x12,
    0x54, /* T = text */
    0x02,
    0x65, 0x6E, /* 'e', 'n', */

    /* 'Hello, world!' NDEF data; */
    0x48, 0x65, 0x6C, 0x6C, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21

    };//Ndef file text

    uint16_t SelectedFile; //the file that is currently selected

    /***************************************************************************************************************************************
    * AppInit
    ***************************************************************************************************************************************
    *
    * Brief : Initializes the file management
    *
    *********/
    void AppInit()
    {
    // Init CC file info
    NdefFiles[0].FileID[0] = 0xE1;
    NdefFiles[0].FileID[1] = 0x03;
    NdefFiles[0].FilePointer = (uint8_t *)CCFileText;
    NdefFiles[0].FileLength = 0; //?


    // Init Ndef file info
    NdefFiles[1].FileID[0] = 0xE1;
    NdefFiles[1].FileID[1] = 0x04;
    NdefFiles[1].FilePointer = (uint8_t *)FileTextE104;
    //NdefFiles[1].FileLength = 0; //?

    NumberOfFiles = 2; //the number if NDEF files available
    SelectedFile = 0; //default to CC file
    }

    Thanks,
    Victor Wang
  • Hi Victor,

    the expression (uint8_t *) is a typecast, which is common expression in the C language, at least for embedded software.

    In this case it explicitly casts/converts the pointer from one type into another. In an implicit case (without typecast) the compiler might treat this instruction as a 'Warning/Error'.

    Best regards

    Christoph

  • Since you did not tell us the type of FileTextE104, I can't comment. But it is likely that there is no difference, for the vast majority of computers nowadays* an unsigned char is the same thing as uint8_t, but the cast is wrong, it should be a cast to unsigned char.

    *Some computers had 9 bit bytes and 36 bit words!
  • Hi Victor,

    Please see Christoph's explanation. Using the explicit typecast will ensure that it works correctly, and it is better coding style. But, for your understanding, you can think of them as functionally equivalent.

    Regards,
    Nathan

**Attention** This is a public forum