奇怪的指针在C,请帮助。代码供应...... [英] Weird Pointer In C, Please Help. Code supply...

查看:41
本文介绍了奇怪的指针在C,请帮助。代码供应......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//工作示例:


#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中编写程序

时,我对指针更加困惑。谢谢。

解决方案


MQ*****@gmail.com 写道:


//工作示例:


#include< stdio.h>

#include< time.h>


int main()

{

time_t rawtime; / *将rawtime定义为time_t * /



出于好奇,你为什么要发表评论?这很明显来自

代码本身。


time(& rawtime);

printf(" ;当前日期和时间是:%s",ctime(& rawtime)); / *致电

ctime使用& rawtime * /



再次,明显的评论。更好的评论会很快解释

ctime的作用。


返回0;

}


//不工作示例:


#include< stdio.h>

#include< time.h>


int main()

{

time_t * rawtime; / *将rawtime定义为指针指向time_t * /


time(rawtime);



您正在发送一个NULL指针;没有分配的内存你指的是
。参见malloc()。


printf("当前日期和时间是:%s,ctime(rawtime)); / *致电

ctime使用rawtime * /


返回0;

}


它在非工作的例子中非常奇怪。他们都是指针。



我没有看到使用指针和分配使用

直接变量的任何优势。我推荐使用第一个例子。


这里有更多例子。

char t_time;

read(fd, & t_time,25); / *工作* /


char * t_time;

read(fd,t_time,25); / *不工作* /



再次,你没有在第二个例子中分配内存。


有人帮忙。当我用C编写程序

时,我对指针更加困惑谢谢。



在C中,你必须手动分配和释放存储单元。请参阅malloc()

和free()。​​


文章< 11 ********* ************@m73g2000cwd.googlegroups。 com>,

Chris Johnson< ef ****** @ gmail.comwrote:


> MQ **** *@gmail.com写道:


> //不工作示例:


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


> int main()
{
time_t * rawtime ; / *将rawtime定义为指针指向time_t * /


>时间(原始时间);


>您正在发送一个NULL指针;没有分配的内存你指向。请参阅malloc()。



你没有分配内存是正确的,但是发送的

指针可能是任何东西,因为''auto''变量

不会自动初始化为任何东西。

要强调原始海报:


time_t * rawtime;只声明空间来保持指针本身,

并且没有分配内存来保存任何指向的东西。


你可以,例如,说,


time_t rawtime;

time_t * rawtimeptr =& rawtime;

time(rawtimeptr);


指针可用于指向现有

对象的开头,或指向现有对象的不同部分

(除了位域)或者指针可以用来保存地址

使用malloc()或calloc()分配的内存。


指针也可以赋值为NULL ,承诺不指向任何

对象。


指针也可以合法设置为立即指向

对象的结尾,前提是没有尝试访问该位置的
内存。

-

原型是超类型他们的克隆。 - maplesoft




Chris Johnson写道:

MQ ***** @ gmail.com 写道:


//工作示例:


#include< stdio.h>

#include< time.h>


int main()

{

time_t rawtime; / *将rawtime定义为time_t * /



出于好奇,你为什么要发表评论?这很明显来自

代码本身。


time(& rawtime);

printf(" ;当前日期和时间是:%s",ctime(& rawtime)); / *致电

ctime使用& rawtime * /



再次,明显的评论。更好的评论会很快解释

ctime的作用。


返回0;

}


//不工作示例:


#include< stdio.h>

#include< time.h>


int main()

{

time_t * rawtime; / *将rawtime定义为指针指向time_t * /


time(rawtime);



您正在发送一个NULL指针;没有分配的内存你指的是
。参见malloc()。


printf("当前日期和时间是:%s,ctime(rawtime)); / *致电

ctime使用rawtime * /


返回0;

}


它在非工作的例子中非常奇怪。他们都是指针。



我没有看到使用指针和分配使用

直接变量的任何优势。我推荐使用第一个例子。


这里有更多例子。

char t_time;

read(fd, & t_time,25); / *工作* /


char * t_time;

read(fd,t_time,25); / *不工作* /



再次,你没有在第二个例子中分配内存。


有人帮忙。当我用C编写程序

时,我对指针更加困惑谢谢。



在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.

解决方案


MQ*****@gmail.com wrote:

//Working Example:

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

int main ()
{
time_t rawtime; /* define rawtime as time_t */

Out of curiosity, why are you making this comment? This is obvious from
the code itself.

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/

Again, obvious comment. A better comment would quickly explain what
ctime does.

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 );

You are sending a NULL pointer; there is no allocated memory you''re
pointing to. See malloc().

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.

I don''t see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.

More example here.
char t_time;
read(fd, &t_time, 25); /* working */

char *t_time;
read(fd, t_time, 25); /* Not working */

Again, you''re not allocating memory in the second example.

Somebody help. i get more confuse on the pointer when i write program
in C. thank you.

In C, you have to allocate and free memory cells by hand. See malloc()
and free().


In article <11*********************@m73g2000cwd.googlegroups. com>,
Chris Johnson <ef******@gmail.comwrote:

>MQ*****@gmail.com wrote:

>//Not Working Example:

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

>int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */

> time ( rawtime );

>You are sending a NULL pointer; there is no allocated memory you''re
pointing to. See malloc().

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



Chris Johnson wrote:

MQ*****@gmail.com wrote:

//Working Example:

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

int main ()
{
time_t rawtime; /* define rawtime as time_t */


Out of curiosity, why are you making this comment? This is obvious from
the code itself.

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/


Again, obvious comment. A better comment would quickly explain what
ctime does.

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 );


You are sending a NULL pointer; there is no allocated memory you''re
pointing to. See malloc().

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.


I don''t see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.

More example here.
char t_time;
read(fd, &t_time, 25); /* working */

char *t_time;
read(fd, t_time, 25); /* Not working */


Again, you''re not allocating memory in the second example.

Somebody help. i get more confuse on the pointer when i write program
in C. thank you.


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,请帮助。代码供应......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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