Reputation: 1151
I'm trying to create a function that creates an string in RAM of a 16 bit microcontroller. The function prototype is like this
char CID_Str[12] GetContactID(char Qual,long Event,char Partition,char Zone);
The function should return a little string created with the input parameters.
I never do it a function like this before. I don't need help with the code inside the function. I need to know how should I declare the returning parameter (Buffer) because the CCS compiler doesn't like that prototype.
Upvotes: 2
Views: 661
Reputation: 14044
Arrays can be returned as a pointer to the array element type. The returned pointer would be the address of the first element in the array (or memory region). In your case:
char *GetContactID(char Qual,long Event,char Partition,char Zone);
And to your comment:
I just created a buffer inside the variable like this char ContactID[12]; then at the end of the function i have a return ContactID;
Be sure not to return stack addresses (e.g. local variable address). Such an address is only valid within the scope of the function and becomes an invalid (dangling) pointer if returned to the caller. Main options are:
The first two are the usual cases.
Update: I missed the fact that you are running on a microcontroller. In that case, static memory may be more appropriate as such systems tend not to want the overhead and complexity of dynamic allocations.
Upvotes: 4