function to return the date in a string

This is a discussion on function to return the date in a string within the C Programming forums, part of the General Programming Boards category; Hello I am trying to write a function that should return the date in a string in this format mm.dd.yy ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    6

    function to return the date in a string

    Hello

    I am trying to write a function that should return the date in a string in this format mm.dd.yy

    Example to day is 09 of dec 2005, it should return "120905".
    This is my program and the function:

    Code:
    #include <time.h>
    #include <stdio.h>
    #define SIZE 256
    
    
    char *tf();
    
         int main (void)
         {
           char *po;
           po=tf();
           fputs (po, stdout);
           return 0;
    
         }
    
    
    char *tf()
         {
           char *p;
           char buffer[SIZE];
           time_t curtime;
           struct tm *loctime;
           /* Get the current time. */
           curtime = time (NULL);
           /* Convert it to local time representation. */
           loctime = localtime (&curtime);
    
           strftime (buffer, SIZE, "%m.%d.%y", loctime);
           p=buffer;
           return p;
         }

    But when I run it it does not print anything to the screen.
    What is wrong here?
    regards
    fabian

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You can't return an automatic local variable from a function. buffer is allocated on the stack and as soon as your function ends the stack space that the tf() function reserved is free'd up.

    And your example date is a little confusing. Do you actually mean it should show "12.09.05" for today's date? Your example doesn't have dots.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    thanks , yes the format is as you say "12.09.05", sorry

    I thougth that the statement: "return p;" at the end of the function passes the date to the main program. correct me if I am wrong.
    How then can I get the date from the function?

    regards

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    a couple ways to do that.
    1. pass the buffer into the function from main()
    Code:
    char *tf(char buffer[])
    {
    
    }
     int main (void)
     {
           char *po;
           char buffer[SIZE];
           po=tf(buffer);
    }
    2. Or call malloc() to allocate memory for it, but main() will have to free() the pointer when done.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    thank you ancient dragon

    But why is this so difficult?
    Why is the difference between a function returning an int and a function returning a string?
    The statement "return p" is not supposed to return the string stored in "p"?
    regards
    fabian

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    returning a POD (plain old data) type such as int and float -- the compiler creates a copy of the object and returns that to the calling function. Returning arrays is completly different -- only a pointer to the array is returned to the caller. When the array is declared on the stack such as in your example, the data for the array is destroyed as soon as the function returns making the pointer that is returned invalid, pointing to memory that it no longer ownes. Returning p and returning buffer in your example are identical, they are both pointers to the same memory location.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    I got the idea now



    very short but at the same time very efective explanation.

    thank you very much
    Fabian

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    ok, returning to the same problem. I tried to re-write the main program and the function to do what I want. For some reason out of my understanding about functions, pointers etc this does not work as I want.

    This is my line of thought:

    1- I have this string in main():
    char string[]= "texto"

    Lets say that the OS assigns this block of address (memory address 100 throught memory address 105) to the program to store the string.

    mem add 100 == 't'
    mem add 101 == 'e'
    .
    .
    mem add 104 == 'o'
    mem add 105 == '\0'

    2- Now, in main() I define 2 pointers to a char like this:
    char *p_st1, *p_st2;

    3- Then I assign the address of the string to both pointers
    p_st1 = string;
    p_st2 = string;

    By doing those previous statements now p_st1 and p_st2 should point to memory address 100.

    4- Then I call a function called "funct" whose prototype is ;

    void funct(char *newp)

    This function takes a memory address, and writes new content to that memory address like this:


    Code:
    void funct(char *newp)
    {
    char news[] ="12345";
    newp = news;  // now the n-th element of the array news[] should replace the n-th element of the array in the main function
    }
    I call the function in the main program like this:

    funct(p_st1);

    What I intend to do is:
    The function takes the memory address pased by the main program (memory addres 100)
    The function has a local array of char called news[] ="12345"
    The OS assigns another block of memory for the function in the stack or the RAM (I dont know where), lets say the string news[] ="12345" is stored here, started at memory address 200:

    mem add 200 == '1'
    mem add 201 == '2'
    .
    .
    mem add 204 == '5'
    mem add 205 == '\0'

    5- Now the statement

    newp = news;

    should store the chars 1,2,3,4,5 and \0 starting at memory address 100, which is why I passed that pointer to the function. (that is what I think !!!!!)

    Now, the function does not return any value, BUT should have changed the contents of memory 100 throught 105 with its locals values.

    Then in the main program I tried to print the content of p_st2, which was pointing to memory address 100 and the result is that p_st2 still points to the old string "texto".

    Where am I failing?

    Here is the complete code:

    Code:
    #include<stdio.h>
    
    void funct(char *newp); // defining a function that takes a pointer to a char and
                          // returns also a pointer of char.
    
    int main()
    {
    char *p_st1, *p_st2;
    char string[]= "texto";
    p_st1 = string;
    p_st2 = p_st1; // p_st1 and p_st2 should point to the 1st element of the array "texto", which is the address of the first "t"
    puts(string); // prints "texto"
    puts(p_st1);  // prints "texto"
    
    funct(p_st1); // passing a pointer to the function and expecting a mofified array in the pointer "p_st2".
    
    puts(p_st2);   // now "p_st2" should point to the firts element of the modified array
    return 0;
    }
    
    
    void funct(char *newp)
    {
    char news[] ="12345";
    newp = news;  // now the n-th element of the array news[] should replace the n-th element of the array in the main function
    }

    Regards
    Fabian

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <string.h>
    
    void funct(char *newp)
    {
       strcpy(newp, "12345");
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    newp = news;

    should store the chars 1,2,3,4,5 and \0 starting at memory address 100, which is why I passed that pointer to the function. (that is what I think !!!!!)

    Now, the function does not return any value, BUT should have changed the contents of memory 100 throught 105 with its locals values.
    Nope. You're just assigning newp to the new address...So instead of pointing to memory address 100 it now points to memory address 200. The contents at memory address 100 remain unchanged.

    You'd have to do something like:
    Code:
    newp[0] = news[0];
    newp[1] = news[1];
    ...
    newp[5] = news[5];
    in order to copy the contents from news to the memory that newp points to. This is exactly how strcpy() works. In your function you could just do strcpy(newp, news);. And it's also why you can't just do string1=string2 to copy a string. All you're trying to do there is assign the value of one pointer to another.

    It looks like you're just about to discover the whole fundamental knowledge of C that so many people miss. Once you understand pointers everything else is cake. So don't give up!
    Last edited by itsme86; 12-12-2005 at 10:33 AM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    I feel like a moron:

    of course :
    newp = news;

    It is the same comand that i used in the main program to assign the mem adress of "texto" to p_sp1.
    I am doing the same here!!!!!!!!!
    How could miss that???????????????!!!!




    Dave:
    I did not know about that function. It works perfect. Thanks!!!!!

    Itsme86:

    Yes!!! That solution works too.

    Code:
    void funct(char *newp)
    {
    int i = 0;
    char news[] ="12345";
    
    for (i = 0; i < 5; i++)
    {
    newp[i] = news[i];
    }
    }
    Thank you very much guys

    Fabian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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