how to change and return a string from function?

This is a discussion on how to change and return a string from function? within the C Programming forums, part of the General Programming Boards category; i want to delete the last char in a string, and return it to main. it is my program but ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    Question how to change and return a string from function?

    i want to delete the last char in a string, and return it to main.

    it is my program but doesn't work!
    pls help!


    #include <stdio.h>

    void ClrR(char**);

    void main(void)
    {
    char* a;
    a="1234";
    ClrR(&a);
    }

    void ClrR(char** str)
    {
    int pos;

    pos = strlen(*str)-1;
    *(*str+pos)='/0';
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    1. Function main must be of type int and return a return-value.
    2. A double pointer is not needed, ClrR wants the address of the string. Since a is a pointer, you can pass a.
    3. It's easier to treat a string as an array of chars in situations like these.
    4. You need to pass the last character back to main if you want main to have it.


    Code:
    #include <stdio.h>
    
    void ClrR (char*, char *);
    
    int main(void)
    {
        char* a = "1234";
        char ch;
        
        ClrR(a, &ch);
        
        printf ("%s %c\n", a, ch);
        
        return 0;
    }
    
    void ClrR(char* str, char *ch)
    {
        int pos;
    
        pos = strlen (str) - 1;
        
        *ch = str [pos];
        str [pos] = '\0';
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    i can complie the program now,
    but i cannot run the program,
    "segmentation fault"

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Hmmm. Strange, I have no problems. I'm using GCC on Win98. What's your environment?

    [EDIT]

    Compiled now with LCC, gives error for strlen. Try including string.h.

    [/EDIT]
    Last edited by Shiro; 03-23-2002 at 07:26 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    my environment:
    i'm using gcc-2.95 in slackware 8....

    i've already instring.h but still segmentation fault!

    pls help!

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    :-)

    Yep, MS Visual C++ also crashes. But when using

    char a [] = "1234";

    it works.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    it works now
    but i want to allocate a dynamic memory to the string...

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You can use malloc to allocate memory. Don't forget to free the memory when you don't need it anymore!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char* str;
    
        str = malloc (5);
        
        strcpy (str, "doei");
            
        printf ("%s %c\n", str);
    
        free (str);
            
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    Smile

    i got it.
    think you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21