问题描述
我读了;
int *p = (int []){3, 0, 3, 4, 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".
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.
这篇关于哪里的化合物/字符串将存储在内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!