Home > Net >  Pass buffer as argument to function
Pass buffer as argument to function

Time:05-21

I want to pass a buffer as argument to a function and use it somewhere else afterwards. I've tried different ways:

void parseData(uint8_t Data);
void parseData(uint8_t *Data);
void parseData(uint8_t Data[]);

and then called the function to use it

data_result[10U]={1,2,3,...,10};
parseData(data_result);

but none of them worked.

CodePudding user response:

The function call syntax as well as the declaration for data_result is incorrect. The correct syntax would be as shown below:

void parseData(uint8_t *Data)
{
    
}
int main()
{
//--vvvvvvv------------------------------->added type of element of the array
    uint8_t data_result[4]={1,2,3,10};
//------------v--------------------------->removed uint8_t from here 
    parseData(data_result);
    
}

You can also pass the size of the array as the second argument as shown below:

//-----------------------------------vvvvvvvvv--->second parameter is size of array
void parseData(uint8_t *Data, size_t arraySize)
{
    
}
int main()
{
    uint8_t data_result[4]={1,2,3,10};
//-------------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv------->pass size of array as second argument
    parseData(data_result, sizeof(data_result)/sizeof(data_result[0]));
    
}

CodePudding user response:

I quess you forgot to define data_result properly. The type is missing.

uint8_t data_result[10U]={1,2,3,---,10} might work.

But it would be great if you give more information about what is not working, like a compiler error.

  •  Tags:  
  • c
  • Related