Solomander
Solomander

Reputation: 41

Struct tm time inside of another struct

As part of my assignment, I have been asked to construct a structure of command, which stores a list of executed commands. The instructions provided were:

typedef struct {
char *name;
struct tm time;
int code;
} Command;
Note: struct tm defined in <time.h>

I am having trouble accessing the time values stored in the struct tm time, which is stored inside of my Command structure. My code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h> 
#include <string.h>
#include <time.h>

typedef struct command{
    char* name;
    struct tm *time;
    int code;
} Command;

void add_log(Command command[], int return_code, char name[100]){
    
    time_t current_time;
    time(&current_time);

    command[0].code = return_code;
    command[0].name = strdup(name);
    command[0].time = localtime(&current_time);
}

int main(){

    Command command[10];

    add_log(command, 0, "log");

    printf("Printing return code\n%d\n", command[0].code);
    printf("Printing command name\n%s\n", command[0].name);

    char time_log[10];

    if (strftime(time_log, 10, "%c", time)){
        printf("\nPrinting time log\n");
        puts(time_log);
    }
    else{
        printf("FAILED");
    }

    return 0;
}

The error I am getting looks as follows:

tester.c: In function ‘main’:
tester.c:37:38: warning: passing argument 4 of ‘strftime’ from incompatible pointer type [-Wincompatible-pointer-types]
   37 |     if (strftime(time_log, 10, "%c", time)){
      |                                      ^~~~
      |                                      |
      |                                      time_t (*)(time_t *) {aka long int (*)(long int *)}
In file included from tester.c:8:
/usr/include/time.h:90:32: note: expected ‘const struct tm * restrict’ but argument is of type ‘time_t (*)(time_t *)’ {aka ‘long int (*)(long int *)’}
   90 |    const struct tm *__restrict __tp) __THROW;
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
Printing return code
0
Printing command name
log
FAILED

I am specifically focusing on the line note: expected ‘const struct tm * restrict’ but argument is of type ‘time_t (*)(time_t *)’ {aka ‘long int (*)(long int *)’}. If the fourth argument for the strftime is wrong in my case, could anyone please point out what the correct argument should be?

Upvotes: 2

Views: 424

Answers (2)

chux
chux

Reputation: 153417

At least this problem:

OP's code command[0].time = localtime(&current_time); attempts

// Pseudo code
struct tm = *struct tm

Instead

struct tm * lt = localtime(&current_time);
if (lt != NULL) {
  command[0].time = *lt;
} else {
  // Handle conversion error;
}

Upvotes: 0

Jackson
Jackson

Reputation: 1223

Problem is here:

strftime(time_log, 10, "%c", time)

You need to access the time object in command so you should be doing

strftime(time_log, 10, "%c", command->time)

Upvotes: 1

Related Questions