哪里的化合物/字符串将存储在内存? [英] Where does the compound/string literals get stored in the memory?

查看:102
本文介绍了哪里的化合物/字符串将存储在内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了;


  

文字的​​化合物是C99的功能,可用于创建一个没有名字的数组。请看例子:


 为int * p =(INT []){3,0,3,4,1};


  

P 指向一个包含五元素的数组 3,0,3,4的第一个元素,和 1


其实我想知道,这是数组将存储在内存或没有,因为它不会有名字吗?结果

的情况下,换句话说

 的char *海峡=你好

其中字符串你好将存储在内存中?


解决方案

使用指针运算。因此,

  P [0],第[1],...

  * P *(P + 1),...

这里的事情。在C中,你有一个像 INT 基本类型漂亮的文字和<$​​ C $ C>字符,甚至是字符串。因此,我们可以很容易地说这样的话

  INT长度(字符* S);
INT LEN =长度(你好,世界!);

在C99,概念的复合文字的加入来处理数组文本和结构体文字。因此,我们现在可以说这样的话:

  INT总和(int类型的[],INT N);
INT总=总和((INT []){17,42}​​,2);

这是使用文字重新present阵列文字的化合物。


  

其实我想知道,这是数组将存储在内存或没有,因为它不会有一个名字?


是的,在内存中。

我觉得你的困惑源于此。 P 都有一个名字。 (INT []){3,0,3,4,1} 没有。它只是恰巧, P 的值为的地址(INT []){3,0,3,4,1} 。当然,(INT []){3,0,3,4,1} 是在内存中;这将是在对可执行的数据段。你只是没有与引用它的任何名称。

I read that ;

A compound literal is a C99 feature that can be used to create an array with no name. Consider the example:

int *p = (int []){3, 0, 3, 4, 1};

p points to the first element of a five- element array containing 3, 0, 3, 4, and 1.

Actually I want to know, Is this array will stored in memory or not as it doesn't have a name?
In other words in case of

char* str = "hello"

Where the string "hello" will stored in memory?

解决方案

Using pointer arithmetic. So

p[0], p[1], ...

or

*p, *(p + 1), ...

Here's the thing. In C, you have nice literals for primitive types like int and char, and even string literals. So, we can easily say things like

int length(char *s);
int len = length("Hello, World!");

In C99, the concept of compound literals was added to handle "array literal" and "struct literal". Therefore, we can now say things like:

int sum(int a[], int n);
int total = sum((int []){ 17, 42 }, 2);

This is using a compound literal to represent an "array literal".

Actually I want to know, Is this array will stored in memory or not as it doesn't have a name?

Yes, in memory.

I think your confusion stems from this. p has a name. (int []){3, 0, 3, 4, 1} does not. It just so happens that p's value is the address of (int []){3, 0, 3, 4, 1}. Of course (int []){3, 0, 3, 4, 1} is in memory; it will be in the data segment for your executable. You just don't have any name with which to refer to it.

这篇关于哪里的化合物/字符串将存储在内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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