473,655 Members | 3,057 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 1511
&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
3962
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
9623
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
2316
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
2772
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
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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
7310
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
4150
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
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.