K
kumarvis
int main ()
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}
it's giving segfault
why ?????
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}
it's giving segfault
why ?????
If you meant to writeint main ()
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}
it's giving segfault
why ?????
pete said:int main(void)
{
char *str = "Aamit" ;
*str = 'R' ;
puts(str);
return 0;
}
int main ()
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}
it's giving segfault
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.
--
bert said: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.
Read again. Chad was using char str[] in both cases, his only difference wasCBFalconer said:Chad said:... 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".
Then look again.Chad said:Maybe I'm not thinking this through enough, but I REALLY don't see the
difference between
See this linefeed?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
Here it is not.%
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 *OP's* actual problem, but not Chad's problem.CBFalconer said:Joachim said:CBFalconer said: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.
Guess that reply should have gone to Chad?pete said:Joachim said:Chad wrote:Then look again.Maybe I'm not thinking this through enough, but I REALLY don't see
the difference between
See this linefeed?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
Here it is not.%
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%
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.
Joachim said:Chad said: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
RamitSee 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.
[...]Chad said: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;
}
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.