|
|
View previous topic :: View next topic |
Author |
Message |
sjharris
Joined: 11 May 2006 Posts: 78
|
Returning a string from a function |
Posted: Tue Aug 16, 2011 7:54 am |
|
|
Hello all,
I am passing a string to a function and doing some operation on that string. Basically I am stripping out strings within the original string. Using a comma separation, I pass the string and the field number that I want to the function.
The function correctly separates the string and I have a char array with the string that I want in it.
But I cannot seem to return that result to the main program. I can return the first byte but cannot return the whole string.
Any ideas?
Thanks |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Aug 16, 2011 8:15 am |
|
|
How about showing some code? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Aug 16, 2011 8:16 am |
|
|
Hi,
It's hard to tell without seeing your actual code, so why not just post the function for us to look at?
Thanks,
John |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Tue Aug 16, 2011 8:53 am |
|
|
Hopefully this will help??
Code: |
/********************************************************************
*
*
*********************************************************************
* Scope:- Test SD Card read functions
*********************************************************************
*
* Changes:-
*
*********************************************************************
*
* To Do:-
*
*********************************************************************
*
*
********************************************************************/
/*
SD Card PIC
------- ---
CS RB3
CLK RC3
DO RC4
DI RC5
*/
/*
SD Card address
Address Description
Pg File Address
0x38 0x7000 First file - Settings
0x3C 0x7800 Second file- Data / Logging from PIC
0x18 0x3000 File counter
*/
//*******************************************************************
// Includes
//*******************************************************************
#define MMC_CLK PIN_C3
#define MMC_DI PIN_C4
#define MMC_DO PIN_C5
#define MMC_CS PIN_B3
#include "D:\Tests\test.h"
#include "D:\Tests\flexLCD.c"
#include <mmc_spi.c>
#include <string.h>
#include <stdlib.h>
//*******************************************************************
// Global Variables
//*******************************************************************
int8 data0[512];
int8 commands[512];
int16 timer1_counter = 0;
#BIT rcsta_cren = 0xFAB.4
#BYTE rcsta = 0xFAB
volatile unsigned char string_len = 0;
#define MAX_STRING_LEN (32)
char input_string[MAX_STRING_LEN+1];
volatile boolean string_flag;
//*******************************************************************
// Interrupt Routines
//*******************************************************************
#int_RDA
void RDA_isr()
{
char c;
if(rcsta & 6)
{
rcsta_cren=0;
rcsta_cren=1;
c = getc();
}
else
{
c = getc();
input_string[string_len++]=c;
if(string_len==MAX_STRING_LEN || c==10)
{
input_string[string_len++]=0;
string_len = 0;
string_flag = TRUE;
}
}
}
#int_TIMER1
TIMER1_isr()
{
timer1_counter++;
}
//*******************************************************************
// Functions
//*******************************************************************
separate_message(char *input, int field_num)
{
char output[32];
int *out;
int i,counter,start_pos;
puts(input);
counter = 0;
for(i=0; i< strlen(input); i++)
{
if(input[i]==',')
{
counter++;
if(counter == field_num)
{
start_pos = i;
}
}
}
start_pos++;
i=0;
while(input[i+start_pos] != ',')
{
output[i] = input[i+start_pos];
i++;
}
out=output[0];
output[i]=0;
printf("Output %s\n\r", output);
return out;
}
//*******************************************************************
// CCS PIC Init
//*******************************************************************
void CCS_Init()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_spi2(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
}
//*******************************************************************
// Other Init
//*******************************************************************
//*******************************************************************
// Main
//*******************************************************************
void main()
{
int response;
int attempt;
int16 count, i;
int *ptra; //Pointer used in
int program_counter; //Counter used to count number of commands
int counter; //General purpose counter, do not use to store data
int number_of_commands;
char command[32];
int tens, unit;
char message[32];
char reply[32];
char timeout[3];
int8 retry_time;
int *messages;
ptra = (data0[0]);
count =0;
attempt = 48;
response = 0;
CCS_Init(); //Initialise PIC
lcd_init(); //Initialise LCD
response = 18;
lcd_putc("SD Read test"); //Write on lcd
lcd_gotoxy(1,2);
lcd_putc("Init"); //Write on lcd
output_high(PIN_F0);
while (response !=0)
{
response = mmc_init();
lcd_gotoxy(6,2);
lcd_putc("Error ");
lcd_putc(response + 48);
delay_ms(1000);
}
lcd_gotoxy(1,2);
lcd_putc("Init OK "); //Write on lcd
messages = separate_message(command,2);
printf("Message :- %s\n\r", messages);
}
|
command has contents:- M,test1,test2,1,
output is:-
Output test2
Messages rubbish characters
Cheers |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Aug 16, 2011 9:19 am |
|
|
Some comments:
If you just add the keyword 'ERRORS' to your #use RS232 declaration, the compiler will automatically clear the UART error condition for you, allowing about half your INT_RDA, and setup code to disappear.....
First big problem though is you have not declared your 'separate_message' function as returning anything. By default a function will be treated as returning an integer in this case. An integer in CCS C, is an 8bit value. You need it to return a int/char pointer, which is larger than this. Get your function type declaration sorted...
Second problem is variable scope. A local variable only exists while it is being used. You return 'out' as a pointer to the char array 'output', but this array ceases to exist, when you leave the function (bits of it may well still exist in memory, but this is not guaranteed). You need to declare 'output' as _static_, so that it is retained when you leave the function.
Then there is a size problem in some cases. If the input buffer is full, it can contain 33 characters including the '\0'. You only have space for 32 in output. The same applies to command in the main code.
Then you have the problem of running off the end. In CCS, there is no operating system for the code to return to. If you run off the end of the code, things just stop. The last few characters of 'messages', will be unsent at this point.
Best Wishes |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Aug 16, 2011 11:03 am |
|
|
Here is an excellent discussion of returning an array (string) in C.
http://c-faq.com/~scs/cclass/int/sx5.html _________________ Google and Forum Search are some of your best tools!!!! |
|
|
|
|
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
|