|
|
View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
How to call a function that expect a string |
Posted: Wed Nov 27, 2019 12:30 pm |
|
|
I need to do this
Code: | NewMsg(GetContactID(BufferCmdMain.Qual,BufferCmdMain.Partition)); |
Prototypes
Code: | void NewMsg(char ContactID[12]);
char *GetContactID(char Qual,char Partition); |
The second function is like this
Code: | char *GetContactID(char Qual,char Partition)
{
char ContactID[12]
sprintf(ContactID,"%1u A%1u ",Qual,Partition);
delay_cycles(1);
delay_cycles(1);
delay_cycles(1);
return ContactID;
} |
_________________ Electric Blue
Last edited by E_Blue on Wed Nov 27, 2019 12:38 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Nov 27, 2019 12:38 pm |
|
|
First get rid of the size in the function definition.
In C, in general, a 'string' passed to a function is just a pointer to
the data. No size associated with it. Some later C's support a size which
is then used for error checking if the data passed is larger than
required, but this is not supported in CCS.
Then just call it.
However 'caveat'. remember a string needs to be one character
larger than what it may contain. So if this ID is 12 characters, the
character array must be 13 characters long.
Your call would be:
Code: |
char *GetContactID(char Qual,char Partition)
{
char ContactID[12]
sprintf(ContactID,"%1u A%1u ",Qual,Partition);
delay_cycles(1);
delay_cycles(1);
delay_cycles(1);
NewMsg(ContactID);
return ContactID;
}
//or if you mean call with the result of the function:
NewMsg(GetContactID(x,y));
//where x and y are what the function needs
|
|
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Nov 27, 2019 12:43 pm |
|
|
I just made some changes in the original post; would you read it again please.
Answering to your comment, I already do it like that, I mean I called the function like this
Code: | NewMsg(GetContactID(x,y)); |
But when I get inside the NewMsg function, ContactID=0x0865; so I'm getting the address of the first digit; that's why I'm here; I don't understand what I'm doing wrong. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Nov 27, 2019 1:16 pm |
|
|
That is what it should contain. As I said, in C "In C, in general, a 'string' passed to a function is just a pointer to the data". It sounds as if your
uise of this in the NewMsg function is what is faulty.
Functions like strcmp etc., all just expect to receive such a pointer.
Code: |
void demo(char IDString[])
{
while (*IDString != '\0') //walk through the string
{
print("Char value is %d", *IDString);
IDString++;
}
}
|
This shows a function receiving a string, and walking through this
printing the ASCII value of each character. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Nov 27, 2019 1:51 pm |
|
|
Inside of the NewMsg function I'm calling a third function
Code: | char CrearRTH(char VectorRegla,short onlineMode,short FromUSB,char ContactID[], char *BufferX) |
This way
Code: | MemoTxBuffer[1]=(CrearRTH(xVectorBit,FALSE,FALSE,ContactID,MemoTxBuffer+2))+8; |
Inside that third function I create a bigger string concatenating many little strings from different functions.
Close to the end I have
Code: | XPos+=sprintf(BufferX+XPos,"%s00",*ContactID); |
XPos have the amount of characters in the string(Length)
And it was working until I added *GetContactID function
Prior to this I was calling this function like this and work ok.
Code: |
void PanicAlarm()
{
char ContactID[12];
sprintf(ContactID,"112A A1 AAA");
NewMsg(ContactID);
|
_________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Nov 27, 2019 2:10 pm |
|
|
Seriously, you are not showing us enough to help.
My guess would be that your string building function is not correctly
creating a string (remember a string must be null terminated).
Also there is an issue in your original post, in that you try to return
a pointer to a character array inside the function. This does not exist
once the function exits. Local variables only actually exist while the
function is operating. If you want to return a pointer to a array from
inside a function, this array needs to be declared as static so it
remains in existence when the function exits. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|