棘手的阵列问题! [英] tricky array problem !

查看:59
本文介绍了棘手的阵列问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

int main(void){

char a [] =" abcde";

char * p = a;

p ++;

p ++;

p [2] =''z'';

printf("%s \ n",p);

返回0;

}


输出:

cdz


ok,


#include< stdio.h>

int main(void){

char * p =" abcde";

p [2] =''z'';

printf("%s \ n",p);

返回0;

}


输出:

分段错误


在这两种情况下,P都是char *类型,而不是一种情况下z可以改变b
和其他情况z cnt改变为什么?请解释..

#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 ..

推荐答案

onkar写道:
onkar wrote:

#include< ; stdio.h>

int main(void){

char a [] =" abcde";

char * p = a ;

p ++;

p ++;

p [2] =''z'';

printf(" ;%s \ n",p);

返回0;

}


输出:

cdz


ok,


#include< stdio.h>

int main(void ){

char * p =" abcde" ;;
#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";



这是一个字符串文字,在常见问题中查找,或许多线程

提出这个问题。


-

Ian Collins。

This is a string literal, look it up in the FAQ, or the many threads
asking this question.

--
Ian Collins.


onkar说:
onkar said:

#include< stdio.h>

int main(void){

char a [] =" abcde" ;;
#include<stdio.h>
int main(void){
char a[]="abcde";



你拥有数组''a''。

You own the array ''a''.


char * p = a;

p ++;

p ++;

p [2] =''z'';
char *p=a;
p++;
p++;
p[2]=''z'';



你改变数组''a''。没问题。

You change the array ''a''. No problem.


char * p =" abcde" ;;
char *p="abcde";



你没有字符串文字abcde。

You don''t own the string literal "abcde".


p [2] = '' Z '';
p[2]=''z'';



您尝试更改它。问题。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

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.


您好,

#include< stdio.h>

int main(void){

char a [] =" abcde";

char * p = a;

p ++;

p ++;

p [2] =''z'';

printf("%s \ n",p);

返回0;

}


输出:

cdz
#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



数组a放置到堆栈,并初始化为abcde。

您可以使用该数组执行任何操作(前提是索引

在容差范围内) )。

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);

返回0;

}


输出:

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

output:
Segmentation fault



你有一个指向字符串文字的指针,它最终存储在

只读段中。在这种情况下,尝试修改内容导致

a SIGSEGV。


HTH,

Loic。

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.


这篇关于棘手的阵列问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆