我正在开发一个功能,该功能应提示用户输入任意时间和日期。 我想将这些值存储在 struct tm 中,但它无法正常工作:
struct tm * enter_time_GMT(){
struct tm *TIME;
char input[50];
int check=0, buffer;
printf("date:\n");
do{
printf("day > ");
scanf("%49s",&input[0]);
buffer=atoi(input);
if(buffer>=1 && buffer<=31){
check=1;
/* program crashes here, compiler says TIME uninitialized: */
TIME->tm_mday=buffer;
}
else{
check=0;
printf("wrong input\n");
}
}while(check==0);
/* just a short part of the full function */
return TIME;
}
我正在使用这样的函数:
int main(){
struct tm *sysTIME; /* why does the compiler want me to use tm* instead of tm? */
char buffer[80];
sysTIME=enter_time_GMT();
strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", sysTIME);
printf("%s\n", buffer);
return 0;
}
令我惊讶的是,我可能会使用类似的东西
TIME->tm_year=12;
在 main() 中工作,但不在我的函数中工作。那么区别在哪里,struct tm 和其他结构体有什么区别呢?
最佳答案
当编译器提示 TIME
未初始化时,这是正确的。您的指针TIME
没有指向任何有意义的地方,访问它可能会使程序崩溃。
从您的评论中我发现您还不熟悉指针。在这种情况下,您可以直接使用struct tm
。这样您就不必担心内存管理,因为struct
是按值传递的。
如果要使用指针,则必须使指针指向有效内存。实现此目的的一种方法是使用 malloc
或 calloc
在堆上分配内存。然后,您就可以从函数外部访问该内存,但是当您不再需要它时,您应该释放
它。
下面的示例程序使用了两种方法:xmas
使用指针,newyear
使用普通的 struct tm
:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct tm *xmas(int year)
{
struct tm *t = calloc(1, sizeof(*t)); // must be free'd after use
t->tm_mday = 24;
t->tm_mon = 11;
t->tm_year = year - 1900;
return t; // pointers to memory on the heap can be safely returned
}
struct tm newyear(int year)
{
struct tm t = {0};
t.tm_mday = 1;
t.tm_mon = 0;
t.tm_year = year - 1900;
return t; // structure is returned "by value"
}
int main()
{
struct tm *x = xmas(2014);
struct tm ny = newyear(2015);
char buf[30];
strftime(buf, sizeof(buf), "%D", x);
puts(buf);
strftime(buf, sizeof(buf), "%D", &ny);
puts(buf);
// Free ressources of allocated memory
free(x);
return 0;
}
在您牢牢掌握指针(其中不仅仅包括堆上的内存分配)之前,使用普通结构可能会更容易。
关于改变函数中 struct tm 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565218/