473,890 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

P 155 k&r section 7.3

mdh
Hi All,
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontai ns a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
.......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

thanks in advance.

Sep 5 '08 #1
45 2033
mdh <md**@comcast.n etwrites:
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontai ns a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.
The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.

For example, va_arg takes two arguments, an expression of type va_list
(I'm actually not sure it can be an arbitrary expression) and a type
name. It yields a result of the named type. A C function cannot take
a type name as an argument, only an expression, and it must return a
result of some single type that's specified when the function is
declared.

Now va_arg can't *portably* be defined as a macro either -- but
there's always some way to define it non-portably. In some cases, it
might even be necessary for the compiler to provide some extension
just for the purpose, and have the va_arg macro use that extension;
for example, an implementation might have something like

#define va_arg(ap, type) __builtin_va_ar g__(ap, type)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 6 '08 #2
mdh wrote:
Hi All,
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontai ns a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.
You can't do this:

va_arg(arg, int)

with a function.

--
pete
Sep 6 '08 #3
mdh
On Sep 5, 5:20*pm, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.
"The standard header <stdarg.hcontai ns a set of macro definitions

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.

For example, va_arg takes two arguments, an expression of type va_list
(I'm actually not sure it can be an arbitrary expression) and a type
name. *It yields a result of the named type. *A C function cannot take
a type name as an argument, only an expression,


So, in the example that is given on p156,

void minprintf(char *fmt, ...){

va_list ap;

? This declares ap a pointer of type va_list? And the reason ap is a
pointer is simply because that is the way it is defined??
Then,

va_start(ap, fmt);

So, from what you say, if I understand this, the type here (fmt) is
"pointer to char" and the expression 'ap' is initialized or "returned"
as a pointer to char (to the first argument)?

And just to make sure, an example here of va_arg use.
ival is declared an int.

so the expression

ival=va_arg(ap, int) will return a value ( ival) that is of
whatever_type ....in this case int, but could be char *, char etc?
So, although each of these macros does something a little different,
the common thread seems to be that the type of the argument is
unknown, and these macros provide a mechanism to deal with it?

Sep 6 '08 #4
mdh <md**@comcast.n etwrites:
On Sep 5, 5:20*pm, Keith Thompson <ks...@mib.orgw rote:
>mdh <m...@comcast.n etwrites:
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.
"The standard header <stdarg.hcontai ns a set of macro definitions

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.

For example, va_arg takes two arguments, an expression of type va_list
(I'm actually not sure it can be an arbitrary expression) and a type
name. *It yields a result of the named type. *A C function cannot take
a type name as an argument, only an expression,


So, in the example that is given on p156,

void minprintf(char *fmt, ...){

va_list ap;

? This declares ap a pointer of type va_list? And the reason ap is a
pointer is simply because that is the way it is defined??
What makes you think va_list is a pointer type? The standard merely
says that it's "an object type suitable for holding information needed
by the macros va_start, va_arg, va_end, and va_copy". It could be a
pointer type, but it could just as easily be a structure or an array.
>
Then,

va_start(ap, fmt);

So, from what you say, if I understand this, the type here (fmt) is
"pointer to char" and the expression 'ap' is initialized or "returned"
as a pointer to char (to the first argument)?
va_start doesn't return anything. It initializes ap, an object of
type va_list. (That's part of the reason it's a macro; a function
can't modify an argument.) The va_list object exists to allow access
to the variadic arguments; you can think of it as a kind of abstract
index into the argument list.

The second argument to va_start is the name of the parameter just
before the ", ...".

The way in which this provides access to the following parameters is
entirely implementation-specific. If arguments are passed on a stack,
for example, a va_list object might be a pointer that gets advanced
through the region of memory containing the parameter objects. Or it
might be pure compiler magic.
And just to make sure, an example here of va_arg use.
ival is declared an int.

so the expression

ival=va_arg(ap, int) will return a value ( ival) that is of
whatever_type ....in this case int, but could be char *, char etc?
Right. Before calling va_arg, you have to already *know* what the
type of the next argument is going to be, and you have to know when to
stop. For something like printf, this is specified by the format
string. Or all the variadic arguments might be of the same pointer
type, with a null pointer marking the end of the list. Other schemes
are possible.

If you get the type wrong, for example if you call va_arg(ap, int)
when the next argument is actually of type char*, then you've just
entered the realm of undefined behavior.
So, although each of these macros does something a little different,
the common thread seems to be that the type of the argument is
unknown, and these macros provide a mechanism to deal with it?
Right.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 6 '08 #5
mdh
On Sep 5, 7:21*pm, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:

va_list ap;
? This declares ap a pointer of type va_list?

What makes you think va_list is a pointer type?
I was **deceived**!!! !

In K&R, va_list ap;

is followed by a comment

/*points to each unnamed arg in turn*/

so I assumed ap must be a pointer. :-)
*The standard merely
says that it's "an object type suitable for holding information needed
by the macros va_start, va_arg, va_end, and va_copy". *It could be a
pointer type, but it could just as easily be a structure or an array.
Then,
va_start(ap, fmt);
So, from what you say, if I understand this, the type here (fmt) is
"pointer to char" and the expression 'ap' is initialized or "returned"
as a pointer to char (to the first argument)?

va_start doesn't return anything. *It initializes ap, an object of
type va_list. *(That's part of the reason it's a macro; a function
can't modify an argument.)

The second argument to va_start is the name of the parameter just
before the ", ...".

The way in which this provides access to the following parameters is
entirely implementation-specific. *If arguments are passed on a stack,
for example, a va_list object might be a pointer that gets advanced
through the region of memory containing the parameter objects. *Or it
might be pure compiler magic.

thank you Keith.

Sep 6 '08 #6
mdh
On Sep 5, 5:20 pm, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:
The section is titled Variable-length Argument lists.

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.

May I pursue another issue a little further.

K&R (p 155) declare minprintf thus:

void minprintf(char *fmt, ...);
When I followed the example, it did not seem to matter to the output
whether the declaration was

void minprintf(char *fmt, ...);
or

void minprintf(int fmt, ...);
The only difference is ( maybe the **only** will raise some chuckles)
that char *fmt points to all the arguments in my debugger, but int
returns an integer( surprise, surprise!). For fear of flogging a dead
horse, is that the difference?

Sep 6 '08 #7
In article <3d************ *************** *******@b38g200 0prf.googlegrou ps.com>,
mdh <md**@comcast.n etwrote:
>On Sep 5, 5:20 pm, Keith Thompson <ks...@mib.orgw rote:
>mdh <m...@comcast.n etwrites:
The section is titled Variable-length Argument lists.

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.


May I pursue another issue a little further.

K&R (p 155) declare minprintf thus:

void minprintf(char *fmt, ...);
When I followed the example, it did not seem to matter to the output
whether the declaration was

void minprintf(char *fmt, ...);
or

void minprintf(int fmt, ...);
On many machines 'int' and 'char *' are the same size, have the same bit
representation, and are, basically, interchangeable . You can't rely on
this being true on all machines, of course...

Sep 6 '08 #8
On Sat, 6 Sep 2008 11:46:27 -0700 (PDT), mdh <md**@comcast.n etwrote:
>On Sep 5, 5:20 pm, Keith Thompson <ks...@mib.orgw rote:
>mdh <m...@comcast.n etwrites:
The section is titled Variable-length Argument lists.

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can't* be defined as functions.


May I pursue another issue a little further.

K&R (p 155) declare minprintf thus:

void minprintf(char *fmt, ...);
When I followed the example, it did not seem to matter to the output
whether the declaration was

void minprintf(char *fmt, ...);
or

void minprintf(int fmt, ...);
How were you able to check the first argument for '%' followed by 'd',
'f', or 's' if its type was int instead of char*?
>

The only difference is ( maybe the **only** will raise some chuckles)
that char *fmt points to all the arguments in my debugger, but int
What does this mean? How can a pointer in your code point to
something in your debugger? For that matter, how do you even pass
arguments to a debugger?
>returns an integer( surprise, surprise!). For fear of flogging a dead
Arguments don't "return" anything. Did you mean your debugger
displayed an integer?
>horse, is that the difference?
What difference? Show your code for a minprintf with an int as its
first parameter and a sample calling statement.

As coded in K&R, minprintf will handle variadic arguments (the ones
implied by the ...) of type int, double, and char* based on the
conversion specification in the format string.

If you change the parameter and argument to an int, how will minprintf
know the type of the next argument to extract? How will it know how
many arguments to extract?

--
Remove del for email
Sep 6 '08 #9
mdh
On Sep 6, 12:03*pm, gaze...@shell.x mission.com (Kenny McCormack)
wrote:
In article <3d002485-0189-49ff-bc1f-b5199f205...@b3 8g2000prf.googl egroups..com>,
On many machines 'int' and 'char *' are the same size, have the same bit
representation, and are, basically, interchangeable . *You can't rely on
this being true on all machines, of course...

thanks Kenny..there is clearly something I am not seeing...but
hopefully this will become clearer.
Sep 6 '08 #10

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

Similar topics

3
6016
by: wenke | last post by:
Hi, I am using the following code (see below) from php.net (http://www.php.net/manual/en/ref.xml.php, example 1) to parse an XML file (encoded in UTF-8). I changed the code slightly so that the cdata sections will be echoed an not the element names as in the original example. In the cdata sections of my XML file I have terms like this:
6
15512
by: Dimitri Tholen | last post by:
Hi all, For a project I m working on I am looking for a database with all current car-makes & models from post-WII till now. With all the car websites around I am sure this information should be available somewhere so that I can import it into my MySQL database, but I simply can't find it. Does anybody know where I can get my hands on "a" database with this
0
2430
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a href="http://blah.com/?test=test&amp;amp;test2=test2">Test2&amp;amp;</a> This results in the following HTML Code:
4
2826
by: MLH | last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application for me. When he was done, he sent me the PHP files and the MySQL dump file. Now, when I connect to the application on my LAN using http://192.168.1.106/~mlh/credifree/index.php the AMP app still thinks the data resides somewhere else. It runs fine - as long as I leave my LAN's external internet connection up. But if I unplug my LAN from the world, my app locks up. Before I...
16
1696
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
6
3268
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the work may not be completed before the event is triggered again I am trying to add critical sections round the producer & consumer parts of
4
1883
by: António Pinho | last post by:
Hi, I have a big problem with an webpart/assembly. i'm trying to connect to sql server but i get the error "Request for the permission of type System.Data.SqlClient.SqlClientPermission, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.". i've searched for it and can't find a clear answer. but one thing i'm sure: permissions. if i change the trust level in the web.config of the sharepoint portal...
6
2570
by: Mike Moore | last post by:
I'm not very familiar with the web config file. I would like to use the timeout item in the session state for our connection string, but not use the stateconnection string and sqlconnectionstring in the sessionstate section. I have our connection string information in the appsettings section of the web config file. Our authenication mode reads - <authentication mode="Windows" /> <identity impersonate="true" />
1
3483
by: Andrew Wan | last post by:
How can VBScript code access JScript code variables in the same ASP page? <SCRIPT LANGAUGE="VBScript"> Dim a a = 10 </SCRIPT> <SCRIPT LANGUAGE="JScript"> Response.Write(a); </SCRIPT>
7
185
by: Ioannis Vranos | last post by:
In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome. Ambiguous errata (mentioned first) of "The C Programming Language" 2nd Edition errata page (http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html), and
0
9820
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
10811
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...
1
10920
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
10461
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
9626
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
7169
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
5846
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...
1
4675
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
4270
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.