是否有可能有一个指针文字? [英] Is it possible to have a pointer literal?
问题描述
在C你也可以拥有字符串的形式
In C one can have string literals in the form of
char *string = "string here";
整数文字:
uint8_t num = 5;
较长的文字:
long long bigNum = 90322L;
浮点文本:
float decimal = 6.3f;
时的办法有文字的指针?这是一个文字地址到内存空间。我做的嵌入式项目的一些工作,需要硬code为DMA访问的值。我在做类似如下的内容:
Is the a way to have a pointer literal? That is a literal address to a memory space. I am doing some work on an embedded project and need to hard code a value for a DMA access. I am doing something similar to the following:
uint32_t *source = 0x08000000;
虽然这个编译并正常工作,我得到以下编译器错误(我使用 GCC
的变体):
cc0144: {D} warning: a value of type "int" cannot be used to initialize an entity of type "uint32_t *"
cc0152: {D} warning: conversion of nonzero integer to pointer
有没有办法做到这一点还是我只需要接受此为C的事实的正确方法?我知道我可以做的:
Is there a correct way to do this or do I just need to accept this as a fact of C? I know I can do:
uint32_t *source = (uint32_t *)0x08000000;
但是,这只是似乎很不必要的。什么是做这个行业的方式?我也想知道,这个功能在C ++中存在。
But that just seems very unnecessary. What is the industry way of doing this? I am also wondering if this feature exists in C++.
推荐答案
在C和C ++的唯一指针文字或者常量是零。我们可以去草案C99标准部分 6.3.2.3
的指针的:
In both C and C++ the only pointer literal or constant is zero. We can go to the draft C99 standard section 6.3.2.3
Pointers:
这是整型常量前pression值为0,或者这样的前pression
转换为类型为void *,被称为空指针常量。 55)
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55)
和
的整数可以被转换为任何指针类型。除previously
指定,其结果是实现定义,可能不
正确对齐,可能不会指向引用的实体
类型,可能是一个陷阱重新presentation。 56)
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.56)
应对非零整型常量的正确方法是使用一个演员。
the correct way to deal with non-zero integer constant is to use a cast.
从草案C ++标准等效节很可能是部分 5.2.10
的 Reinter preT投的它说:
The equivalent section from the draft C++ standard would probably be section 5.2.10
Reinterpret cast which says:
整型或枚举类型的值可以显式
转换为指针。转换为整数的指针
足够的尺寸(如果有的话,例如存在于实现)并回
相同的指针型将具有其原始值;之间的映射
指针和整数,否则是实现定义。 [ 注意:
除了如3.7.4.3中所述,这样的转换的结果将
不是一个安全衍生指针值。末端注意事项】
A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined. [ Note: Except as described in 3.7.4.3, the result of such a conversion will not be a safely-derived pointer value. —end note ]
您需要见 3.7.4.3
的所有细节。
有关需要部分指针字面参考 2.14.7
的指针文字的它说:
For the pointer literal reference you need section 2.14.7
Pointer literals which says:
指针文字是关键字nullptr。它是类型的prvalue
的std :: nullptr_t。 [注:性病:: nullptr_t是一个独特的类型是
既不是指针类型,也不是一个成员指针类型;相反,一个prvalue
这种类型的是一个空指针常数和可以转换为一个空
指针值或空成员指针值。见4.10和4.11。 -结束
注意事项】
The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 4.10 and 4.11. —end note ]
这篇关于是否有可能有一个指针文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!