Thread: adding __TIME__ macro to char malloc

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    adding __TIME__ macro to char malloc

    Code:
    
    char *time=(char*)malloc(9*sizeof(char *));
    	time=__TIME__; 
    	for(i=0;i<9;i++);
    		if(time[i]>='0' && time[i]<='9')
    		printf("correct");
    but it's not working... any help please?
    the 'error message' its no print message
    any help would be grateful
    what i want is to get every number of the time for example if time is 20:22:23
    i want to get a string 202223
    Last edited by cable; 09-29-2011 at 12:35 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose the semicolon lon line 3 ...

    Don't typecast the return value of malloc() ... if your compiler complains about it's because it thinks it's compiling C++.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    __TIME__ is a string literal. Your allocated memory leaks out when you assign the __TIME__ address to your pointer.

    You could use:
    Code:
    const char* time = __TIME__;
    or:
    Code:
    char* time = malloc(strlen(__TIME__) + 1);
    
    strcpy(time, __TIME__);
    Last edited by GReaper; 09-29-2011 at 01:03 PM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    i have made this change but a strange thing happens look
    my new code and the output
    Code:
    char* time = malloc(strlen(__TIME__) + 1);
    strcpy(time, __TIME__);
    for(i=0;i<strlen(__TIME__) + 1;i++)
    if(time[i]>='0' && time[i]<='9')
    printf("%d\n",atoi(time+i));
    output
    Code:
    22
    2
    49
    9
    44
    4
    than
    22
    49
    44

  5. #5
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    Just try this.
    Code:
    int a, b, c;
    char* time = malloc(strlen(_TIME__) + 1);
    strcpy(time, _TIME__);
    sscanf(time, "%02d:%02d:%02d", &a, &b, &c);
    printf("%02d\n%02d\n%02d", a, b, c);
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  6. #6
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    This would work to.
    Code:
    int i;
    char* time = malloc(strlen(__TIME__) + 1);
    strcpy(time, __TIME__);
    for(i=0; i<strlen(__TIME__)-1; i++)
    {
        if(time[i]==':')
        {
            time[i]='\n';
        }
    }
        printf("%s", time);
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or even this
    Code:
    sscanf(__TIME__, "%02d:%02d:%02d", &a, &b, &c);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. adding extra to char*
    By sujeet1 in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 12:21 PM
  4. Adding to char variables together
    By maxorator in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2005, 10:27 AM
  5. Adding .txt to a char
    By ErionD in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2002, 04:01 PM