3 years, 7 months ago.

How can i pass the buffer char variable from the main? Code problem

Hi everybody, I am using a very simple code but i am a newbie regarding programming. The code that you can see in the attached images works nice. But i would like to change the code and create the buffer char variable in the main code instead of creating it in the SamplesyncWriteandchangeall.cpp (code lines 86-88) and pass it to the other file ( SamplesyncWriteandchangeall.cpp). Summing up, i would like to move the code lines 86-88 to the main code and call it from the other file (SamplesyncWriteandchangeall.cpp). . /media/uploads/jmol/writetext.jpg /media/uploads/jmol/main.jpg

Thanks for your attention.

For future reference, when posting code please use

the code

It's more readable then screen shots and lets people copy and paste the code to make changes.

posted by Andy A 08 Dec 2017

1 Answer

3 years, 7 months ago.

So... in main.cpp, declare the buffer and then pass it, together with the size to the function. You need the size because buffer is a pointer, and a pointer has no idea of how much data it points to.

int main(void) {
    char buffer[50];
    int size = sprintf(buffer, "blablabla");

    write_nfc_tag(buffer, size + 1); // size+1 because sprintf() does not return the last \0 character in the count

Then in your other file:

int write_nfc_tag(char *buffer, int size) {
    // use the buffer like you'd normally use it