473,378 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

tricky array problem !

#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
27 2276
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
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
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
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
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
"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
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
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

"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

"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
"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
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
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
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
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
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
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
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
"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
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
1
by: Tim | last post by:
Hello, I'm extremely puzzled; I cannot figure out what I'm doing wrong. Here's the situation. I would really appreciate any suggestions. I'm modifying a shopping cart in the following way....
5
by: Danny | last post by:
Hi there I need help with a tricky problem. I have a 2 dimensional array with qualities such as ball size, ball color, ball weight. Now I have to print out all the possible combinations of...
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
2
by: Mark Sandfox | last post by:
I have a tricky control validation issue. I have probably designed this the wrong way but here is what I have. I have 6 TextBoxes; tbPN, tbA, tbC, tbS, tbZ, tbDOB and there are 20 of each with...
5
by: Sebastian | last post by:
Here's the thing I have a web aplication (1.1) and one of my classes has an array as a property (let's say Class A, has an ArrayList of Bs). On the other side I have a win form app which uses web...
2
by: torpecool | last post by:
Hello Everyone, I have a situation that I just cannot wrap my head around. I would appreciate any ideas or suggestions. I am trying to parse and load a tab delimited file into a mysql...
3
by: jamesgoode | last post by:
Hi, I've got a complicated problem to solve. Say I have an indexed array $array, with contents as follows: 'name' ='James' 'favefood' ='Spagbol' 'os' ='Debian GNU/Linux' And say I want...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
1
by: claudfs | last post by:
Hey Guys, Ok here is my problem: There is around 350 rows in the db. All the variables $actor_id, $comment_id and $likes_id are all uid's for facebook users Now i want to group and sum...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.