C++ 在 64 位平台中传递字符数组地址
c++ pass char array address in 64bit platform
我有func((,它必须采用无符号的int参数。
void func(unsigned int val(是有原因的,
我必须将各种类型(无符号字符、无符号短字符、无符号整数(传递给 func 参数。
我必须将 char 数组传递给 func((,我当前的解决方案如下代码。
编辑:有没有简单的方法可以在64位平台上移植此代码?
char test_str[128] = { 0 };
void func(unsigned int val)
{
memcpy(test_str, (char *)val, 128); //current my solution
printf("%sn", test_str);
}
int main()
{
char str[128] = "hello world";
func((unsigned int)(char *)&str); //current my solution
return 0;
}
注:intptr_t
你的代码有很多问题,但至少对我来说,编译器首先抱怨的是:
错误:从指针强制转换为较小的类型"无符号 int"丢失 信息 │偏移量:4 字节: 0x7ffe4b93ddd4 内容:9 func((unsigned int((char *(str(;
我假设您正在尝试将 char 数组的文字地址潜入无符号的 int 参数中。然而,一个无符号的int只能容纳4个字节(在我的平台上(,这不足以容纳完整的地址,因为所有指针都需要8个字节(同样,在我的平台中(。你自己看吧。
#include <stdio.h>
int main (int argc, char *argv[])
{
char str[128] = "hello world";
unsigned int *address = (unsigned int *)str;
printf("address: %p t contains: %s t pointer size: %lu n", address, (char *)address, sizeof(char *));
// address: 0x7ffcf9492540 contains: hello world pointer size: 8
printf("Size of address in pointer: %lu n", sizeof(long long *));
// will print the same size as the past sizeof() operation
printf("Size of unsigned int variable: %lu n", sizeof(unsigned int));
// 4 bytes, not enough to fit in the necessary 8 bytes
return 0;
}
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?