问题描述
由于指针为char,可以做到以下几点:
Given pointers to char, one can do the following:
char *s = "data";
据我了解,指针变量在这里宣布,内存分配给变量和数据,后者则充满了数据\\ 0
和变量问题被设定为指向它的第一字节(即变量包含可以解除引用一个地址)。这是短而紧凑。
As far as I understand, a pointer variable is declared here, memory is allocated for both variable and data, the latter is filled with data\0
and the variable in question is set to point to the first byte of it (i. e. variable contains an address that can be dereferenced). That's short and compact.
由于指针为int,例如,你可以这样做:
Given pointers to int, for example, one can do this:
int *i;
*i = 42;
或
int i = 42;
foo(&i); // prefix every time to get a pointer
bar(&i);
baz(&i);
或
int i = 42;
int *p = &i;
这是有些同义反复。它体积小,能耐受的单变量的一个用法。这不是几个变量的多种用途,但是,生产code杂乱。
That's somewhat tautological. It's small and tolerable with one usage of a single variable. It's not with multiple uses of several variables, though, producing code clutter.
在那里写干和简洁同样的事情什么方法?他们是什么?
是否有更广泛的范围内接近编程,允许完全避免这个问题?可能是我不应该使用在所有的指针(笑话)还是什么?
Are there any ways to write the same thing dry and concisely? What are they?Are there any broader-scope approaches to programming, that allow to avoid the issue entirely? May be I should not use pointers at all (joke) or something?
推荐答案
字符串文字是一个极端例子:他们触发静态存储器中创建的文字,其作为访问的字符
阵列。请注意,以下不编译,尽管 42
作为一个 INT
的文字,因为它不是隐含分配:
String literals are a corner case : they trigger the creation of the literal in static memory, and its access as a char
array. Note that the following doesn't compile, despite 42
being an int
literal, because it is not implicitly allocated :
int *p = &42;
在其他情况下,你有责任分配尖锐的物体,无论是在自动或动态内存。
In all other cases, you are responsible of allocating the pointed object, be it in automatic or dynamic memory.
int i = 42;
int *p = &i;
下面 I
是一个自动变量,p指向它。
Here i
is an automatic variable, and p points to it.
int * i;
*i = 42;
您只需调用未定义行为。 I
尚未初始化,并随机在内存中,因此指向的地方。然后你分配 42
这个随机位置,与联合国predictable后果。坏的。
You just invoked Undefined Behaviour. i
has not been initialized, and is therefore pointing somewhere at random in memory. Then you assigned 42
to this random location, with unpredictable consequences. Bad.
int *i = malloc(sizeof *i);
下面 I
初始化为指向的内存动态分配的块。不要忘了免费(I)
一旦你用它做。
Here i
is initialized to point to a dynamically-allocated block of memory. Don't forget to free(i)
once you're done with it.
int i = 42, *p = &i;
这里是如何创建一个自动变量和一个指向它作为一个班轮。 I
是变量, P
指向它。
编辑:好像你的真正的想该变量隐式和匿名分配。嗯,这里是你如何能做到这一点:
Edit : seems like you really want that variable to be implicitly and anonymously allocated. Well, here's how you can do it :
int *p = &(int){42};
这是啄字面的化合物。它们与自动存储时间(或在文件范围内的静态)匿名情况下,只有在C90和进一步存在(但不是C ++!)。相对于字符串,复合文字都是可变的,即可以修改 * P
。
This thingy is a compound literal. They are anonymous instances with automatic storage duration (or static at file scope), and only exist in C90 and further (but not C++ !). As opposed to string literals, compound literals are mutable, i.e you can modify *p
.
编辑2:添加该解决方案从另一个答案(不幸的是提供了一个错误说明)的完整性:
Edit 2 : Adding this solution inspired from another answer (which unfortunately provided a wrong explanation) for completeness :
int i[] = {42};
这将分配与自动存储时间一个元素可变数组。阵列的名称,而不是指针本身一>,将衰减到需要的指针。
This will allocate a one-element mutable array with automatic storage duration. The name of the array, while not a pointer itself, will decay to a pointer as needed.
然而要注意的sizeof我
将返回错误的结果,即数组( 1 *的sizeof(INT的实际大小)
),而不是一个指针的大小(的sizeof(INT *)
)。这应该不过很少是一个问题。
Note however that sizeof i
will return the "wrong" result, that is the actual size of the array (1 * sizeof(int)
) instead of the size of a pointer (sizeof(int*)
). That should however rarely be an issue.
这篇关于声明并初始化简洁指针(即指向int的指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!