|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
int main ()
{ char *str = *Aamit ; *str='R' ; printf("%s \n " ,str); } it's giving segfault why ????? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
kumarvis@gmail.com said:
> int main () > { > char *str = *Aamit ; > *str='R' ; > printf("%s \n " ,str); > } > > it's giving segfault > why ????? What you have written doesn't compile, so it's hard to see how it could segfault. -- 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
kumarvis@gmail.com wrote:
> int main () > { > char *str = *Aamit ; > *str='R' ; > printf("%s \n " ,str); > } > > it's giving segfault > why ????? int main(void) { char *str = "Aamit" ; *str = 'R' ; puts(str); return 0; } -- pete |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
kumarvis@gmail.com wrote:
> int main () > { > char *str = *Aamit ; > *str='R' ; > printf("%s \n " ,str); > } > > it's giving segfault > why ????? If you meant to write char *str = "Aamit"; Then the *str ="R"; possibly writes into read-only memory, but surely invokes undefined behavoir. segvault is one possible outcome of that. Another fault (and causing undefined behavoir) is the lack of #include <stdio.h> resp. the resulting lack of a prototype for the varadic function printf(). Bye, Jojo |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
pete wrote:
> kumarvis@gmail.com wrote: >> int main () >> { >> char *str = *Aamit ; >> *str='R' ; >> printf("%s \n " ,str); >> } >> >> it's giving segfault >> why ????? > > int main(void) > { > char *str = "Aamit" ; Should be: char str[] = "Aamit" ; instead. > *str = 'R' ; > puts(str); > return 0; -- pete |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
pete <pfiland@mindspring.com> writes:
> kumarvis@gmail.com wrote: >> int main () >> { >> char *str = *Aamit ; >> *str='R' ; >> printf("%s \n " ,str); >> } >> it's giving segfault >> why ????? > > int main(void) > { > char *str = "Aamit" ; > > *str = 'R' ; > puts(str); > return 0; > } pete, what exactly was the point of your followup? You corrected the OP's compilation error and presumably reproduced something close to his actual code (which the OP should have posted himself). Meanwhile, you haven't answered his actual question, or said anything at all about it. You also forgot the required "#include <stdio.h>" and replaced the OP's printf call with a puts call, which subtly changed the behavior of the program. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
kumarvis@gmail.com writes:
> int main () > { > char *str = *Aamit ; > *str='R' ; > printf("%s \n " ,str); > } > > it's giving segfault No, it isn't. I'm sure the program you're actually running gives you a seg fault, but the code you posted won't even compile. Never re-type code when you post it here. Always copy-and-paste the *exact* code that you're actually compiling. You also have a few problems that your compiler might not warn you about: If you're going to use printf, you *must* have "#include <stdio.h>". "int main()" is ok, but "int main(void)" is better. You should add a "return 0;" at the end of main. It's not required in all circumstances, but it can't hurt. The space after the "%s" in your printf format is fairly harmless, but almost certainly useless. It causes an extra blank to be printed after your string. The way you format your code is odd and makes it more difficult to read. The compiler doesn't care about whitespace between tokens, but for the sake of your readers it's generally best to have a space after each comma and surrounding binary operators like "=", and *not* to have a blank preceding a semicolon or comma. And see question 1.32 of the comp.lang.c FAQ, <http://www.c-faq.com/>. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 29 May, 13:54, kumar...@gmail.com wrote:
> int main () > { > char *str = *Aamit ; > *str='R' ; > printf("%s \n " ,str); > > } > > it's giving segfault > why ????? Apart from the problems other posters have already pointed out, there is a major one which they haven't :- printf() expects its (str) argument to point to a zero-terminated string. Your code modifies the first character which str is pointing at, but does nothing about the following ones. If they are all non-zero junk, and you have fixed the other problems, it is quite possible for printf() to run off the end of available memory looking for the zero terminator. -- |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On May 29, 9:18am, bert <bert.hutchi...@btinternet.com> wrote:
> On 29 May, 13:54, kumar...@gmail.com wrote: > > > int main () > > { > > char *str = *Aamit ; > > *str='R' ; > > printf("%s \n " ,str); > > > } > > > it's giving segfault > > why ????? > > Apart from the problems other posters have > already pointed out, there is a major one > which they haven't :- > > printf() expects its (str) argument to > point to a zero-terminated string. Your > code modifies the first character which > str is pointing at, but does nothing about > the following ones. If they are all non-zero > junk, and you have fixed the other problems, > it is quite possible for printf() to run off > the end of available memory looking for the > zero terminator. > -- Maybe I'm not thinking this through enough, but I REALLY don't see the difference between m-net% more mod.c #include <stdio.h> int main (void) { char str[] = "Aamit"; *str='R'; printf("%s \n " ,str); return 0; } m-net% gcc -g -Wall mod.c -o mod m-net% ./mod Ramit % m-net% Versus something like m-net% more mod.c #include <stdio.h> int main (void) { char str[] = "Aamit"; *str='R'; puts(str); return 0; } m-net% gcc -g -Wall mod.c -o mod m-net% ./mod Ramit m-net% |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Chad wrote:
> .... snip ... > > Maybe I'm not thinking this through enough, but I REALLY don't > see the difference between > .... snip ... > > Versus something like > > #include <stdio.h> > int main (void) { > char str[] = "Aamit"; > *str='R'; > puts(str); > return 0; > } The difference is between: char *str = "Aamit"; /* 1 */ and char str[] = "Aamit"; /* 2 */ in /* 1 */ str is a char pointer to a non-modifiable string. In /* 2 */ str is a fully modifiable 6 char array, holding "Aamit\0". -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. ** Posted from http://www.teranews.com ** |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Chad wrote:
> Maybe I'm not thinking this through enough, but I REALLY don't see the > difference between > m-net% more mod.c > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > printf("%s \n " ,str); > return 0; > } > > m-net% gcc -g -Wall mod.c -o mod > m-net% ./mod > Ramit > > % > m-net% > > > Versus something like > m-net% more mod.c > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > puts(str); > return 0; > } > > m-net% gcc -g -Wall mod.c -o mod > m-net% ./mod > Ramit > m-net% The printf version, outputs a space character (' ') before the newline. -- pete |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
bert <bert.hutchings@btinternet.com> writes:
> On 29 May, 13:54, kumar...@gmail.com wrote: >> int main () >> { >> char *str = *Aamit ; >> *str='R' ; >> printf("%s \n " ,str); >> >> } >> >> it's giving segfault >> why ????? > > Apart from the problems other posters have > already pointed out, there is a major one > which they haven't :- > > printf() expects its (str) argument to > point to a zero-terminated string. Your > code modifies the first character which > str is pointing at, but does nothing about > the following ones. If they are all non-zero > junk, and you have fixed the other problems, > it is quite possible for printf() to run off > the end of available memory looking for the > zero terminator. The code as posted will not compile, so it clearly isn't the same code that the OP actually compiled and ran. I think you're making a different assumption than the rest of us about just how the posted code differs from the real code. Are you assuming that there's a declared object of type char** named ``Aamit''? I think the rest of us assumed that ``*Aamit'' should have been the string literal "Aamit". Given that assumption, *if* we ignore the fact that modifying the contents of a string literal invokes undefined behavior, then the array is already properly terminated with a '\0'. Unfortunately, the original poster has not seen fit to clear this up by posting his actual code. Until and unless he does so, I suggest there's no point in pursuing this further. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
CBFalconer wrote:
> Chad wrote: >> > ... snip ... >> >> Maybe I'm not thinking this through enough, but I REALLY don't >> see the difference between >> > ... snip ... >> >> Versus something like >> >> #include <stdio.h> >> int main (void) { >> char str[] = "Aamit"; >> *str='R'; >> puts(str); >> return 0; >> } > > The difference is between: > > char *str = "Aamit"; /* 1 */ > and > char str[] = "Aamit"; /* 2 */ > > in /* 1 */ str is a char pointer to a non-modifiable string. In /* > 2 */ str is a fully modifiable 6 char array, holding "Aamit\0". Read again. Chad was using char str[] in both cases, his only difference was printf vs. puts Bye, Jojo |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
Chad wrote:
> On May 29, 9:18 am, bert <bert.hutchi...@btinternet.com> wrote: >> On 29 May, 13:54, kumar...@gmail.com wrote: >> >>> int main () >>> { >>> char *str = *Aamit ; >>> *str='R' ; >>> printf("%s \n " ,str); >> >>> } >> >>> it's giving segfault >>> why ????? >> >> Apart from the problems other posters have >> already pointed out, there is a major one >> which they haven't :- >> >> printf() expects its (str) argument to >> point to a zero-terminated string. Your >> code modifies the first character which >> str is pointing at, but does nothing about >> the following ones. If they are all non-zero >> junk, and you have fixed the other problems, >> it is quite possible for printf() to run off >> the end of available memory looking for the >> zero terminator. >> -- > > > Maybe I'm not thinking this through enough, but I REALLY don't see the > difference between Then look again. > m-net% more mod.c > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > printf("%s \n " ,str); > return 0; > } > > m-net% gcc -g -Wall mod.c -o mod > m-net% ./mod > Ramit > See this linefeed? > % > m-net% > > > Versus something like > m-net% more mod.c > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > puts(str); > return 0; > } > > m-net% gcc -g -Wall mod.c -o mod > m-net% ./mod > Ramit Here it is not. > m-net% On top you got a space before and anotzher one after the newline, but these are 'invisible'. Bye, Jojo |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
Joachim Schmitz wrote:
> CBFalconer wrote: >> Chad wrote: >>> >> ... snip ... >>> >>> Maybe I'm not thinking this through enough, but I REALLY don't >>> see the difference between >>> >> ... snip ... >>> >>> Versus something like >>> >>> #include <stdio.h> >>> int main (void) { >>> char str[] = "Aamit"; >>> *str='R'; >>> puts(str); >>> return 0; >>> } >> >> The difference is between: >> >> char *str = "Aamit"; /* 1 */ >> and >> char str[] = "Aamit"; /* 2 */ >> >> in /* 1 */ str is a char pointer to a non-modifiable string. In /* >> 2 */ str is a fully modifiable 6 char array, holding "Aamit\0". > > Read again. Chad was using char str[] in both cases, his only > difference was printf vs. puts That was in his last manual copy of the original problem. The char* = "constant"; was the actual problem. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. ** Posted from http://www.teranews.com ** |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
CBFalconer wrote:
> Joachim Schmitz wrote: >> CBFalconer wrote: >>> Chad wrote: >>>> >>> ... snip ... >>>> >>>> Maybe I'm not thinking this through enough, but I REALLY don't >>>> see the difference between >>>> >>> ... snip ... >>>> >>>> Versus something like >>>> >>>> #include <stdio.h> >>>> int main (void) { >>>> char str[] = "Aamit"; >>>> *str='R'; >>>> puts(str); >>>> return 0; >>>> } >>> >>> The difference is between: >>> >>> char *str = "Aamit"; /* 1 */ >>> and >>> char str[] = "Aamit"; /* 2 */ >>> >>> in /* 1 */ str is a char pointer to a non-modifiable string. In /* >>> 2 */ str is a fully modifiable 6 char array, holding "Aamit\0". >> >> Read again. Chad was using char str[] in both cases, his only >> difference was printf vs. puts > > That was in his last manual copy of the original problem. The > > char* = "constant"; > > was the actual problem. the *OP's* actual problem, but not Chad's problem. |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
Joachim Schmitz wrote:
> Chad wrote: >> Maybe I'm not thinking this through enough, but I REALLY don't see the >> difference between > Then look again. > >> m-net% more mod.c >> #include <stdio.h> >> >> int main (void) >> { >> char str[] = "Aamit"; >> *str='R'; >> printf("%s \n " ,str); >> return 0; >> } >> >> m-net% gcc -g -Wall mod.c -o mod >> m-net% ./mod >> Ramit >> > See this linefeed? > >> % >> m-net% >> >> >> Versus something like >> m-net% more mod.c >> #include <stdio.h> >> >> int main (void) >> { >> char str[] = "Aamit"; >> *str='R'; >> puts(str); >> return 0; >> } >> >> m-net% gcc -g -Wall mod.c -o mod >> m-net% ./mod >> Ramit > Here it is not. > >> m-net% > > On top you got a space before and anotzher one after the newline, but these > are 'invisible'. I think the printf version is stylistically a little bit worse than that. N869 7.19.2 Streams [#2] Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line; no new-line character is immediately preceded by space characters; and the last character is a new-line character. Whether space characters that are written out immediately before a new-line character appear when read in is implementation-defined. -- pete |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
pete wrote:
> Joachim Schmitz wrote: >> Chad wrote: > >>> Maybe I'm not thinking this through enough, but I REALLY don't see >>> the difference between >> Then look again. >> >>> m-net% more mod.c >>> #include <stdio.h> >>> >>> int main (void) >>> { >>> char str[] = "Aamit"; >>> *str='R'; >>> printf("%s \n " ,str); >>> return 0; >>> } >>> >>> m-net% gcc -g -Wall mod.c -o mod >>> m-net% ./mod >>> Ramit >>> >> See this linefeed? >> >>> % >>> m-net% >>> >>> >>> Versus something like >>> m-net% more mod.c >>> #include <stdio.h> >>> >>> int main (void) >>> { >>> char str[] = "Aamit"; >>> *str='R'; >>> puts(str); >>> return 0; >>> } >>> >>> m-net% gcc -g -Wall mod.c -o mod >>> m-net% ./mod >>> Ramit >> Here it is not. >> >>> m-net% >> >> On top you got a space before and anotzher one after the newline, >> but these are 'invisible'. > > I think the printf version is stylistically > a little bit worse than that. > > N869 > 7.19.2 Streams > > [#2] > Data read in from a text > stream will necessarily compare equal to the data that were > earlier written out to that stream only if: the data consist > only of printing characters and the control characters > horizontal tab and new-line; no new-line character is > immediately preceded by space characters; and the last > character is a new-line character. > > Whether space characters > that are written out immediately before a new-line character > appear when read in is implementation-defined. Guess that reply should have gone to Chad? Bye, Jojo |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
On May 30, 7:46 am, pete <pfil...@mindspring.com> wrote:
> Joachim Schmitz wrote: > > Chad wrote: > >> Maybe I'm not thinking this through enough, but I REALLY don't see the > >> difference between > > Then look again. > > >> m-net% more mod.c > >> #include <stdio.h> > > >> int main (void) > >> { > >> char str[] = "Aamit"; > >> *str='R'; > >> printf("%s \n " ,str); > >> return 0; > >> } > > >> m-net% gcc -g -Wall mod.c -o mod > >> m-net% ./mod > >> Ramit > > > See this linefeed? > > >> % > >> m-net% > > >> Versus something like > >> m-net% more mod.c > >> #include <stdio.h> > > >> int main (void) > >> { > >> char str[] = "Aamit"; > >> *str='R'; > >> puts(str); > >> return 0; > >> } > > >> m-net% gcc -g -Wall mod.c -o mod > >> m-net% ./mod > >> Ramit > > Here it is not. > > >> m-net% > > > On top you got a space before and anotzher one after the newline, but these > > are 'invisible'. > > I think the printf version is stylistically > a little bit worse than that. > > N869 > 7.19.2 Streams > > [#2] > Data read in from a text > stream will necessarily compare equal to the data that were > earlier written out to that stream only if: the data consist > only of printing characters and the control characters > horizontal tab and new-line; no new-line character is > immediately preceded by space characters; and the last > character is a new-line character. > > Whether space characters > that are written out immediately before a new-line character > appear when read in is implementation-defined. > > -- > pete It appears that I don't get undefined behavior when I remove the space before and after '\n' in printf() m-net% more mod.c #include <stdio.h> int main (void) { char str[] = "Aamit"; *str='R'; printf("%s\n" ,str); return 0; } m-net% gcc -g -Wall mod.c -o mod m-net% ./mod Ramit m-net% |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
Chad <cdalten@gmail.com> writes:
[...] > It appears that I don't get undefined behavior when I remove the space > before and after '\n' in printf() > > m-net% more mod.c > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > printf("%s\n" ,str); > return 0; > } [...] The space before or after '\n' has nothing to do with whether the behavior is undefined. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
Chad wrote:
> On May 30, 7:46 am, pete <pfil...@mindspring.com> wrote: >> Joachim Schmitz wrote: >>> Chad wrote: >>>> #include <stdio.h> >>>> int main (void) >>>> { >>>> char str[] = "Aamit"; >>>> *str='R'; >>>> printf("%s \n " ,str); >>>> return 0; >>>> } >>> On top you got a space before and anotzher one after the newline, but these >>> are 'invisible'. >> I think the printf version is stylistically >> a little bit worse than that. >> >> N869 >> 7.19.2 Streams >> >> [#2] >> Data read in from a text >> stream will necessarily compare equal to the data that were >> earlier written out to that stream only if: the data consist >> only of printing characters and the control characters >> horizontal tab and new-line; no new-line character is >> immediately preceded by space characters; and the last >> character is a new-line character. >> >> Whether space characters >> that are written out immediately before a new-line character >> appear when read in is implementation-defined. > It appears that I don't get undefined behavior when I remove the space > before and after '\n' in printf() Yes. > #include <stdio.h> > > int main (void) > { > char str[] = "Aamit"; > *str='R'; > printf("%s\n" ,str); > return 0; > } That is an example of a "correct program" [#3] and also of a "strictly conforming program". [#5] N869 4. Conformance [#1] In this International Standard, ``shall'' is to be interpreted as a requirement on an implementation or on a program; conversely, ``shall not'' is to be interpreted as a prohibition. [#2] If a ``shall'' or ``shall not'' requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ``undefined behavior'' or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ``behavior that is undefined''. [#3] A program that is correct in all other aspects, operating on correct data, containing unspecified behavior shall be a correct program and act in accordance with 5.1.2.3. [#5] A strictly conforming program shall use only those features of the language and library specified in this International Standard.2) It shall not produce output dependent on any unspecified, undefined, or implementation- defined behavior, and shall not exceed any minimum implementation limit. -- pete |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
Keith Thompson wrote:
> Chad <cdalten@gmail.com> writes: > [...] >> It appears that I don't get undefined behavior when I remove the space >> before and after '\n' in printf() >> >> m-net% more mod.c >> #include <stdio.h> >> >> int main (void) >> { >> char str[] = "Aamit"; >> *str='R'; >> printf("%s\n" ,str); >> return 0; >> } > [...] > > The space before or after '\n' has nothing to do with whether the > behavior is undefined. According to some interpretations of the standard, the removed space, which had previously been placed after the '\n', might have had something to with it on an implementation that requires a terminating new-line character on the last line. N869 7.19.2 Streams [#2] Whether the last line requires a terminating new-line character is implementation-defined. -- pete |
|
![]() |
| Outils de la discussion | |
|
|