newbie-Why does an extra character appear?

I was playing with args from command line, trying to understand how
they work. But my little program shows up a strange behaviour that I
don't understand. An extra beginning char appears. For instance where I
expected "abba" I get "aabba" and instead of "tony I get "ttony". Can
someone explain why this is so.
Thanks
Bob

Advertisements

>I was playing with args from command line, trying to understand how
>they work. But my little program shows up a strange behaviour that I
>don't understand. An extra beginning char appears.

There is no guarantee that the argument strings for argv[] appear
in order or without extra junk in between them. If you look at
memory beyond the end of the argument strings, you have invoked
the wrath of undefined behavior, and you deserve anything you
get (including smegmentation faults).
>For instance where I
>expected "abba" I get "aabba" and instead of "tony I get "ttony".

It is a bad idea to hardcode addresses like this into your program.
The address might change on different runs of the same program. In
particular, on some platforms, the argv[] strings go at the top of
the stack (yes, I know C doesn't require a stack, but it doesn't
forbid one either), the address of the *end* of the strings may be
fixed, and the beginning may vary with the length of the strings.

On 27 Nov 2005 13:57:57 -0800, "Bob" <> wrote:
>I was playing with args from command line, trying to understand how
>they work. But my little program shows up a strange behaviour that I
>don't understand. An extra beginning char appears. For instance where I
>expected "abba" I get "aabba" and instead of "tony I get "ttony". Can
>someone explain why this is so.
>Thanks
>Bob
>
>THE PROGRAM
>// argtest.c
>#include <stdio.h>
>
>main(int argc, char *argv[])

If you are going to print pointer values, use %p and cast the value to
a void*.
> printf("argv[0]= %d\n", argv[0]);

You only print argv[0] once. The next time through the loop you print
argv[1]. It would be nice if your message reflected that:
printf("argv[%d]=%p\n", i, (void*)argv);
and delete the increment of argv below.
> printf("*argv= %s\n", *argv);
> argv++;
> }
>
> char *g;

Defining variables after executable statements is not standard before
C99. Don't restrict the number of people who can help you to those
who have a C99 compiler.
> for(i=0; i<20; i++) {
> g = (char*)(4006977+i);
> printf(" %c\n", *g);

You are printing 20 sequential characters beginning with the character
pointed to by argv[0]. You have already established that this string
is only 8 bytes long. Evaluating anything after that invokes
undefined behavior.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!