Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Returning a string from ANSI-C

Status
Not open for further replies.

smartsarath2003

Member level 4
Joined
Feb 12, 2004
Messages
77
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
1,061
Hai,
I want to return a srting buffer from a C function. I have trouble doing that now

My sample code:

-----------------------------------------
char myfunction(void)
{
int i=1;
char buff[25]="Hai";
i=i+1;
return *buff;
}

char str_value = myfunction(void);
--------------------------------------------------

I am getting "88" in str_value when I return buff[25] and "115" when I return *buff, but not "Hai" in both cases.

Can some one please help me:!:
Thanks
 

gliss

Advanced Member level 2
Joined
Apr 22, 2005
Messages
692
Helped
75
Reputation
150
Reaction score
16
Trophy points
1,298
Activity points
5,892
There are many problems here.
I think the Programming forum would be more appropriate for this thread and return more helpful responses:
https://www.edaboard.com/forum7.html

When posting code, it's helpful to the reader to use the code tag:

Code:
char myfunction(void)
{
int i=1;
char buff[25]="Hai";
i=i+1;
return *buff;
}

char str_value = myfunction(void);
Next, you are trying to return a local value by reference. buff is local to myfunction(). You need to pass by value, not by reference.
Next, your indexing is off. C based indexing starts at 0 and goes to size -1.
buff[25] hasn't been allocated. Valid addresses are buff[0] to buff[24].
If you use buff[25] = "Hai" as assignment then only buff[0], buff[1], buff[2] have been defined. Anything else is just what is memory.

Here is an example using a global variable.

Code:
#include <stdio.h>
#include <string.h>
 
char myfunction(void);
char buff[3];
 
int main ( void /*int argc, char *argv[]*/ ){
 
        printf("Before myfunction()...\n");
        puts(buff);
        printf("After myfunction()...\n");
        myfunction();
        puts(buff);
        return 0;
}
 
char myfunction(void)
{
        int i = 1;
        strcpy(buff, "Hai");
        //buff = "Hai";
        i = i + 1;
        return buff[0];
}
 
//char str_value = myfunction(void);
Here is using pass by reference:


Code:
#include <stdio.h>
#include <string.h>
                                                                                
char * myfunction(char *);
char buff[3];
                                                                                
int main ( void /*int argc, char *argv[]*/ ){
                                                                                
    char buff[3];
    printf("myfunction()...\n");
    puts(myfunction(buff));
    return 0;
}
                                                                                
char * myfunction( char * buff_pass )
{
    int i = 1;
    strcpy(buff_pass, "Hai");
    //buff = "Hai";
    i = i + 1;
    return buff_pass;
}
                                                                                
//char str_value = myfunction(void);
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top