简体   繁体   English

字符数组和 uintptr_t

[英]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

  • what purpose it serve by first converting temporarily char array type to 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM