为什么不需要声明字符串时分配内存 [英] Why dont need to allocate memory when declaring string
问题描述
我是新的C,目前我想了解如何工作的指针。
下面是一个问题困惑我:
据我所知,值分配给指针之前,您应该为指针分配一定的内存(如果我错了,请大家指正:)),如后续code:
INT主要(无效){
INT I;
为int * PTR =(INT *)malloc的(的sizeof(INT)); 我= 2;
* PTR = 5;
printfn(%D,* PTR);
免费(PTR);
}
然而,在声明C字符串的时候,就像:
的char * p =Hello World的;
而不需要分配内存。
是什么原因,它是如何工作的?如果我失去了一些东西,善意地提醒我。非常感谢!
...你应该分配一定的内存为指针...
块引用>没有,你似乎误解了关于指针...
指针是持有型某种类型的的地址的。看下面这个例子。
int类型的= 1;
为int * p =&放大器;一个;类型的
A
是INT
,其中包含的整数的的类型
P
是为int *
,其中包含的整型变量的地址的让我们假设内存是这样的:
--------------------------------------
变量:| A | p |
--------------------------------------
地址:| 0x12341234 | 0x12341238 |
--------------------------------------在
。&安培;
运营商获得的操作的地址。因此,&放大器;一个
等于0x12341234
所以变量初始化是这样的:
--------------------------------------
变量:| A | p |
--------------------------------------
地址:| 0x12341234 | 0x12341238 |
--------------------------------------
值:| 1 | 0x12341234 |
--------------------------------------现在,看看这个code,
* P
。在*
运营商的引用操作的,得到的该指针指向的变量的值。在这种情况下,P
包含0x12341234
- 在0x12341234 $ C $有什么变数C>?在
A
!因此,* P
等于1
,其中A
包含现在看看这个例子:
的#include<&stdlib.h中GT;炭C1 ='1';诠释的main()
{
炭C2 ='2'; 字符* P1 =&放大器; C1;
字符* P2 =&放大器; C2; CHAR AR [13] =世界,你好! / *不要忘记'\\ 0':12 + 1 * / 字符* P3 =&放大器; AR [0];
为const char * P4 =世界,你好! / *注意的类型,`为const char *`* / 字符* P5 =的malloc(13 * sizeof的(炭));
}
C1
是全局变量,因此的编译器将它放在数据部分节目直接。C2
是主
,所以它被放置在的栈的由<$ C的局部变量$ C>主。无论如何,他们放在内存中。和P1
和P2
包含他们的地址的,所以* P1
是1
(C1
)和* P2
是'2'
(C2
)。
AR
是字符
13长度的阵列的。它被放置在内存中是这样的:-------------------------------------- ----------------
|'H'|'E'|'L'|'L'|'O'|' '|'W'|'O'|'R'|'L'|'D'|'!'|'\\ 0'|
-------------------------------------------------- ----和
&放大器; AR [0]
是的第一个元素的AR
的地址,因此P3
包含的地址'H'
。现在看看
P4
。它是由初始化世界,你好!
。你的问题是它被分配 - 这只是(串)的恒的,如1234
,2.71
或'A'
。常数被放置在程序直接由编译器。像C1
的字符串常量的放在 RO 数据程序段的直接。对比的数据部分的 rodata节的只读(只读数据),因为字符串常量是恒定的。 (如你所知,不变是只读的。)所以类型P4
是为const char *
。
P5
是由的malloc
,也就是从动态分配的返回值初始化。在这种情况下,的malloc
地方分配内存和P5
由该initialied。I am new to C, and currently I am trying to understand how to pointer work.
Here is one problem confusing me:
As far as I know, before assigning a value to pointer, you should allocate certain memory for that pointer (if I am wrong, pls correct me :) ), like the follow code:
int main(void) { int i; int * ptr = (int *)malloc(sizeof(int)); i = 2; *ptr = 5; printfn("%d",*ptr); free(ptr); }
However, when declaring string in C, just like:
char *p = "Hello world";
without need to allocate memory.
What's the reason and how it work? If I am missing something, kindly remind me. many thanks!
解决方案...you should allocate certain memory for that pointer ...
No, you seems to misunderstand about pointers...
Pointer is the type which hold some type's address. Look at this example.
int a = 1; int *p = &a;
The type of
a
isint
, which contains integer.The type of
p
isint *
, which contains address of integer variables.Let's suppose memory like this:
-------------------------------------- variables: | a | p | -------------------------------------- address : | 0x12341234 | 0x12341238 | --------------------------------------
the
&
operator gets the address of operand. So,&a
is equal to0x12341234
.So the variables are initialized like this:
-------------------------------------- variables: | a | p | -------------------------------------- address : | 0x12341234 | 0x12341238 | -------------------------------------- value : | 1 | 0x12341234 | --------------------------------------
Now, look this code,
*p
. the*
operator, dereference operator, gets the value of the variables which the pointer is pointing. In this case,p
contains0x12341234
- What variables is in0x12341234
? thea
! So*p
is equal to1
, whicha
contains.
Now look this example:
#include <stdlib.h> char c1 = '1'; int main() { char c2 = '2'; char *p1 = &c1; char *p2 = &c2; char ar[13] = "hello world!"; /* don't forget '\0' : 12 + 1. */ char *p3 = &ar[0]; const char *p4 = "hello world!"; /* notice the type, `const char *` */ char *p5 = malloc(13 * sizeof(char)); }
c1
is global variables, so compiler place it on the data section of program directly.c2
is local variables ofmain
, so it is placed on stack bymain
. Anyway, they're placed on memory. Andp1
andp2
contains their address, so*p1
is'1'
(c1
) and*p2
is'2'
(c2
).
ar
is 13-length array ofchar
. It is placed on memory like this:------------------------------------------------------ |'h'|'e'|'l'|'l'|'o'|' '|'w'|'o'|'r'|'l'|'d'|'!'|'\0'| ------------------------------------------------------
And
&ar[0]
is the address of the first element ofar
, sop3
contains the address of'h'
.Now look at
p4
. It's initialized by"hello world!"
. Your question is where it is allocated - it's just (string) constant, like1234
,2.71
or'a'
. Constant is placed on the program directly, by compiler. Likec1
, the string constant is placed on the rodata section of program directly. Contrast to data section, rodata section is read-only (Read Only DATA) because string constant is constant. (As you know, constant is read-only.) So the type ofp4
isconst char *
.
p5
is initialized by return value ofmalloc
, that is from dynamic allocation. In this case,malloc
allocates the memory somewhere, andp5
is initialied by this.这篇关于为什么不需要声明字符串时分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!