Dear all,
I'd like to ask for the following C code segment:
#include <stdio.h>
int main()
{
char *a = "abc"; /* Line 1 */
printf("%s", a); /* Line 2 */
a[0] = '1'; /* Line 3 */
return 0;
}
The code compiles without warnings and errors,
runs ok until line 3 with "segmentation fault".
However, if Line 1 is changed to:
char a[] = "abc";
The program runs successfully. My question is:
Why char *a = "abc" is allowed, but then a[0] = '1'
will cause a segmentation fault?
Also, if Line 1 is changed to:
char *a = {'a', 'b', 'c', '\0'};
The program has compilation warnings, and the
execution will give segmentation fault on Line 2.
Why this form of initialization is not allowed?
Thanks!
Best Regards,
Benny (LaBird),
email: Remove all numerals to get the valid address. 5 2279
By standard (I think) if you have string char*="asdasda" it can be placed
in constant section of program, so changing that data will produce
segmentation. Some compilers put it even in code section.
char a[] = "asdasd" mean that "a" is array of chars finished by
null-termin. So, this string must not be in const section.
In
char *a = {'a', 'b', 'c', '\0'}
a is so-colled scalar and initialization of scalar object is only allowd
with one element.
I think that all gcc compilers move string to constant section (even
MinGW). With Win Borland C++, VC++ this is not case.
LaBird wrote:
char *a = "abc"; /* Line 1 */
In the above line, the string literal "abc",
converts to a pointer to a string. The pointer a,
is initialized with that value.
(sizeof a == sizeof &"abc")
Attempting to overwrite that string is undefined behavior.
Other references to "abc" in the same program,
may refer to the same string or different strings.
char a[] = "abc";
In the above line, a is array of 4 char which is initialized
with the values of 'a', 'b', 'c' and '\0'.
(sizeof a == sizeof "abc")
char *a = {'a', 'b', 'c', '\0'};
That doesn't mean anything in C.
--
pete
On Thu, 14 Oct 2004 19:37:40 +0800
"LaBird" <wl**********@hkucs2004.org> wrote: Dear all,
I'd like to ask for the following C code segment:
#include <stdio.h> int main() { char *a = "abc"; /* Line 1 */ printf("%s", a); /* Line 2 */ a[0] = '1'; /* Line 3 */ return 0; }
The code compiles without warnings and errors, runs ok until line 3 with "segmentation fault". However, if Line 1 is changed to:
char a[] = "abc";
The program runs successfully. My question is: Why char *a = "abc" is allowed, but then a[0] = '1' will cause a segmentation fault?
<snip>
We've only just been discussing an almost identical question. The simple
answer is DON'T MODIFY STRING LITERALS. Also, your two definitions of a
are completely different, one is a pointer the other is an array.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
pete wrote: LaBird wrote:
char *a = "abc"; /* Line 1 */
In the above line, the string literal "abc", converts to a pointer to a string. The pointer a, is initialized with that value. (sizeof a == sizeof &"abc")
I think that should be:
(sizeof a == sizeof &*"abc")
--
pete
On Thu, 14 Oct 2004 19:37:40 +0800, "LaBird"
<wl**********@hkucs2004.org> wrote:
[writing into string literal segfault]
FAQ 1.32 at the usual places and http://www.eskimo.com/~scs/C-faq/top.html
- David.Thompson1 at worldnet.att.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Hunter Hou |
last post by:
Hi,
I have this program, compiling is passed, but when i wanted to get the
executable file(g++ -o output input.o), it fails.
Who can lead me where the error is?
Thanks,
Hunter
===
|
by: Rafi Kfir |
last post by:
Hi,
This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.
All I want is that one function passes a two dimensional array of
strings to another...
|
by: Chris Mantoulidis |
last post by:
I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.
Here's my question......
|
by: RAB |
last post by:
I am programming in the palm environment using C++ and am stuck. I
have created an char array:
const char MyArray =
{
{"string1"},
{"string2"},
....
{"string50"}
}
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
| |
by: arnuld |
last post by:
int main()
{
const char* arr = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;
}
this gives an error:
$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot...
|
by: copx |
last post by:
Unforuntately, I know next to nothing about ASM and compiler construction,
and while I was aware of the syntactic differences between pointers and
arrays, I was not aware of this:
...
|
by: Peithon |
last post by:
Hi,
I'm initialising unit test structures using a macro as follows:
#define UTMACRO(x) UT##x
typedef struct
{
char * key;
char * arr;
|
by: mosullivan |
last post by:
I had to write a program that would accept 7 strings through scanf, list the strings, alphabetize, and relist. I was supposed to use strcmp to assist with the sort and write it so that it can sort...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |