Results 1 to 8 of 8

Thread: how to return a string in function?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    6

    Unhappy how to return a string in function?

    I have try this for 10 time but it can't work and I'am sure my logic is correct, is this coding correct::

    #include<stdio.h>
    #include<conio.h>
    //======================================
    char* Decodestr(char* encodedstr,char* codestr);
    //======================================
    void main(){

    char encodedstr[6]=" ";
    char codestr[6]=" ";

    clrscr();
    printf("Enter String:");
    scanf(" %s",&encodedstr);
    printf("Enter Code:");
    scanf(" %s",&codestr);
    Decodestr(encodedstr,codestr);
    printf("Encoded Result Is:%s");
    getch();
    }
    //========================================
    char* Decodestr(char* encodedstr,char* codestr){

    char *p1, *p2;
    p1=encodedstr;
    p2=codestr;
    char ans[6]=" "

    while(*p1!='\0'){
    ans[c]=*p1 + *p2;
    p1++;
    p2++;
    c++;
    }
    return;
    }
    //======================================

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Depends what you want to do.

    First: you may wish to ignore me as I am known for being foolish
    Second: There are some syntax errors etc:

    clrscr() - does this work still - doesn't on mine. (may not be a problem)

    No ';' on line - char ans[6]=" "

    'c' not declared for - ans[c]=*p1 + *p2; need int c = 0;

    printf("Encoded Result Is:%s"); - %s doesn't refer to anything.

    return; doesn't return anything. What do you want to return - the ans[] variable. You don't assign the function to anything anyway.

    Hope this helps

    HD

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't return strings this way. Search the forum for this, it has been discussed quite a few times.

    Remove the & in the calls to scanf. Or better yet, use gets (or even better fgets(stdin, buffer) ) for reading strings. fgets provides bounds checking and is faster than formatted input.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Lively Member
    Join Date
    Oct 2002
    Posts
    72
    I'll correct it maybe it will work not so sure coz didn't touch C for sometimes:

    #include<stdio.h>
    #include<conio.h>
    //======================================
    char* Decodestr(char* encodedstr,char* codestr);
    //======================================
    void main(){

    char encodedstr[6]=" ";
    char codestr[6]=" ";

    clrscr();
    printf("Enter String:");
    scanf(" %s",&encodedstr);
    printf("Enter Code:");
    scanf(" %s",&codestr);
    printf("Encoded Result Is:%s",Decodestr(encodedstr,codestr));
    getch();
    }
    //========================================
    char* Decodestr(char* encodedstr,char* codestr){

    char *p1, *p2;
    p1=encodedstr;
    p2=codestr;
    char ans[6]=" ";
    int c=0;

    while(*p1!='\0'){
    ans[c]=*p1 + *p2;
    p1++;
    p2++;
    c++;
    }
    return ans;
    }
    //======================================

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It will not reliably. Again, you can't return strings this way.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Lively Member
    Join Date
    Oct 2002
    Posts
    72
    hmmmmmm... issit?
    not quite remember but I've done that before. Only have partial memory about it that to return a string the start of the function will have:
    char* Decodestr(char* encodedstr,char* codestr){
    ......}
    as the return statement I forgot how whether an * needed or not.
    Or maybe can u provide the code? if possible.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    char * strToUpper(const char *instr, char *outstr)
    {
      const char *p = instr;
      char *q = outstr;
      while(*p) {
        *q++ = toupper(*p++);
      }
      *q = 0;
      return outstr;
    }
    
    // To call:
    char buffer[30];
    strToUpper("weird", buffer);
    The return value is the same as outstr, it's only provided for convenience, so that you can do things like
    char * str = strToUpper("bad", malloc(10));
    or pass the return value to another function like
    char buf1[100], buf2[30];
    strcpy(buf1, "This is ");
    strcat(buf1, strToUpper("good", buf2));
    // buf1 is now "This is GOOD"
    You should not do the malloc thing as it doesn't give you a chance to check the malloc return.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Lively Member
    Join Date
    Oct 2002
    Posts
    72
    ohhhhh... now it kinda make sense to me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width