救命! - 时间功能问题?? [英] HELP! - time function problem??

查看:46
本文介绍了救命! - 时间功能问题??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个作为测试:

#include< time.h>

main(){

printf(X1: %s \ n",putim());

printf(X2:%s \ n",putim());

}

putim(){

time_t t;

time(& t);

return(ctime(& t)) ;

}

它的工作原理并给我时间和日期,就像日期一样。命令
linux中的


当我在数据收集程序中使用它时,我也使用睡眠命令

(不包含上述任何内容) )我在/ b
返回后得到分段错误。


不可否认,两种情况下的编译都会产生警告

返回从没有强制转换的指针生成整数。

由于这不影响上面的测试,为什么它或使用睡眠

函数杀了我一个seg fault ?????


任何建议都会有所帮助

Dan

I created this as a test:
#include <time.h>
main(){
printf(X1: %s\n", putim());
printf(X2: %s\n", putim());
}
putim() {
time_t t;
time(&t);
return(ctime(&t));
}
It works and gives me the time of day and date just like "date" command
in linux.
When I use this in a data collection program where I also use sleep commands
(not encompassing any of the above) I get a segmentation fault at/after
the return.

Admittedly, the compile in both cases produces a warning
"return makes integer from pointer without a cast."
Since this is not effecting the test above why is it or the use of sleep
functions killing me with a seg fault?????

Any suggestions would be helpful
Dan

推荐答案

Razzel写道:

我创建了这个测试:
#include< time.h>
main(){
printf(X1:%s \ n",putim());
printf(X2:%s \ n",putim());
}
putim(){
time_t t;
时间(& t);
返回(ctime(& t));
}
它有效

I created this as a test:
#include <time.h>
main(){
printf(X1: %s\n", putim());
printf(X2: %s\n", putim());
}
putim() {
time_t t;
time(&t);
return(ctime(&t));
}
It works




它没有甚至编译。

没有人关心你的代码出现的模糊印象



使用复制和粘贴工具!


修好一切明显错误的东西后,

然后就没有错了。

#include< time.h>

#include< stdio.h>


char * putim(void);


int main(无效)

{

printf(" X1:%s \ n",putim());

printf(" X2:%s \ n",putim());

返回0;

}


char * putim(无效)

{

time_t t;


time(& t);

返回ctime(& t);

}

-

pete



It doesn''t even compile.
Nobody cares about the vague impressions
in your memory of the appearence of your code.
Use copy and paste facilities!

After you fix up everything that''s obviously wrong with it,
then there''s nothing wrong with it.

#include <time.h>
#include <stdio.h>

char *putim(void);

int main(void)
{
printf("X1: %s\n", putim());
printf("X2: %s\n", putim());
return 0;
}

char *putim(void)
{
time_t t;

time(&t);
return ctime(&t);
}
--
pete


2005年6月9日星期四10:09:40 +0000,Razzel写道:
On Thu, 09 Jun 2005 10:09:40 +0000, Razzel wrote:
我创建了这个作为测试:
#include< time.h>
main(){


int main(void){

printf(X1:%s \ n",putim());


printf?那不是时间。...

你也错过了开场报价和某种形式的缩进。

printf(X2:%s \ n,putim());


返回0;

}
putim(){


char * putim(void) {


您应该在主要功能之前放置一个原型,这将是

看起来像这样:

char * putim (无效);

time_t t;
时间(& t);


哎呀,你没有检查成功时间的返回值。

return(ctime(& t));


请注意,ctime将在失败时返回NULL,你应该在某个地方检查



}
它工作


它甚至不编译。

并给我时间和日期的时间就像日期一样。命令
在linux中。
当我在数据收集程序中使用它时,我也使用睡眠命令
(不包含上述任何内容)我在返回。
不可否认,两种情况下的编译都会产生警告
返回使得没有强制转换的指针整数。


您正在从putim返回一个char *,但是您没有将它放在您的

函数定义中,因此编译器假定它将返回一个int。由于

你的返回值是一个指针,而不是一个int,你会得到一个警告。

因为这不影响上面的测试为什么它或睡眠的使用
函数用seg故障杀死我?????
I created this as a test:
#include <time.h>
main(){
int main (void){
printf(X1: %s\n", putim());
printf? That''s not in time.h...
You are also missing the opening quote, and some form of indentation.
printf(X2: %s\n", putim());
return 0;
}
putim() {
char * putim (void) {

You should put a prototype before the main function as well, that would
look like this:
char * putim (void);
time_t t;
time(&t);
Whoops, you didn''t check the return value of time for success.
return(ctime(&t));
Be aware that ctime will return NULL on failure, you should be checking
for this somewhere.
}
It works
It doesn''t even compile.
and gives me the time of day and date just like "date" command
in linux.
When I use this in a data collection program where I also use sleep commands
(not encompassing any of the above) I get a segmentation fault at/after
the return.
Admittedly, the compile in both cases produces a warning
"return makes integer from pointer without a cast."
You are returning a char * from putim but you do not put this in your
function definition so the compiler assumes it will return an int. Since
your return value is a pointer, not an int, you get a warning.
Since this is not effecting the test above why is it or the use of sleep
functions killing me with a seg fault?????




修复你的代码,如果你还有问题,发布*最小*

*完整* *可编译*演示此问题的示例。请

确保你先解决我提到的物品并复制并粘贴你的

代码,而不是手工复制。


Robert Gamble



Fix your code, if you still have a problem, post the *smallest*
*complete* *compilable* example that demonstrates the problem. Please
make sure you address the items I mentioned first and copy and paste your
code instead of copying it by hand.

Robert Gamble


2005年6月9日星期四07:21:28 -0400,Robert Gamble写道:


.. ..
On Thu, 09 Jun 2005 07:21:28 -0400, Robert Gamble wrote:

....
return(ctime(& t));
return(ctime(&t));



请注意ctime将返回失败时为NULL,你应该在某处检查



Be aware that ctime will return NULL on failure, you should be checking
for this somewhere.




在ctime()和asctime()的标准C规范中/>
返回值是基于的,返回值是指向字符串的指针。 null

指针不是允许的返回值。


劳伦斯



In the standard C specification of ctime() and asctime() on which its
return value is based, the return value is a pointer to a string. A null
pointer is not a permissible return value.

Lawrence


这篇关于救命! - 时间功能问题??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆