Thread: Noob trouble: returning strings (pointers to char)

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    31

    Noob trouble: returning strings (pointers to char)

    Hi, I'm relatively new to C and extremely new to pointers. I use Dev-C++ as my IDE and when I compile my program I get warnings such as "[Warning] passing arg 1 of `printf' makes pointer from integer without a cast ". The thing is, I don't get this error if I simply open the IDE and instantly press "compile" but I do get it if, say, the printf() line was commented before and then I remove the comment, with the final code being identical. I'm guessing that I'm doing something wrong with pointers. Here's the program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        FILE *outfile;
        outfile = fopen("output.txt","w");
        if(!outfile)
        {
                    puts("Oh noes!");
                    exit(0);
        }
        time_t rawtime = time(NULL);
        printf(mlTimeString(&rawtime));
        fprintf(outfile, mlTimeString(&rawtime));
        fclose(outfile);
      return 0;
    }
    
    char *mlTimeString(time_t* t)
    {
         int k, k1;
         char* retstring = malloc(30);
         if(retstring == NULL) return NULL;
         for(k = 0; k < 30; k++)
                 retstring[k]='\0';
         char weekdays[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 
         char months[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
         struct tm* readabletime;
         readabletime = localtime(t);
         int x = (*readabletime).tm_wday;
         for(k = 0; weekdays[x][k] != '\0'; k++)
               retstring[k] = weekdays[x][k];
         k1 = k;
         retstring[k1]=',';
         retstring[++k1]=' ';
         x = (*readabletime).tm_mon;
         k1++;
         for(k = 0; months[x][k] != '\0'; k++, k1++)
               retstring[k1] = months[x][k];
         return retstring;
    }
    The basic idea is that it find the date then mlTimeSTring returns a string with the date formatted in a more readable form. Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need a prototype for the function mlTimeString(), so add this near the top of your source file:

    Code:
    char *mlTimeString(time_t* t);
    ... reason being, with no prototype, the function's first use is assumed to return an int, which is not compatible with printf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Awesome, that seems to have cleared things up. You have my thanks. Can you explain to me why it thought that it was an integer?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... reason being, with no prototype, the function's first use is assumed to return an int. This is a "standard" default, if the compiler doesn't get told what return type a function has, it will assume it's an int.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Awesome Hammer, you rock.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Concatenation of strings as char pointers
    By sameerc in forum C Programming
    Replies: 11
    Last Post: 05-10-2005, 04:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM