Segmentation fault error

E

enjoyfate

I am newbie ,in linux/gnu envirement,wrot test file below to see the
first argument.
#include "stdio.h"
void main (int argc, char *argv[])
{
printf ("%s", argv[0][1]);
}
After compiled it,
$ ./test ww
Segmentation fault

I have no clue to work out. help , thanks
 
W

Willem

Greg Comeau wrote:
)>$ ./test ww
)>Segmentation fault
)
) argv[0] points to "ww"

Nitpick: it points to "test" or perhaps "./test".

) argv[0][1] is 'w' (the second one)

Nitpick: So this would either be 'e' or '/'.

) Therefore, you're trying the pass the value of the char 'w' to %s.

Otherwise, your comment is perfectly valid.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
L

lovecreatesbea...

Greg Comeau wrote:
)>$ ./test ww
)>Segmentation fault
)
) argv[0] points to "ww"
Nitpick: it points to "test" or perhaps "./test".
) argv[0][1] is 'w' (the second one)
Nitpick: So this would either be 'e' or '/'.
) Therefore, you're trying the pass the value of the char 'w' to %s.
Otherwise, your comment is perfectly valid.

Oops yeas, and in that case, it's worth pointing out that it (argv[0])
may actually be a null pointer on the above system (which means [1]ing
it does no good), although it probably is "./test".

How then is main function invoked, if the relevant argv[0] is null?
 
S

santosh

Greg Comeau wrote:
)>$ ./test ww
)>Segmentation fault
)
) argv[0] points to "ww"
Nitpick: it points to "test" or perhaps "./test".
) argv[0][1] is 'w' (the second one)
Nitpick: So this would either be 'e' or '/'.
) Therefore, you're trying the pass the value of the char 'w' to %s.
Otherwise, your comment is perfectly valid.

Oops yeas, and in that case, it's worth pointing out that it
(argv[0]) may actually be a null pointer on the above system (which
means [1]ing it does no good), although it probably is "./test".

How then is main function invoked, if the relevant argv[0] is null?

The two have no relationship. How main is invoked is implementation
defined. If argc is non-zero then argv[0] points to a string which
contains the command by which the program was invoked. If no such
string can be had from the environment then argv[0][0] is a null
character. Whether or not argc is zero or non-zero argv[argc] is always
a null pointer.

Note that the string pointed to by argv[0] may not necessarily be the
actual name by which your program was invoked. Under many systems this
string could be set to any name before invoking your program, so this
not a 100% reliable means of obtaining the invocation command of your
program.
 
S

santosh

Greg said:
Greg Comeau wrote:
)>$ ./test ww
)>Segmentation fault
)
) argv[0] points to "ww"

Nitpick: it points to "test" or perhaps "./test".

) argv[0][1] is 'w' (the second one)

Nitpick: So this would either be 'e' or '/'.

) Therefore, you're trying the pass the value of the char 'w' to %s.

Otherwise, your comment is perfectly valid.

Oops yeas, and in that case, it's worth pointing out that it (argv[0])
may actually be a null pointer on the above system (which means [1]ing
it does no good), although it probably is "./test".

To clarify further... argv[0] is not a null pointer so long as argc >
0 (and when it's not, I seem to recall it's indeterminate).

When argc is not greater than zero it must be zero and argv[argc] will
be a null pointer. So there is no indeterminacy, AFAICS.
What I had intended to say though was that argv[0][0] may be the
null character meaning there may not be a "helpful" string available
via argv[0].

Exactly, and it's easy to do this under POSIX system via the exec family
of functions by simply giving a misleading string or nothing at all as
the second and/or subsequent arguments.
 
J

jaysome

(e-mail address removed) said:
I am newbie ,in linux/gnu envirement,wrot test file below to see the
first argument.
#include "stdio.h"
void main (int argc, char *argv[])
{
printf ("%s", argv[0][1]);
}
After compiled it,
$ ./test ww
Segmentation fault

I have no clue to work out. help , thanks

#include <stdio.h>

int main(int argc, char **argv)
{
printf("%s\n", argv[0]);
return 0;
}

This program could result in undefined behavior.

The C Standard says this about the function main:

-- The value of argc shall be nonnegative.
-- argv[argc] shall be a null pointer.

If argc shall be nonnegative, then 0 is an acceptable value. If argc
is 0, then argv[0] shall be a null pointer. In such a case, the printf
statement above is equivalent to:

printf("%s\n", (char*)0);

This is undefined behavior, as pointed out by some arguably
hall-of-fame members of this newsgroup in a thread that appeared over
10 years ago:

http://groups.google.com/group/comp.../16f4d84b10c041ed/c95cbfb0302763f3?lnk=st&q=#

The moral of this story is to judiciously check the value of argc
before using the value of argv in a way that could lead to undefined
behavior. For example:

#include <assert.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if ( argc > 0 )
{
printf("%s\n", argv[0]);
}
else
{
assert(argc == 0); /* must be nonnegative, so must be 0 */
printf("argc is 0.\n");
}
return 0;
}

Regards
 
N

Nick Keighley

I am newbie ,in linux/gnu envirement,wrot test file below to see the
first argument.
#include "stdio.h"

it's usual to pull in standard headers like this

#include <stdio.h>

non-standard headers like this:

#include "mylib.h"

<snip>
 
D

dj3vande

(e-mail address removed) said:
void main (int argc, char *argv[])
int main(int argc, char **argv)

Every difference between the above version and your version is significant.

*Every* difference? What's the significance of "char *argv[]" vs.
"char **argv"?


dave

--
Dave Vandervies dj3vande at eskimo dot com
Where can I get a free fully C99-conforming compiler for this 'ere mainframe?
Sorry, Richard, that sort of thing only works when the thing you're asking for
actually exists. --Richard Heathfield and Ben Pfaff in comp.lang.c
 
R

Richard Tobin

*Every* difference? What's the significance of "char *argv[]" vs.
"char **argv"?
[/QUOTE]
Documentary. The newer syntax fully recognises the "new" <cough> pointer
syntax. Using the [] syntax for pointer parameters is like saying
"forsooth" and "oddsbodkins" - legal, but nevertheless an archaic
affectation.

On the contrary; the "old" syntax documents that the argument refers
to an array, rather than merely being a pointer used to return a value.
The "new" syntax conflates the two.

-- Richard
 
C

CBFalconer

Richard Heathfield said:
(e-mail address removed) said:
void main (int argc, char *argv[])
int main(int argc, char **argv)
Every difference between the above version and your version is
< significant.

*Every* difference? What's the significance of "char *argv[]"
vs. "char **argv"?

In a function header describing a parameter, none. The parameter
is always passed as a pointer to a pointer to char. The void type
for main is a horror.
 
K

Keith Thompson

*Every* difference? What's the significance of "char *argv[]" vs.
"char **argv"?
Documentary. The newer syntax fully recognises the "new" <cough> pointer
syntax. Using the [] syntax for pointer parameters is like saying
"forsooth" and "oddsbodkins" - legal, but nevertheless an archaic
affectation.

On the contrary; the "old" syntax documents that the argument refers
to an array, rather than merely being a pointer used to return a value.
The "new" syntax conflates the two.

The *language* conflates the two. The "new" syntax accurately
reflects this conflation. Using [] in a parameter declaration
documents the intent, but doesn't enforce it. A comment is, IMHO, a
better way to do this, and also gives you the opportunity to provide
more information, such as that a char* parameter points to a string,
not just to the first element of some arbitrary array.

(As I recall, one of C's ancient ancestors, either B or BCPL, actually
used "[]" as the syntax for pointers; thus the reference to the
<cough> "new" pointer syntax.)
 
D

David Thompson

(e-mail address removed) wrote:
How then is main function invoked, if the relevant argv[0] is null?

The two have no relationship. How main is invoked is implementation
defined. If argc is non-zero then argv[0] points to a string which
contains the command by which the program was invoked. If no such
string can be had from the environment then argv[0][0] is a null
character. Whether or not argc is zero or non-zero argv[argc] is always
a null pointer.
I'd make a distinction here: argv[0] is officially supposed to be, or
rather 'represent', the program name. On many systems and in many
cases, the command used to invoke a program is some form of its name,
but not always; aside from aliases and symlinks and such, where it
could be debated whether the source or the target is 'really' the
command and/or the name, the most obvious example was Digital's DCL
where on RT-11 for example COPY and DELETE both actually ran PIP.
(Admittedly not a C program, and definitely not a portable one.)
Note that the string pointed to by argv[0] may not necessarily be the
actual name by which your program was invoked. Under many systems this
string could be set to any name before invoking your program, so this
not a 100% reliable means of obtaining the invocation command of your
program.

Yes, even though it's supposed to be the name, it might not.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top