By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

tricky array problem !

P: n/a
#include<stdio.h>
int main(void){
char a[]="abcde";
char *p=a;
p++;
p++;
p[2]='z';
printf("%s\n",p);
return 0;
}

output :
cdz

ok than ,

#include<stdio.h>
int main(void){
char *p="abcde";
p[2]='z';
printf("%s\n",p);
return 0;
}

output:
Segmentation fault

In both cases P is of the type char * than in one case the z can
change and the othe csae z cnt change Why ?? Please explain ..

Dec 15 '06 #1
Share this Question
Share on Google+
27 Replies


P: n/a
onkar wrote:
#include<stdio.h>
int main(void){
char a[]="abcde";
char *p=a;
p++;
p++;
p[2]='z';
printf("%s\n",p);
return 0;
}

output :
cdz

ok than ,

#include<stdio.h>
int main(void){
char *p="abcde";
This is a string literal, look it up in the FAQ, or the many threads
asking this question.

--
Ian Collins.
Dec 15 '06 #2

P: n/a
onkar said:
#include<stdio.h>
int main(void){
char a[]="abcde";
You own the array 'a'.
char *p=a;
p++;
p++;
p[2]='z';
You change the array 'a'. No problem.
char *p="abcde";
You don't own the string literal "abcde".
p[2]='z';
You try to change it. Problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #3

P: n/a
Hello,
#include<stdio.h>
int main(void){
char a[]="abcde";
char *p=a;
p++;
p++;
p[2]='z';
printf("%s\n",p);
return 0;
}

output :
cdz
The array "a" is placed to the stack, and is initialized to "abcde".
You can do whatever you want with that array (provided that the index
is within the tolerated range).
#include<stdio.h>
int main(void){
char *p="abcde";
p[2]='z';
printf("%s\n",p);
return 0;
}

output:
Segmentation fault
You have a pointer to a string literal, which is stored eventually to a
read-only segment. In this case, trying to modify the content leads to
a SIGSEGV.

HTH,
Loic.

Dec 15 '06 #4

P: n/a
lo******@gmx.net wrote:
char a[]="abcde";
The array "a" is placed to the stack, and is initialized to "abcde".
It has automatic storage duration; automatic storage is often
implemented as a stack, but need not be.
char *p="abcde";
You have a pointer to a string literal, which is stored eventually to a
read-only segment.
It may very well be stored in read-only memory, and certainly appears
to be based on the behavior reported by the OP, but again, such need
not be the case.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Dec 15 '06 #5

P: n/a
Hi Chris,
char a[]="abcde";
The array "a" is placed to the stack, and is initialized to "abcde".

It has automatic storage duration; automatic storage is often
implemented as a stack, but need not be.
I am a system programmer, not a C standard guy... Just out of
curiosity, could you give an example of OS/compiler which does not
implement automatic variable as a stack? And what would be the
implementation then?
char *p="abcde";
You have a pointer to a string literal, which is stored eventually to a
read-only segment.

It may very well be stored in read-only memory, and certainly appears
to be based on the behavior reported by the OP, but again, such need
not be the case.
Absolutely. I guess that modifying a string literal is an "undefined
behavior" accordingly to the C standard, right?

Cheers,
Loic.

Dec 15 '06 #6

P: n/a
"onkar" <on*******@gmail.comwrites:
#include<stdio.h>
int main(void){
char a[]="abcde";
char *p=a;
p++;
p++;
p[2]='z';
printf("%s\n",p);
return 0;
}

output :
cdz

ok than ,

#include<stdio.h>
int main(void){
char *p="abcde";
p[2]='z';
printf("%s\n",p);
return 0;
}

output:
Segmentation fault
The comp.lang.c FAQ is at <http://www.c-faq.com/>. You have asked
question 1.32.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '06 #7

P: n/a
lo******@gmx.net said:
Hi Chris,
char a[]="abcde";
The array "a" is placed to the stack, and is initialized to "abcde".

It has automatic storage duration; automatic storage is often
implemented as a stack, but need not be.

I am a system programmer, not a C standard guy... Just out of
curiosity, could you give an example of OS/compiler which does not
implement automatic variable as a stack? And what would be the
implementation then?
IIRC the cc51 compiler for the 8051 will sometimes place automatic objects
in registers.

<snip>
I guess that modifying a string literal is an "undefined
behavior" accordingly to the C standard, right?
Correct.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #8

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
lo******@gmx.net said:
> char a[]="abcde";

The array "a" is placed to the stack, and is initialized to "abcde".

It has automatic storage duration; automatic storage is often
implemented as a stack, but need not be.

I am a system programmer, not a C standard guy... Just out of
curiosity, could you give an example of OS/compiler which does not
implement automatic variable as a stack? And what would be the
implementation then?

IIRC the cc51 compiler for the 8051 will sometimes place automatic objects
in registers.
I think most compilers do that kind of thing; it's a relatively
straightforward optimization.

I think there is (or was?) some IBM mainframe system which doesn't
have a system stack in the usual sense; what would otherwise be a
stack frame for a function call is allocated on the heap. I'm sure
someone here has the details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '06 #9

P: n/a

"Richard Heathfield д
"
onkar said:
#include<stdio.h>
int main(void){
char a[]="abcde";

You own the array 'a'.
char *p=a;
p++;
p++;
p[2]='z';

You change the array 'a'. No problem.
char *p="abcde";f
when you use " char * p " ,the pointer is wild. that's the problem.
>
You don't own the string literal "abcde".
p[2]='z';

You try to change it. Problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 16 '06 #10

P: n/a

"onkar д
"
#include<stdio.h>
int main(void){
char a[]="abcde";
' a ' is a pointer of initiated stack.
char *p=a;
' p ' point the same location in memory
p++;
p++;
p[2]='z';
printf("%s\n",p);
return 0;
}

output :
cdz

ok than ,

#include<stdio.h>
int main(void){
char *p="abcde";
but ' p ' in here point to a random location( may be read-only
segement)
p[2]='z';
printf("%s\n",p);
return 0;
}

output:
Segmentation fault

In both cases P is of the type char * than in one case the z can
change and the othe csae z cnt change Why ?? Please explain ..
Dec 16 '06 #11

P: n/a
"queniao" <pa**********@gmail.comwrites:
"onkar д
"
[...]
>#include<stdio.h>
int main(void){
char *p="abcde";
but ' p ' in here point to a random location( may be read-only
segement)
No, "p" does not point to a random location. It points to the first
character of a string literal, consisting of the character "abcde"
followed by a '\0' character. For example, reading the value of *p is
well defined; it yields the value 'a'.

The problem, as several people have said, is that attempting to modify
a string literal invokes undefined behavior. If you're going to
initialize or assign a pointer to point into a string literal, it's
best to declare it as a pointer to const char:

const char *p = "abcde";

Then the compiler will (in most cases) detect any attempt to modify
the string literal.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '06 #12

P: n/a
On 15 Dec 2006 18:08:21 -0800, "queniao" <pa**********@gmail.com>
wrote in comp.lang.c:
>
"Richard Heathfield д
"
onkar said:
#include<stdio.h>
int main(void){
char a[]="abcde";
You own the array 'a'.
char *p=a;
p++;
p++;
p[2]='z';
You change the array 'a'. No problem.
char *p="abcde";f

when you use " char * p " ,the pointer is wild. that's the problem.
Either your command of the English language is very poor, or you do
not know what you are talking about at all.

There is no such thing as a "wild pointer" in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 16 '06 #13

P: n/a
queniao said:
"Richard Heathfield" [wrote]:
>onkar said:
<snip>
char *p="abcde";f

when you use " char * p " ,the pointer is wild. that's the problem.
No, not really. It ought really to be const char *, but there's nothing
terribly wrong with pointing a char * at a string literal as long as you
don't write through that pointer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 16 '06 #14

P: n/a
On 15 Dec 2006 13:43:08 -0800, in comp.lang.c , lo******@gmx.net
wrote:
>I am a system programmer, not a C standard guy... Just out of
curiosity, could you give an example of OS/compiler which does not
implement automatic variable as a stack? And what would be the
implementation then?
Most implementations will put automatics that are small enough into a
register. Its a common optimisation.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 16 '06 #15

P: n/a
On 15 Dec 2006 18:13:59 -0800, in comp.lang.c , "queniao"
<pa**********@gmail.comwrote:
>"onkar ??????
"
> char *p="abcde";
but ' p ' in here point to a random location
No, its a pointer to a string literal "abcde", which has a
well-defined location.
>( may be read-only segement)
possibly, but not necessarily. Many implementations place it in
writable memory. Its still illegal to write to it.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 16 '06 #16

P: n/a
In article <kp********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>On 15 Dec 2006 13:43:08 -0800, in comp.lang.c , lo******@gmx.net
wrote:
>>I am a system programmer, not a C standard guy... Just out of
curiosity, could you give an example of OS/compiler which does not
implement automatic variable as a stack? And what would be the
implementation then?

Most implementations will put automatics that are small enough into a
register. Its a common optimisation.
We could save ourselves a lot of pain and repetitive argumentation if we
just took it that the word(?) "stack" is just techy, hardware geeky speak
for "automatic storage". I.e., when someone mentions "stack", just
pretend they said "automatic storage" and everybody is happy.

Similarly, when someone mentions "bss", just pretend they said "global (*)
storage" and everybody is happy.

(*) Or whatever the standard-ese is for this.
Dec 16 '06 #17

P: n/a
Jack Klein wrote:
There is no such thing as a "wild pointer" in C.
That term is often used for a pointer with indeterminate value.

Dec 17 '06 #18

P: n/a
On 17 Dec 2006 15:42:09 -0800, in comp.lang.c , "Old Wolf"
<ol*****@inspire.net.nzwrote:
>Jack Klein wrote:
>There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.
Hm? I can honestly say that in nearly 20 years of programming in C and
C++ I have __NEVER__ heard anyone use the term "wild pointer" for an
indeterminate or uninitialised pointer. Not once.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 18 '06 #19

P: n/a
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jack Klein wrote:
>There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.
It is? Not in the real world. I have never heard it in relation to
any language whatsoever.

"Uninitialized pointer" is nearly universal afaik.
Dec 18 '06 #20

P: n/a
Richard wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jack Klein wrote:
There is no such thing as a "wild pointer" in C.
That term is often used for a pointer with indeterminate value.

It is? Not in the real world. I have never heard it in relation to
any language whatsoever.
I hear it regularly. Must be a regional thing :)

Dec 18 '06 #21

P: n/a
Old Wolf wrote:
>
Richard wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jack Klein wrote:
>There is no such thing as a "wild pointer" in C.
>
That term is often used for a pointer with indeterminate value.
>
It is? Not in the real world. I have never heard it in relation to
any language whatsoever.

I hear it regularly. Must be a regional thing :)
http://en.wikipedia.org/wiki/Dangling_pointer

--
pete
Dec 18 '06 #22

P: n/a
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jack Klein wrote:
>There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.
I agree (and Google will show you a number of real-life uses of the
phrase).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 18 '06 #23

P: n/a
Mark McIntyre wrote:
On 17 Dec 2006 15:42:09 -0800, in comp.lang.c , "Old Wolf"
<ol*****@inspire.net.nzwrote:
Jack Klein wrote:
There is no such thing as a "wild pointer" in C.
That term is often used for a pointer with indeterminate value.

Hm? I can honestly say that in nearly 20 years of programming in C and
C++ I have NEVER heard anyone use the term "wild pointer" for an
indeterminate or uninitialised pointer. Not once.
Odd, because there are numerous prior usages of the term in this very
newsgroup.

Here are a few exerpts:

From: Richard Bos
Date: Thurs, Oct 19 2006 2:48 am

The solution is to go over all your allocation and pointer handling code
to make sure you don't scribble through a wild pointer . . .
From: Eric Sosman
Date: Fri, Jul 21 2006 2:05 pm

.. . .
stomped on the memory-manager's data structures with a
wild pointer or crazy array index, or something of that
sort.
From: CBFalconer - view profile
Date: Fri, Jul 8 2005 10:50 am

.. . .
(with suitable allowances for the ends of the list). My nmalloc
mechanism for DJGPP uses this sort of thing as self protection, and
it reduces the chance of a wild pointer destroying the entire
malloc arena.


Brian
Dec 18 '06 #24

P: n/a
Mark McIntyre said:
On 17 Dec 2006 15:42:09 -0800, in comp.lang.c , "Old Wolf"
<ol*****@inspire.net.nzwrote:
>>Jack Klein wrote:
>>There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.

Hm? I can honestly say that in nearly 20 years of programming in C and
C++ I have __NEVER__ heard anyone use the term "wild pointer" for an
indeterminate or uninitialised pointer. Not once.
I've heard it quite a bit. Even used it once or twice myself, according to
Google. But Jack's right, in the sense that the Standard does not use the
term.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 18 '06 #25

P: n/a
2006-12-17 <11**********************@f1g2000cwa.googlegroups. com>,
Old Wolf wrote:
Jack Klein wrote:
>There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.
In which case the OP's usage was false. (or, more likely, he
misunderstood what x = "stringliteral" does.)
Dec 18 '06 #26

P: n/a
Random832 <ra****@random.yi.orgwrote:
2006-12-17 <11**********************@f1g2000cwa.googlegroups. com>,
Old Wolf wrote:
Jack Klein wrote:
There is no such thing as a "wild pointer" in C.
That term is often used for a pointer with indeterminate value.

In which case the OP's usage was false. (or, more likely, he
misunderstood what x = "stringliteral" does.)
Not the OP(onkar)'s, but queniao's. But yes. The pointer in the OP's
code was not a wild pointer, it just pointed to a non-modifiable string
literal.

Richard
Dec 18 '06 #27

P: n/a
2006-12-18 <45*****************@news.xs4all.nl>,
Richard Bos wrote:
Random832 <ra****@random.yi.orgwrote:
>2006-12-17 <11**********************@f1g2000cwa.googlegroups. com>,
Old Wolf wrote:
Jack Klein wrote:
There is no such thing as a "wild pointer" in C.

That term is often used for a pointer with indeterminate value.

In which case the OP's usage was false. (or, more likely, he
misunderstood what x = "stringliteral" does.)

Not the OP(onkar)'s, but queniao's. But yes. The pointer in the OP's
code was not a wild pointer, it just pointed to a non-modifiable string
literal.
I think the misunderstanding may have actually been a belief that
var="string" operates as a strcpy(). It's a common misconception among
people who don't understand C, though it usually comes up in a different
context.

Dec 18 '06 #28

This discussion thread is closed

Replies have been disabled for this discussion.