473,700 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Learning C, Trying to Understand & (unary op)

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

Nov 15 '05 #1
10 1516
&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.c om> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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

Nov 15 '05 #2
Tom
Thanks! That put it in context, I get it now.

Regards,
Tom

Nov 15 '05 #3
Tom <te****@yahoo.c om> wrote:
I have the following code: ////////////////////////////////////////////////////////////
#include <linux/kernel.h>


comp.unix.progr ammer 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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #4
On Fri, 23 Sep 2005 14:06:50 +0000, Christopher Benson-Manica wrote:
comp.unix.progr ammer is that way --->

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


Try actually reading his post before jumping on your high horse, his
question had nothing to do with anything system-specific.

--
"Being a social outcast helps you stay concentrated on the really important
things, like thinking and hacking." - Eric S. Raymond

Nov 15 '05 #5
# ////////////////////////////////////////////////////////////
# #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.

'Assign' means something else in C. The variable is declared. It will
be allocated space in the compiled code.

# 2) ???

Passes a pointer to the allocated space to the function sysinfo.
Presumably it is storing into the space pointed to by the
passed pointer.

Originally C functions could not return structs, so you would
have to allocate space for struct and then pass a pointer to
a function to fill in the space. C now allows structs to be
returned, but the old interfaces persist.

# 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?

A new instance of s is created automatically as the main
function is enterred. s refers to this same instance throughout
that function invocation.

C does not hide memory allocation; and the usual C library
does not hide deallocation. The programmer has to handle these
issues.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Nov 15 '05 #6
Tom wrote on 23/09/05 :
I've recently started trying to learn C (for the fun of it!). <...>

I have the following code:

////////////////////////////////////////////////////////////
#include <linux/kernel.h>
Wait a minute. You claim to be an absolute beginner in C and you want
to play with the Linux kernel ? Don't you think that you are a little
green for that ?
int main()
{
struct sysinfo s;
sysinfo(&s);

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


Huh, more that green. If you are writing a Linux daemon of a module
(main() in a module ?), you want printk() here, but it is absolutely
off-topic on c.l.c.

Please stick to standard C.

- it's on-topic here
- it's a good way to learn C (whatever the system you are using).
- it will prevent you to do things you are not allowed to do on a
managed system like wIndows NT/XP or Linux.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #7

Emmanuel Delahaye wrote:
Tom wrote on 23/09/05 :
I've recently started trying to learn C (for the fun of it!). <...>

I have the following code:

////////////////////////////////////////////////////////////
#include <linux/kernel.h>
Wait a minute. You claim to be an absolute beginner in C and you want
to play with the Linux kernel ? Don't you think that you are a little
green for that ?
int main()
{
struct sysinfo s;
sysinfo(&s);

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


Huh, more that green. If you are writing a Linux daemon of a module
(main() in a module ?), you want printk() here, but it is absolutely
off-topic on c.l.c.

Please stick to standard C.

- it's on-topic here
- it's a good way to learn C (whatever the system you are using).
- it will prevent you to do things you are not allowed to do on a
managed system like wIndows NT/XP or Linux.


His actual question (which you snipped) is topical.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

.sig under repair


Nov 15 '05 #8
Emmanuel Delahaye wrote:

Tom wrote on 23/09/05 :
I've recently started trying to learn C (for the fun of it!). <...>

I have the following code:

////////////////////////////////////////////////////////////
#include <linux/kernel.h>
Wait a minute. You claim to be an absolute beginner in C and you want
to play with the Linux kernel ? Don't you think that you are a little
green for that ?


I see nothing in his post that he's doing anything like playing with
the kernel. He's simply including a system-specific header file
named "linux/kernel.h". (And the header file is not really related
to his question, beyond including a complete compilable [on Linux]
example of his question.)
int main()
{
struct sysinfo s;
sysinfo(&s);

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


Huh, more that green. If you are writing a Linux daemon of a module
(main() in a module ?), you want printk() here, but it is absolutely
off-topic on c.l.c.


I saw no indication anywhere in his post that he was writing anything
related to a kernel module. Rather, this is a 3-line program which
prints some information obtained via a system-specific "sysinfo()"
function call.
Please stick to standard C.

- it's on-topic here
- it's a good way to learn C (whatever the system you are using).
- it will prevent you to do things you are not allowed to do on a
managed system like wIndows NT/XP or Linux.


His actual question (what does "&s" as a parameter mean?) was on
topic. It was simply wrapped in non-portable example code.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #9

Tom wrote:
[snip]
What is "sysinfo(%s );"
sysinfo(&s)
actually doing?


The sysinfo function is modifying the contents of the s variable.

The expression &s evaluates to the address of s. C passes all function
parameters by value, so when a function writes to a parameter, that
change is not reflected in the calling function. For example, the
following code doesn't work as intended:

void swap(int x, int y)
{
int tmp;
tmp = x;
x = y;
y = tmp;
}

int main(void)
{
int foo = 1, bar = 10;
swap(foo, bar);
printf("foo = %d, bar = %d\n", foo, bar);
return 0;
}

The formal parameters x and y are different objects from foo and bar;
they occupy different memory addresses. The values of foo and bar are
copied into x and y respectively when swap is called, but the new
values of x and y are not written back to foo and bar when swap
returns.

In order to get around this, we use pointers to pass the addresses of
foo and bar to swap, which swap dereferences:

void swap (int *x, int *y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}

int main(void)
{
int foo = 1, bar = 10;
swap(&foo, &bar);
printf("foo = %d, bar = %d\n", foo, bar);
return 0;
}

x and y are still distinct variables with their own addresses in
memory, but instead of copying the values of foo and bar to them, we're
copying the *addresses* of foo and bar to them.

The declaration

int *x;

specifies that x is a pointer to int; that is, that the value of x is
the address of an object of int type in memory. The statement

*x = *y;

takes the value of the object pointed to by y (bar) and assigns it to
the object pointed to by x (foo).

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3974
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
1
9636
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
6
2319
by: Angel Tsankov | last post by:
How can an overloaded operator& take the address of its argument: template<typename T> Smth operator &(T& SomeObject) { // The address of SomeObject is needed here }
10
2775
by: Zach | last post by:
I am looking for a comprehensive list of errors in Kernighan & Ritchie's "The C Programming Lanuage: 2nd Edition (ANSI C)" Also what is the best way to work through the list with the Errata? I don't want to constantly refer back to the Errata since that may slow down my learning but I also don't want to learn something that isn't 100% accurate. Zach
0
8725
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8644
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9074
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8924
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7807
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4656
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.