Help with my program

Hello, my program dont behave; If this program were in C extension, it would work quietly, but in C ++ extension it does not work, why?
Look my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <time.h>

// funcao format. Ela recebe um valor do tipo inteiro e caso este seja menor que 10
// ela inclui o 0 na frente. ex.: 1 antes 01 depois
// for fim retorna um ponteiro para uma variavel do tipo char com o valor da conversao
char *format(int number){
   char *retorno;
   char  ret[100];
   int   i;

   if (number < 10){
      sprintf(ret,"0%d",number);
      retorno = ret;
      return retorno;
   }
   else{
      sprintf(ret,"%d",number);
      retorno = ret;
      return retorno;
   }
}

// funcao que retorna data
char *data(void){

   int dia,mes,ano;
   char   var1[100],
      var2[100],
      var3[100],
      var4[100],
      *dataPtr;

   struct tm *local;
   time_t t;

   t = time(NULL);
   local = localtime(&t);

   dia = local -> tm_mday;
   mes = local -> tm_mon + 1;
   ano = local -> tm_year + 1900;

   // por algum motivo precisa converter os valores retornados pelos ponteiros
   // da funcao em variaveis do tipo char
   sprintf(var1,"%s",format(dia));
   sprintf(var2,"%s",format(mes));
   sprintf(var3,"%s",format(ano));

   // cria a variavel de retorno dos dados e cria um ponteiro para essa variavel
   sprintf(var4,"%s/%s/%s",var1,var2,var3);

   // retorna data no formato dd:MM:yyyy com um ponteiro
   dataPtr = var4;
   return dataPtr;
}

// funcao que retorna hora

char *hora(void){

   int   hora,minuto,segundo;
   char   var1[100],
      var2[100],
      var3[100],
      var5[100],
      *retorno;
   struct tm *local;
   time_t t;

   t = time(NULL);
   local = localtime(&t);

   // obtem hora, minuto e segundo e os aloca em uma variavel do tipo inteiro
   hora   =   local -> tm_hour;
   minuto   =    local -> tm_min;
   segundo =   local -> tm_sec;

   // por algum motivo precisa converter os valores retornados pelos ponteiros
   // da funcao em variaveis do tipo char
   sprintf(var1,"%s",format(hora));
   sprintf(var2,"%s",format(minuto));
   sprintf(var3,"%s",format(segundo));

   // cria a variavel de retorno dos dados e cria um ponteiro para essa variavel
   sprintf(var5,"%s:%s:%s",var1,var2,var3);

   // retorna hora no formato hh:mm:ss com um ponteiro
   retorno = var5;
   return retorno;
}

int main()
{
   char data_sistema[100];
   char  hora_sistema[100];

   sprintf(data_sistema,"%s",data());
   sprintf(hora_sistema,"%s",hora());

   printf("%s\n", data_sistema);
   printf("%s\n",hora_sistema);

}
It looks like you went to a lot of trouble in order to suppress the compiler messages which tell you what is wrong.

The functions are returning a pointer to a local variable. The variable is destroyed when the function ends, so the pointer is no longer pointing to a valid location.

It might happen to sometimes seem to give a valid result, but nevertheless the code is not safe.

http://stackoverflow.com/questions/4570366/pointer-to-local-variable



By the way, did you consider the use of strftime()?

http://www.cplusplus.com/reference/ctime/strftime/
Topic archived. No new replies allowed.