int main ()
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}
it's giving segfault
why ????? 21 1357 ku******@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 ku******@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 ku******@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.hresp. the resulting lack of a prototype for the varadic function
printf().
Bye, Jojo
pete wrote:
ku******@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
pete <pf*****@mindspring.comwrites:
ku******@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) ks***@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" ku******@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) ks***@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"
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.
--
On May 29, 9:18*am, bert <bert.hutchi...@btinternet.comwrote:
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%
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
bert <be************@btinternet.comwrites:
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) ks***@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"
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 **
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
Chad wrote:
On May 29, 9:18 am, bert <bert.hutchi...@btinternet.comwrote:
>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
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 **
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.
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
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
On May 30, 7:46 am, pete <pfil...@mindspring.comwrote:
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%
Chad <cd*****@gmail.comwrites:
[...]
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) ks***@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"
Chad wrote:
On May 30, 7:46 am, pete <pfil...@mindspring.comwrote:
>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
Keith Thompson wrote:
Chad <cd*****@gmail.comwrites:
[...]
>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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int...
|
by: AMT2K5 |
last post by:
Hello guys.
I have a function, cleanSpace . I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Matt S |
last post by:
Hello,
I'm trying to build a C# client to consume an AXIS Web Service (running SOAP
over HTTP). The Web Service encodes
full server-side exception traces in the Soap Fault > Detail element...
|
by: utab |
last post by:
Hi there,
I am trying to read from a file and at the same time change certain
fields of the same field, there are 6 fields in this file like
1 2 3 4 5 6...
|
by: WaterWalk |
last post by:
Hello, I'm currently learning string manipulation. I'm curious about
what is the favored way for string manipulation in C, expecially when
strings contain non-ASCII characters. For example, if...
|
by: tikcireviva |
last post by:
Hi Guys,
I am not sure what's wrong with my program. When I ran it under the
GDB, it shows the following messages , which said that the issue is
from stl_list.h. Does anyone know what's...
|
by: Vlad Dogaru |
last post by:
Hello,
I am trying to learn C, especially pointers. The following code
attempts to count the appearences of each word in a text file, but
fails invariably with Segmentation Fault. Please help me...
|
by: Equinex |
last post by:
Hi,
I am trying to call a Web Service using Php and Soap.
Why does the following Php 5 code return?
try
{
//$ExchangeLoginResult = $ExchangeClient->Login(array("request" =>
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |