return textstring ??

This is a discussion on return textstring ?? within the C Programming forums, part of the General Programming Boards category; Hi Is it possible to return a textstring. ex: I need to return the time in a program. But if ...

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

    return textstring ??

    Hi

    Is it possible to return a textstring.
    ex: I need to return the time in a program.
    But if I put the time in a textsting, how can I return it, so I can
    call the function gettime() from another program??

    tx
    ------------------------ex: code

    int gettime()
    {
    struct time t;
    gettime(&t);
    printf("The current time is: %2d:%02d:%02d\n",
    t.ti_hour, t.ti_min, t.ti_sec);

    return ??;

    }
    !G!

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    A quick and dirty way of returning strings is to put the burden of memory management on the recepient. Caller will have to free() the memory it receives.

    Code:
    char *gettime()
    {
      struct time t;
      gettime(&t);
      char *pc = (char *)malloc(1024);
      sprintf(pc, "The current time is: %2d:%02d:%02d\n", t.ti_hour, t.ti_min,  t.ti_sec);
    
      return pc;
    }
    Something like that (allocation can be done in a better way);

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What you will want to do is return a char*, so it would look like this:
    Code:
    char* GetTime() 
    { 
        char *Time = malloc(sizeof(char)*256);
        struct time t; 
        gettime(&t); 
        snprintf(Time, 256, "The current time is: %2d:%02d:%02d\n", t.ti_hour, t.ti_min, t.ti_sec); 
    
    return Time; 
    }
    What it does is create a char* called Time. Time is created with a size of 256. snprintf stores the message like printf into a string (Time). Then the time is returned. You could call GetTime() like this:
    Code:
    char* CurrentTime;
    CurrentTime = GetTime();
    //Later in your program you need to free CurrentTime
    free(CurrentTime);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    cannot convert void * to char * ??

    tx for your reply, but the error message is as follows:

    Cannot convert 'void *' to 'char *'


    char* gettime()
    {
    char *Time = malloc(sizeof(char)*256); /* this line ??*/
    struct time t;
    gettime(&t);
    sprintf(Time,"The current time is: %2d:%02d:%02d\n", t.ti_hour, t.ti_min, t.ti_sec);
    return Time;
    }

    int freeAllocated()
    {
    char* CurrentTime;
    CurrentTime = gettime();
    free(CurrentTime);
    return 0;
    }


    int main()
    {
    gettime();
    getch();
    freeAllocated();
    getch();
    return 0;
    }
    !G!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Cannot convert 'void *' to 'char *'
    You're probably compiling this code as C++.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    yes, that why, but

    Howcome it dosn't work in C++.?
    !G!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C++ has a much stronger type checking than C, so you have to cast the return of malloc before assigning it. This is one of the places where perfectly valid and correct C code will not compile as C++.

    -Prelude
    My best code is written with the delete key.

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

    No warning/Errors now, but nothing?

    Now it seems to work , but not text is comming up.

    Is the something missing.
    Here is the whole test code:

    ---------code clip

    #include <stdio.h>
    #include <conio.h>
    #include <alloc.h>
    #include <dos.h>
    char* GetTheTime(void);
    int freeAllocated(void);

    char* GetTheTime()
    {
    char *Time = malloc(sizeof(char)*256);
    struct time t;
    gettime(&t);

    sprintf(Time,"The current time is: %2d:%02d:%02d\n", t.ti_hour, t.ti_min, t.ti_sec);
    return Time;
    }

    int freeAllocated()
    {
    char* CurrentTime;
    CurrentTime = GetTheTime();
    free(CurrentTime);
    return 0;
    }


    int main()
    {

    GetTheTime();
    getch();
    freeAllocated();*/

    getch();
    return 0;
    }
    !G!

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

    Sorry no need to reply to this!!

    I figured it all out.
    Tx a lot for all your help...
    !G!

  10. #10
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Last edited by shaik786; 06-18-2002 at 04:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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