问题描述
//工作示例:
#include< stdio.h>
#include< time.h>
int main()
{
time_t rawtime; / *将rawtime定义为time_t * /
time(& rawtime);
printf("当前日期和时间为:%s",ctime (& rawtime)); / *致电
ctime使用& rawtime * /
返回0;
}
//不工作示例:
#include< stdio.h>
#include< time.h>
int main()
{
time_t * rawtime; / *将rawtime定义为指针指向time_t * /
time(rawtime);
printf("当前日期和时间为:%s", ctime(rawtime)); / *致电
ctime使用rawtime * /
返回0;
}
它在非工作的例子中非常奇怪。它们都是指针。
这里有更多例子。
char t_time;
read(fd,& t_time,25) ; / *工作* /
char * t_time;
read(fd,t_time,25); / *不工作* /
有人帮忙。当我在C中编写程序
时,我对指针更加困惑。谢谢。
出于好奇,你为什么要发表评论?这很明显来自
代码本身。
再次,明显的评论。更好的评论会很快解释
ctime的作用。
您正在发送一个NULL指针;没有分配的内存你指的是
。参见malloc()。
我没有看到使用指针和分配使用
直接变量的任何优势。我推荐使用第一个例子。
再次,你没有在第二个例子中分配内存。
在C中,你必须手动分配和释放存储单元。请参阅malloc()
和free()。
你没有分配内存是正确的,但是发送的
指针可能是任何东西,因为''auto''变量
不会自动初始化为任何东西。
要强调原始海报:
time_t * rawtime;只声明空间来保持指针本身,
并且没有分配内存来保存任何指向的东西。
你可以,例如,说,
time_t rawtime;
time_t * rawtimeptr =& rawtime;
time(rawtimeptr);
指针可用于指向现有
对象的开头,或指向现有对象的不同部分
(除了位域)或者指针可以用来保存地址
使用malloc()或calloc()分配的内存。
指针也可以赋值为NULL ,承诺不指向任何
对象。
指针也可以合法设置为立即指向
对象的结尾,前提是没有尝试访问该位置的
内存。
-
原型是超类型他们的克隆。 - maplesoft
出于好奇,你为什么要发表评论?这很明显来自
代码本身。
再次,明显的评论。更好的评论会很快解释
ctime的作用。
您正在发送一个NULL指针;没有分配的内存你指的是
。参见malloc()。
我没有看到使用指针和分配使用
直接变量的任何优势。我推荐使用第一个例子。
再次,你没有在第二个例子中分配内存。
在C中,你必须手动分配和释放存储单元。请参阅malloc()
和free()。
所以,当我声明指针时,它直到我打电话给
malloc()才真正存在。非常感谢克里斯。我有很多要学习的东西:)
//Working Example:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime; /* define rawtime as time_t */
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/
return 0;
}
//Not Working Example:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */
time ( rawtime );
printf ( "Current date and time are: %s", ctime (rawtime) ); /* call
ctime use rawtime */
return 0;
}
it''s very weird in the not working example. they are all pointer.
More example here.
char t_time;
read(fd, &t_time, 25); /* working */
char *t_time;
read(fd, t_time, 25); /* Not working */
Somebody help. i get more confuse on the pointer when i write program
in C. thank you.
Out of curiosity, why are you making this comment? This is obvious from
the code itself.
Again, obvious comment. A better comment would quickly explain what
ctime does.
You are sending a NULL pointer; there is no allocated memory you''re
pointing to. See malloc().
I don''t see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.
Again, you''re not allocating memory in the second example.
In C, you have to allocate and free memory cells by hand. See malloc()
and free().
You are correct about there being no allocated memory, but the
pointer being sent in could be anything, because ''auto'' variables
are not automatically initialized to anything.
To emphasize to the original poster:
time_t *rawtime; only declares space to hold the pointer itself,
and does not allocate memory to hold anything pointed to.
You could, for example, say,
time_t rawtime;
time_t *rawtimeptr = &rawtime;
time( rawtimeptr );
A pointer can be used to point the beginning of an existing
object, or to point into a distinct part of an existing object
(except a bitfield), or a pointer can be used to hold the address
of memory allocated using malloc() or calloc().
A pointer can also be assigned NULL, which is promised not to point to any
object.
A pointer may also legally be set to point immediately -after- the
end of an object, provided that there is no attempt to access
memory at that location.
--
Prototypes are supertypes of their clones. -- maplesoft
Out of curiosity, why are you making this comment? This is obvious from
the code itself.
Again, obvious comment. A better comment would quickly explain what
ctime does.
You are sending a NULL pointer; there is no allocated memory you''re
pointing to. See malloc().
I don''t see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.
Again, you''re not allocating memory in the second example.
In C, you have to allocate and free memory cells by hand. See malloc()
and free().
So, when i declare the pointer, it dosen''t really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)
这篇关于奇怪的指针在C,请帮助。代码供应......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!