473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

K&R 5.7

mdh
A happy and safe Easter to all.

I am tackling this, and in the process, wrote this little bit of code.

#include <stdio.h>

static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};

int main () {
int i;
for (i=1; i<=12; i++)
/**printf("%d\n", mnths[i]); **/ works
printf("%d\n", *mnths++); <<<-------error:rong type code to increment
return 0;
}

I thought that an array was passed to printf as a pointer, but I am
obviously missing something.

Apr 8 '07 #1
1 1173
In article <11**********************@b75g2000hsg.googlegroups .com>
mdh <md**@comcast.netwrote:
>static char mnths[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
(Of course, this is just an example, but I note that it is
kind of silly to have mnths[i] == i.)
>int main () {
int i;
for (i=1; i<=12; i++)
/**printf("%d\n", mnths[i]); **/ works
printf("%d\n", *mnths++); <<<-------error:rong type code to increment
return 0;
}

I thought that an array was passed to printf as a pointer, but I am
obviously missing something.
This is a little tricky.

An array never "is" a pointer (nor vice versa).

An array name, in some cases, *becomes* a pointer *value*. ("But
isn't that the same thing?" The answer is: no, not quite. The
"some cases" thing is very important, as is the "becomes", which
might better be termed "produces" or "computes".)

When you do get a pointer, the pointer value is *computed*, in much
the same way as:

printf("%d\n", 5 + 4);

has to *compute* the sum (9). Although the sum is always actually
9, there is no literal 9 here, just a 4 and a 5. Clearly, the fact
that the sum is 9 does not mean that either 4 or 5 is nine. :-)
Likewise, when you attempt to pass an "array value" to a function,
the compiler computes a pointer value that points to the first
element of the array.

The reason "*mnths++" does not work is that the "++" operator is
applied to "mnths", which is the name of the array. (Remember that
*mnths++ "means" *(mnths++), so the ++ applies to the array name,
not the result of the "*".) The "++" operator demands an object
(or more loosely speaking, a "variable"), not a value.

The transformation of array name to pointer value occurs *only*
when you ask for the "value" of an array. Since "++" needs an
object, not a value, the transformation simply does not happen.

What you can do, if you like, is create a separate pointer variable
(i.e., an "object") and make it point to the first element of
the "mnths" array:

char *p = mnths; /* or: char *p = &mnths[0]; */

The right hand side of an "=" operator needs a value. Here, we
have "mnths", an array. That transformation, from array name to
pointer value, now occurs -- the compiler computes &mnths[0],
just as if you had asked for it explicitly.

Once you have a variable, you can apply the "++" operator to it:

printf("%d\n", *p++);

Here the ++ operator works on the object p, and the value stored
there. It obtains the original value, adds 1, schedules the new
value to be stored back into p, and produces, as its result, the
old value. The "*" operator then takes this value (which is, as
it must be, a pointer) and follows the pointer to whatever it
points to.

The first time you do this, you will get mnths[0] -- so this is
like looping from 0, not from 1. Note that your commented-out
version prints mnths[1], then mnths[2], and so on. If we do:

int i;
char *p = mnths;

for (i = 1; i <= 12; i++)
printf("%d\n", *p++);

you will print mnths[0], then mnths[1], and so on. The mnths[]
array has 13 elements, numbered 0 through 12 inclusive, so it is
OK to do either one, but the output will be different.

Note, by the way, that transformation from "array object" to "pointer
value" happens *extremely* often. It happens so often, it tends
to confuse beginners -- and sometimes even non-beginners -- into
thinking that it always happens. But it does not. An even better
example occurs when using the sizeof operator:

printf("%u\n", (unsigned int)sizeof mnths);

which prints 13 -- the size of the array -- and not the size of a
pointer (generally 2, 4, or 8 on most machines today).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Apr 8 '07 #2

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

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
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...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.