[英]Char Array and uintptr_t
I was reading What is uintptr_t data type but still I'm unable to understand uintptr_t
here as我正在阅读什么是 uintptr_t 数据类型,但我仍然无法在这里理解uintptr_t
unsigned long int
type and then converting back to char*
type.首先将临时char 数组类型转换为unsigned long int
类型,然后再转换回char*
类型,它的用途是什么。Consider the below code snapshot考虑下面的代码快照
strncpy(pCfgMgr->mGlobalCfg.grMap[index].userName,
(char *)(uintptr_t) grParams.peerUsrName, 16); /*index is 0 */
where userName
in the pCfgMgr->mGlobalCfg.grMap[index].userName
is nothing but a char array declared as其中pCfgMgr->mGlobalCfg.grMap[index].userName
中的userName
pCfgMgr->mGlobalCfg.grMap[index].userName
是一个声明为的字符数组
char userName[MAX_USERNAME_LENGTH]; /* MAX_USERNAME_LENGTH is 16 */
And peerUsrName
in the grParams.peerUsrName
is also a char array declared as peerUsrName
中的grParams.peerUsrName
也是一个 char 数组,声明为
char peerUsrName[16];
The thing which I didn't got is that what uintptr_t
makes the difference while copying, which is nothing but alias name of unsigned long int
.我没有得到的是uintptr_t
在复制时产生的不同,这只是unsigned long int
别名。 I am curious to know what the developer was thinking while using uintptr_t
here & is it recommended ?我很想知道开发人员在这里使用uintptr_t
在想什么,是否推荐?
Though without uintptr_t
above strncpy()
statement produces the same output.尽管在strncpy()
语句上方没有uintptr_t
会产生相同的输出。
All helps appreciated wholeheartedly.所有帮助表示衷心感谢。
The cast is unnecessary.演员表是不必要的。
The second argument to strncpy
has type const char *
. strncpy
的第二个参数的类型为const char *
。 An expression of type char *
is also valid here. char *
类型的表达式在这里也有效。 grParams.peerUsrName
has type char [16]
. grParams.peerUsrName
类型为char [16]
。 Arrays in most contexts decay to a pointer to the first element.大多数情况下的数组衰减为指向第一个元素的指针。 So when you pass it to strncpy
it decays to type char *
, which is what it's expecting.因此,当您将它传递给strncpy
它会衰减为char *
类型,这正是它所期望的。 So no need for the cast.所以不需要演员表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.