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

Weird pointer arithmetic on argv - How can it work??

I need to modify the code of a command-line tool. The source code
starts out like this:

int main(int argc, char *argv[])
{
int ch, A, B ;

while ((ch = getopt(argc, argv, "AB")) != -1)
switch (ch) {
case 'A':
A = 1;
break;
case 'B':
B = 1 ;
break;
}
argc -= optind;
argv += optind;

What in heaven's name is that last line doing? It seems to be doing
pointer arithmetic, incrementing argv by the current value of optind
(which is an integer, the "ind"ex of the current argument). But I
thought you can only do pointer arithmetic when the size of each array
element is a constant known by the compiler, and of course a char* does
not have a constant size. How can this even compile? (It does).

Furthermore, I want to rewrite this with argv assigned in the code
instead of being a command-line argument, but when I write:

char* argv[4] ;
argv += optind ;

the compiler complains, rightfully so I would say, "incompatible types
in assignment" on that second line. Why does it work for them, but it
doesn't work for me?

Thanks!

Jerry Krinock

Oct 15 '06 #1
5 3131
>I need to modify the code of a command-line tool. The source code
>starts out like this:

int main(int argc, char *argv[])
char *argv[] in this context (function parameter) really means the
same as char **argv.
>{
int ch, A, B ;

while ((ch = getopt(argc, argv, "AB")) != -1)
switch (ch) {
case 'A':
A = 1;
break;
case 'B':
B = 1 ;
break;
}
argc -= optind;
argv += optind;

What in heaven's name is that last line doing? It seems to be doing
pointer arithmetic, incrementing argv by the current value of optind
(which is an integer, the "ind"ex of the current argument).
Correct. It's skipping over the flag arguments. argv is a
pointer-to-pointer, and you're advancing it by optind pointers.
Knowing the way the non-standard function getopt() is supposed to
work, it won't go out of range of the array.

>But I
thought you can only do pointer arithmetic when the size of each array
element is a constant known by the compiler, and of course a char* does
not have a constant size.
Of course a (char *) *DOES* have a constant size (it's often 32 or
64 bits). argv is an array of pointers, and char * pointers have
a constant size. The strings pointed at do not have constant size,
but they aren't *in* the array.
>How can this even compile? (It does).
>Furthermore, I want to rewrite this with argv assigned in the code
instead of being a command-line argument, but when I write:

char* argv[4] ;
argv += optind ;
argv has char ** type. You can only declare it as char *argv[] if
it's a function parameter, which is equivalent to char ** (when it's
a function parameter). You cannot increment an array name, which is
a problem if you declare it as char *argv[] as a local variable.

char *av[4];
char **argv = av;
/* somehow you initialize av here */
argv += optind;
>the compiler complains, rightfully so I would say, "incompatible types
in assignment" on that second line. Why does it work for them, but it
doesn't work for me?
Oct 15 '06 #2
je***@sheepsystems.com writes:
I need to modify the code of a command-line tool. The source code
starts out like this:

int main(int argc, char *argv[])
{
int ch, A, B ;

while ((ch = getopt(argc, argv, "AB")) != -1)
switch (ch) {
case 'A':
A = 1;
break;
case 'B':
B = 1 ;
break;
}
argc -= optind;
argv += optind;

What in heaven's name is that last line doing? It seems to be doing
pointer arithmetic, incrementing argv by the current value of optind
(which is an integer, the "ind"ex of the current argument). But I
thought you can only do pointer arithmetic when the size of each array
element is a constant known by the compiler, and of course a char* does
not have a constant size. How can this even compile? (It does).
A char* does have a constant size.

C doesn't actually have array parameters. In a parameter list,
"char *argv[]" really means "char **argv", so argv is a pointer to
pointer to char. (Presumably the pointer-to-char that argv points
to is the first element of an array of pointers-to-char.)

C allows the "[]" syntax here for "convenience, but in my opinion it
just causes confusion.

I suggest reading section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.
Furthermore, I want to rewrite this with argv assigned in the code
instead of being a command-line argument, but when I write:

char* argv[4] ;
argv += optind ;

the compiler complains, rightfully so I would say, "incompatible types
in assignment" on that second line. Why does it work for them, but it
doesn't work for me?
Outside a parameter list, "char *argv[4];" really does declare an
array, not a pointer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 15 '06 #3

je***@sheepsystems.com wrote:
I need to modify the code of a command-line tool. The source code
starts out like this:

int main(int argc, char *argv[])
{
int ch, A, B ;

while ((ch = getopt(argc, argv, "AB")) != -1)
switch (ch) {
case 'A':
A = 1;
break;
case 'B':
B = 1 ;
break;
}
argc -= optind;
argv += optind;

What in heaven's name is that last line doing? It seems to be doing
pointer arithmetic, incrementing argv by the current value of optind
(which is an integer, the "ind"ex of the current argument).
That's precisely what it's doing.
But I
thought you can only do pointer arithmetic when the size of each array
element is a constant known by the compiler, and of course a char* does
not have a constant size.
On my machine, sizeof(char *) returns 4. I suspect a 64 bit machine
would return 8. While the array it points to may not have a size known
at compile time, the pointer itself has a set size. Remember: a
pointer's just an integer.
How can this even compile? (It does).

Furthermore, I want to rewrite this with argv assigned in the code
instead of being a command-line argument, but when I write:
Well that's what argv is. I suggest you pick a new name for your array,
for the sake of anybody that might maintain your program.
char* argv[4] ;
argv += optind ;

the compiler complains, rightfully so I would say, "incompatible types
in assignment" on that second line. Why does it work for them, but it
doesn't work for me?
char* argv[4] is an array of four char pointers. You can't perform
pointer arithmetic on an array.
As to why it works for them, it's because as a parameter, a[] is
equivalent to *a. (You can't pass actual arrays in C.)
Thanks!

Jerry Krinock
Oct 15 '06 #4
je***@sheepsystems.com wrote:
>
I need to modify the code of a command-line tool. The source code
starts out like this:

int main(int argc, char *argv[])
{
int ch, A, B ;

while ((ch = getopt(argc, argv, "AB")) != -1)
switch (ch) {
case 'A':
A = 1;
break;
case 'B':
B = 1 ;
break;
}
argc -= optind;
argv += optind;

What in heaven's name is that last line doing? ...
Who knows. getopt() is not a standard C routine, and you failed to
show its source. You also failed to declare optind.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Oct 15 '06 #5
Thank you, guys. I understand it now and have my program working.

Oct 15 '06 #6

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

Similar topics

6
by: a | last post by:
I am having trouble understanding the results of the following code: #include <iostream> using namespace std; int main(int argc, char* argv) { unsigned short *IO =...
8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
1
by: Pieter Claassen | last post by:
Ok, I have something that works, but I don't understand why. I use libpcap to get data of the wire and then initially I casted the packet to ethernet, then marched along the memory in chunks of...
22
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
8
by: subramanian100in | last post by:
Supoose we have, int x; int *p = &x; int *q; q = p + 1;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...
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
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...
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,...

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.