学习C,试着理解& (一元行动) [英] Learning C, Trying to Understand & (unary op)

查看:71
本文介绍了学习C,试着理解& (一元行动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我最近开始尝试学习C(为了它的乐趣!)。我有多年来一直使用Perl和其他语言的
,但是它们总是非常高的水平,这给了我很大的帮助。这是我的

情况:


我有以下代码:


/////// ////////////////////////////////////////////////// ///

#include< linux / kernel.h>

int main()

{

struct sysinfo s;

sysinfo(& s);


printf(服务器正常运行时间:%i天。,s.uptime / 60 / 60/24);

返回0;

}

//////////////// ////////////////////////////////////////////
/>
这样可以正常工作并提供服务器的正常运行时间。但是,我想要确切地了解它在做什么。这是我的理解变得模糊的地方:


1)分配s作为sysinfo结构的一个实例。

2)???

3)打印并计算服务器正常运行时间。


什么是sysinfo(%s);实际上呢?我有限的理解是

,它正在运行针对

" s"的地址的sysinfo系统调用。变量。所以,s是sysinfo结构的新实例和

然后sysinfo

系统调用处理该新实例的地址?


我只是想通过我厚厚的头脑来获得这个概念。任何

启示将不胜感激!谢谢。


Tom

Hello,

I''ve recently started trying to learn C (for the fun of it!). I have
been using Perl and other languages for many years, but they were
always very high level, this is giving me quite a ride. Here is my
situation:

I have the following code:

////////////////////////////////////////////////////////////
#include <linux/kernel.h>
int main()
{
struct sysinfo s;
sysinfo(&s);

printf("Server uptime: %i days.", s.uptime/60/60/24);
return 0;
}
////////////////////////////////////////////////////////////

This works fine and provides the uptime of a server. However, I''m
trying to understand exactly what it is doing. Here is where my
understanding gets fuzzy:

1) Assign "s" as an instance to the sysinfo structure.
2) ???
3) Print and calculate server uptime.

What is "sysinfo(%s);" actually doing? My limited understanding is
that it is running the sysinfo system call against the address of the
"s" variable. So, "s" is a new instance of the sysinfo structure and
then the address of that new instance is processed by the sysinfo
system call?

I''m just trying to get this concept through my thick head. Any
enlightenment would be appreciated! Thanks.

Tom

推荐答案

& s评估s的内存地址 - 该地址被传递给

函数;实现传递引用调用。 C只有值传递 -

但是这里的值可以用来通过取消引用运算符来访问被调用函数中的s。

>
汤姆 < TE **** @ yahoo.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
&s evaluates to the memory address of s - that address is passed to the
function; to effect a pass-by-reference call. C only has pass by value -
but here that value can be used to access s within the called function via
the dereference operator *.

"Tom" <te****@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
你好,

我最近开始尝试学习C(为了它的乐趣!)。我已经使用Perl和其他语言多年了,但它们总是非常高,这给了我很多的机会。这是我的情况:

我有以下代码:

/////////////////// /////////////////////////////////////////
#include< linux /kernel.h>
int main()
结构sysinfo s;
sysinfo(& s);

printf(" Server)正常运行时间:%i天。,s.uptime / 60/60/24);
返回0;
}
////////////// //////////////////////////////////////////////

这很好用,可以提供服务器的正常运行时间。但是,我正在试图准确理解它在做什么。这是我理解模糊的地方:

1)分配s作为sysinfo结构的一个实例。
2)???
3)打印并计算服务器的正常运行时间。

什么是sysinfo(%s);实际上呢?我有限的理解是它正在对着
s的地址运行sysinfo系统调用。变量。所以,s是sysinfo结构的新实例,那么sysinfo
系统调用会处理新实例的地址吗?

我只想尝试这个概念透过我厚厚的脑袋。任何
启示将不胜感激!谢谢。

Tom
Hello,

I''ve recently started trying to learn C (for the fun of it!). I have
been using Perl and other languages for many years, but they were
always very high level, this is giving me quite a ride. Here is my
situation:

I have the following code:

////////////////////////////////////////////////////////////
#include <linux/kernel.h>
int main()
{
struct sysinfo s;
sysinfo(&s);

printf("Server uptime: %i days.", s.uptime/60/60/24);
return 0;
}
////////////////////////////////////////////////////////////

This works fine and provides the uptime of a server. However, I''m
trying to understand exactly what it is doing. Here is where my
understanding gets fuzzy:

1) Assign "s" as an instance to the sysinfo structure.
2) ???
3) Print and calculate server uptime.

What is "sysinfo(%s);" actually doing? My limited understanding is
that it is running the sysinfo system call against the address of the
"s" variable. So, "s" is a new instance of the sysinfo structure and
then the address of that new instance is processed by the sysinfo
system call?

I''m just trying to get this concept through my thick head. Any
enlightenment would be appreciated! Thanks.

Tom



谢谢!把它放在上下文中,我现在就明白了。


问候,

汤姆

Thanks! That put it in context, I get it now.

Regards,
Tom


Tom< te **** @ yahoo.com>写道:
Tom <te****@yahoo.com> wrote:
我有以下代码:
////////////////////////// //////////////////////////////////
#include< linux / kernel.h>
I have the following code: ////////////////////////////////////////////////////////////
#include <linux/kernel.h>




comp.unix.programmer就是这样--->


这里只说ANSI / ISO C.系统特定的扩展名在其他地方讨论。

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



comp.unix.programmer is that way --->

Only ANSI/ISO C is spoken here. System-specific extensions are
discussed elsewhere.

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于学习C,试着理解&amp; (一元行动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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