|  | Segmentation fault error |  | |
| | | Guest |  |
| Posted: Fri Jul 25, 2008 5:17 am Post subject: Segmentation fault error |  |
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 |
| |
| | | Greg Comeau |  |
| Posted: Fri Jul 25, 2008 5:17 am Post subject: Re: Segmentation fault error |  |
In article <68cfa85b-2583-45dd-920b-ae3c50fd11fc@x29g2000prd.googlegroups.com>, <enjoyfate@gmail.com> wrote:
| Quote: | 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[])
|
Check out LINK
| Quote: | { printf ("%s", argv[0][1]); } After compiled it, $ ./test ww Segmentation fault
I have no clue to work out. help , thanks
|
argv[0] points to "ww" argv[0][1] is 'w' (the second one)
Therefore, you're trying the pass the value of the char 'w' to %s. Dunno what you want to do but probably you were try to do one of these:
printf ("%s", argv[0]); printf ("%c", argv[0][1]); -- Greg Comeau / 4.3.10.1 with C++0xisms now in beta! Comeau C/C++ ONLINE ==> LINK World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it? |
| |
| | | Greg Comeau |  |
| Posted: Fri Jul 25, 2008 5:39 am Post subject: Re: Segmentation fault error |  |
In article <slrng8is5l.pl5.willem@snail.stack.nl>, Willem <willem@stack.nl> wrote:
| Quote: | Greg Comeau wrote: ) <enjoyfate@gmail.com> 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". -- Greg Comeau / 4.3.10.1 with C++0xisms now in beta! Comeau C/C++ ONLINE ==> LINK World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it? |
| |
| | | Willem |  |
| Posted: Fri Jul 25, 2008 6:20 am Post subject: Re: Segmentation fault error |  |
Greg Comeau wrote: ) <enjoyfate@gmail.com> 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 |
| |
| | | Richard Heathfield |  |
| Posted: Fri Jul 25, 2008 6:49 am Post subject: Re: Segmentation fault error |  |
| |  | |
enjoyfate@gmail.com said:
| Quote: | 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; }
Every difference between the above version and your version is significant.
a) use <angle-brackets>, not "quotes", for standard headers. b) main is required to return int. c) argv[0][1] is a single character, but you seem to imagine that it's a string ("%s" means "string" in printf-speak). You are asking the program to treat a character value as if it were a pointer, and that is why you are getting your segfault; argv[0] describes the string you want. d) the standard output stream, like any text stream, should have a newline appended to each line, hence the \n in my version. e) because main is required to return int, return an int from main. 0 will do fine in the absence of a reason to return any other value.
-- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
| |
| | | santosh |  |
| Posted: Fri Jul 25, 2008 8:01 am Post subject: Re: Segmentation fault error |  |
| |  | |
lovecreatesbea...@gmail.com wrote:
| Quote: | On Jul 25, 3:39 pm, com...@panix.com (Greg Comeau) wrote: In article <slrng8is5l.pl5.wil...@snail.stack.nl>, Willem <wil...@stack.nl> wrote: Greg Comeau wrote: ) <enjoyf...@gmail.com> 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. |
| |
| | | lovecreatesbea...@gmail.c |  |
| Posted: Fri Jul 25, 2008 9:02 am Post subject: Re: Segmentation fault error |  |
On Jul 25, 3:39 pm, com...@panix.com (Greg Comeau) wrote:
| Quote: | In article <slrng8is5l.pl5.wil...@snail.stack.nl>, Willem <wil...@stack.nl> wrote: Greg Comeau wrote: ) <enjoyf...@gmail.com> 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? |
| |
| | | Greg Comeau |  |
| Posted: Fri Jul 25, 2008 1:17 pm Post subject: Re: Segmentation fault error |  |
| |  | |
In article <g6bvvd$57s$1@panix1.panix.com>, Greg Comeau <comeau@comeaucomputing.com> wrote:
| Quote: | In article <slrng8is5l.pl5.willem@snail.stack.nl>, Willem <willem@stack.nl> wrote: Greg Comeau wrote: ) <enjoyfate@gmail.com> 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). 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]. -- Greg Comeau / 4.3.10.1 with C++0xisms now in beta! Comeau C/C++ ONLINE ==> LINK World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it? |
| |
| | | Greg Comeau |  |
| Posted: Fri Jul 25, 2008 1:29 pm Post subject: Re: Segmentation fault error |  |
| |  | |
In article <82d8ff38-28a3-4fdc-bb12-deee1870d45b@v26g2000prm.googlegroups.com>, lovecreatesbea...@gmail.com <lovecreatesbeauty@gmail.com> wrote:
| Quote: | On Jul 25, 3:39 pm, com...@panix.com (Greg Comeau) wrote: In article <slrng8is5l.pl5.wil...@snail.stack.nl>, Willem <wil...@stack.nl> wrote: Greg Comeau wrote: ) <enjoyf...@gmail.com> 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,
|
We're not exactly told. For some insight (but no means the final words) check out LINK
| Quote: | if the relevant argv[0] is null?
|
It's not, I wrote-o'd. I meant argv[0][0] might be the null character meaning there is no useful string via argv[0].
Generally speaking though, although I seem to recall the Standard pressures assurances, I really don't see how it can all be enforced even with a strictly conforming situation. The OS et al is just so way beyond the scope of C. So, I assume argv to be a gift when you can get it  And just note that although Windows, UNIX, etc often gives you exactly what you typed, when on other OS's you may not get it (or even anything like it -- I could imagine a system while only provides in one case, another where it converts it to another spoken language, another where it is complete garbage, etc, and argv[0] in particular has issues because is hte command what you typed, the full path, nothing, etc etc.)), and IMO, that's ok with the standard(s) because despite any assurances the bottom line is that this stuff is implementation-defined, and so can only be a "fighting chance" of intentions. -- Greg Comeau / 4.3.10.1 with C++0xisms now in beta! Comeau C/C++ ONLINE ==> LINK World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it? |
| |
| | | santosh |  |
| Posted: Fri Jul 25, 2008 1:53 pm Post subject: Re: Segmentation fault error |  |
| |  | |
Greg Comeau wrote:
| Quote: | In article <g6bvvd$57s$1@panix1.panix.com>, Greg Comeau <comeau@comeaucomputing.com> wrote: In article <slrng8is5l.pl5.willem@snail.stack.nl>, Willem <willem@stack.nl> wrote: Greg Comeau wrote: ) <enjoyfate@gmail.com> 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.
| Quote: | 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. |
| |
| Page 1 of 3 .:. Goto page 1, 2, 3 Next | |
|
|