473,398 Members | 2,389 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,398 software developers and data experts.

Style vs function? in arrays

mdh
I guess pointers just take time!!!

This is exercise 5-13, peripherally.
The function accepts a command line argument of -n.

So, given

int main ( int argc, char *argv[])

.......
if ( argc == 2 && (*++argv)[0]=='-')
n= atoi(argv[0]+1);

......

May I ask this?
1) in a given function ( ? scope ?) once one has advanced the pointer
( to argv..in this case) is that where the pointer will remain
pointing at? and until when?

2) In the case above, where Tondo and Gimpel use indices as well as
pointer incrementation, is there a good reason for this or is this
just a matter of style? ie why not use ( instead of (*++argv)[0])
argv[1][0]? ( if that indeed is equivalent?)

3) I have until now not seen the construct "atoi(argv[0]+1". Does this
mean get the pointer at index argv[1] ( which I believe is an pointer
to char array, then add 1 to that pointer? ( not argv)?

Thanks in advance.

Apr 28 '07 #1
6 1518
mdh said:
The function accepts a command line argument of -n.

So, given

int main ( int argc, char *argv[])
......
if ( argc == 2 && (*++argv)[0]=='-')
n= atoi(argv[0]+1);
In general, avoid atoi, which is not robust in the face of ill-formed
input. strtol is far superior.
1) in a given function ( ? scope ?) once one has advanced the pointer
( to argv..in this case) is that where the pointer will remain
pointing at? and until when?
argv has type char **, and it's initially pointing at the first element
in an array of char *, each of which contains the address of the first
character in a string. When you do ++argv, you're moving it to point to
the /second/ element in that array of char *.

Let's look at it in terms of what might actually happen inside a real
(and very very simple) machine. Somewhere in memory, you have something
like this:

+--------+--------+--------+
addr + 0x3000 | 0x3002 | 0x3004 |
+--------+--------+--------+
val + 0x4200 | 0x4280 | 0x0000 |
+--------+--------+--------+

and, elsewhere, you have something like:

+--------+--------+--------+--------+--------+--------+
addr + 0x4200 | 0x4201 | 0x4202 | 0x4203 | 0x4204 | 0x4205 |
+--------+--------+--------+--------+--------+--------+
val + 'E' | 'x' | '5' | '1' | '3' | '\0' |
+--------+--------+--------+--------+--------+--------+

and...

+--------+--------+--------+
addr + 0x4280 | 0x4281 | 0x4282 |
+--------+--------+--------+
val + '-' | '6' | '\0' |
+--------+--------+--------+
argv starts off with the value 0x3000 (in our example, anyway!). As you
can see from the first array diagram, *argv is 0x4200. But then we add
1 to argv using ++argv. This means "move argv along by one OBJECT". So
we do that, moving argv's value from 0x3000 to the next slot in the
array, which is 0x3002. Then we deref it, which gives us the value
0x4280 (the value stored at address 0x3002). Finally, we come to [0].

Bear in mind that A[i] and *(A + I) are synonymous in C. So what we have
finally arrived at is *(0x4280 + 0) which is *0x4280 which is... check
the diagram... '-'.

So (*++argv)[0] ends up meaning this: "move argv along a bit, then find
the first character in what has now become the string that argv is
pointing to".

Better:

myarg = argv[1];

if(myarg[0] == '-')
{
n = strtol(myarg + 1, &e, 10);

where e is a char *. (Look up strtol.)
2) In the case above, where Tondo and Gimpel use indices as well as
pointer incrementation, is there a good reason for this or is this
just a matter of style? ie why not use ( instead of (*++argv)[0])
argv[1][0]? ( if that indeed is equivalent?)
It's nearly equivalent. ++argv has the side effect of adding 1 to argv,
which affects all the indices in subsequent lines.

I can't comment on T&G's code, since I have never seen it.
3) I have until now not seen the construct "atoi(argv[0]+1". Does this
mean get the pointer at index argv[1] ( which I believe is an pointer
to char array, then add 1 to that pointer? ( not argv)?
To understand atoi(argv[0] + 1), start with argv[0] and be sure you know
what it points to. If argv's value has been modified prior to this
point, that is important (and, in your example, it has been).

Once you know that it points to the first character in a string, you can
see that argv[0] + 1 will point to the second character in that string.
So for example, if argv[0] --"-42987", then argv[0] + 1 --"42987",
argv[0] + 2 --"2987", argv[0] + 3 --"987", etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 28 '07 #2
mdh
On Apr 27, 10:49 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>>
Richard,
Thank you for that very detailed response.
In general, avoid atoi,........... strtol is far superior.
Thanks...I don't think that has been covered yet...but will look it
up.

To understand atoi(argv[0] + 1), start with argv[0] and be sure you know
what it points to. If argv's value has been modified prior to this
point, that is important (and, in your example, it has been).
I see.....
Once you know that it points to the first character in a string, you can
see that argv[0] + 1 will point to the second character in that string.
So for example, if argv[0] --"-42987", then argv[0] + 1 --"42987",
argv[0] + 2 --"2987", argv[0] + 3 --"987", etc.

Richard, if argv ( as described above) now points to the first element
of argv and argv[0] points to the first character of the first
element, then why does argv[1] not point to the second character of
the first element of argv? ( I know it does not, as I have tried this
and it crashes)

Apr 28 '07 #3
mdh said:

<snip>
Richard, if argv ( as described above) now points to the first element
of argv
Stop right there. You are overloading the meaning of argv, using it to
mean both the second parameter of main *and* the array to whose first
element that parameter initially points. Normally that doesn't matter
very much, because it's pretty obvious what you mean, but when you
modify the parameter (++argv or whatever) it no longer points to that
first element of the array of command line strings, and so it's unwise
to confuse the issue by using the same name to refer to both.
and argv[0] points to the first character of the first
element,
Right.
then why does argv[1] not point to the second character of
the first element of argv?
Because it points to the first character of the second element. But
argv[0] + 1 does point to the second character of the first element.

X[Y] + Z does not mean the same as X[Y + Z].

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 28 '07 #4
mdh
On Apr 28, 12:07 am, Richard Heathfield <r...@see.sig.invalidwrote:
mdh said:

<snip>
then why does argv[1] not point to the second character of
the first element of argv?

Because it points to the first character of the second element. But
argv[0] + 1 does point to the second character of the first element.

Ok...I think I get it. Argv always refers to the array of char *.
It's the [whatever] that points to the actual character.

Thank you Richard....I am sure that soon this will feel like "old
hat". Right now...the hat seems to fit awkwardly. :-)

Apr 28 '07 #5
mdh said:

<snip>
Ok...I think I get it. Argv always refers to the array of char *.
It's the [whatever] that points to the actual character.
If in doubt, never use more than one * or [] (except in declarations!).
So for example, to do what you originally wanted, don't write this:

int n = strtol((*++argv)[0], &e, 10);

Write this instead:

char *firstrealarg = argv[1];
int n = strtol(firstrealarg, &e, 10);

Isn't that a lot easier to understand?
Thank you Richard....I am sure that soon this will feel like "old
hat". Right now...the hat seems to fit awkwardly. :-)
The trick is to start with the bowl shape on the top. Get that right
first, and fit a brim later on.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 28 '07 #6
mdh
On Apr 28, 12:27 am, Richard Heathfield <r...@see.sig.invalidwrote:
>
If in doubt, never use more than one * or [] (except in declarations!).
Write this instead:
>
char *firstrealarg = argv[1];
int n = strtol(firstrealarg, &e, 10);

Isn't that a lot easier to understand?
Point taken...thanks.
Time to get some sleep.... :-)

thanks for your help. ( as always)
Apr 28 '07 #7

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

Similar topics

10
by: kaeli | last post by:
Is it just me, or does obj.setAttribute("style","border: thin solid navy"); (for example - no style seems to be set) not work in IE6 but works fine in NN6? I don't want to use element.style...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
54
by: Sander | last post by:
1. I was having a discussion with somebody and I think it's just religious. We are developing some software and he was looking through my code. I use if (pointer) to test a pointer for NULL. He...
15
by: Earl Higgins | last post by:
The company where I work as a Senior Software Engineer is currently revamping their (1991 era) "Programming and Style Guidelines", and I'm on the committee. The company, in business for over 20...
86
by: PTY | last post by:
Which is better? lst = while lst: lst.pop() OR while len(lst) 0:
2
by: bundaism | last post by:
The code below is meant to go through all images on the page, and add a red border on the mouseover, and a white border on the mouseout. It's not working and I've spend too many hours on this -...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Michael C | last post by:
"John Brock" <jbrock@panix.comwrote in message news:gg56ab$qsn$1@reader1.panix.com... You really need to stop trying to write perl code in dot net and VBA. Arrays are hard to resize for a good...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.