[Solved] c++ pass char array address in 64bit platform [closed]


There are a lot of problem with your code, but at least for me the one the compiler first complains about is:

error: cast from pointer to smaller type ‘unsigned int’ loses
information │Offset: 4 byte:
0x7ffe4b93ddd4 contents:9 func((unsigned int)(char *)str);

I assume that you’re trying to sneak in the literal address of the char array into the unsigned int parameter. However an unsigned int can only hold 4 bytes (in my platform), that is not enough to hold the full address, since all pointers require 8 bytes (again, in my platform). See for yourself.

#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;
}

2

solved c++ pass char array address in 64bit platform [closed]