char * ptr =" amit",存储它的内存区域(堆,堆栈,代码,全局) [英] char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)
问题描述
char * str1 =" amit";
char str2 [] =" mehta"
strcat(str1,str2);
它会崩溃,我觉得str1会存储在代码部分。一旦分配了内存
,你就无法更改或附加到此内容。
请纠正我如果我错了..
Amit写道:
char * str1 =" amit" ;;
char str2 [] = mehta
strcat(str1,str2);
它会崩溃,我觉得str1会存储在代码段中。一旦分配了内存,就无法更改或附加到此内容。
请纠正我如果我错了..
你很接近。
C的规则说
试图写一个由字符串文字标识的对象,
导致未定义的行为。
str1指向由字符串文字标识的对象。
此外,您需要确保有足够的空间来进行strcat。
char * str1 =" amit";
char str2 [sizeof" amit" - 1 + sizeof" mehta"] =" mehta";
strcat(str2,str1);
-
pete
Amit写道:char * str1 =" amit" ;;
char str2 [] =" mehta"
strcat(str1,str2);
它会崩溃,我觉得str1会存储在代码段中。一旦分配了内存,你就无法改变或追加到它。
请纠正我如果我错了..
我觉得它是错了,因为
str1的地址值为amit
u必须strcat(* str1,str2)
>
venkatesh写道:
Amit写道:char * str1 =" amit" ;;
char str2 [] =" mehta"
strcat(str1,str2);
它会崩溃,我觉得str1会存储在代码部分。
一旦分配了内存,你就不能改变或追加到这个。
请纠正我如果我错了..
我认为这是错的,因为
str1的地址是值amit
你必须strcat(* str1,str2)
让你自己去编译器并尝试你的想法。
你错了。
* str1是char类型的表达式,其值为'a'',
引用不可写对象中的一个字节。
strcat的第一个参数必须是指向字符串的指针
在一个可写对象中,有足够的空间来生成
连接字符串。
-
pete
char *str1="amit";
char str2[]="mehta"
strcat(str1,str2);
It will crash, I feel str1 will be stored in code section. Once memory
is allocated, you cannot change or append into this.
Please correct me If I am wrong..
Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str2);
It will crash, I feel str1 will be stored in code section. Once memory
is allocated, you cannot change or append into this.
Please correct me If I am wrong..
You''re close.
The rules of C say that
an attempt to write to an object identified by a string literal,
causes undefined behavior.
str1 points to an object identified by a string literal.
Also, you need to make sure you have enough room for strcat.
char *str1 = "amit";
char str2[sizeof "amit" - 1 + sizeof "mehta"] = "mehta";
strcat(str2, str1);
--
pete
Amit wrote:char *str1="amit";
char str2[]="mehta"
strcat(str1,str2);
It will crash, I feel str1 will be stored in code section. Once memory
is allocated, you cannot change or append into this.
Please correct me If I am wrong..
I thing it is wrong because
str1 has the address of the value amit
u have to strcat(*str1,str2)
venkatesh wrote:
Amit wrote:char *str1="amit";
char str2[]="mehta"
strcat(str1,str2);
It will crash, I feel str1 will be stored in code section.
Once memory
is allocated, you cannot change or append into this.
Please correct me If I am wrong..
I thing it is wrong because
str1 has the address of the value amit
u have to strcat(*str1,str2)
Get yourself to a compiler and try your idea.
You''re very wrong.
*str1 is an expression of type char with a value of ''a'',
refering to a byte in a nonwriteable object.
The first argument to strcat must be a pointer to a string
in a writable object that has enough space for the resulting
concatenated string.
--
pete
这篇关于char * ptr =" amit",存储它的内存区域(堆,堆栈,代码,全局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!