简体   繁体   English

C语言:将Pointer转换为String,我还能得到什么?

[英]C language: Convert Pointer to String, what am I getting back?

I'm just experimenting with some system level programming using c. 我只是在尝试使用c进行一些系统级编程。 I have encountered something ambiguous and I'm hoping someone here can clear it up for me. 我遇到了一些模棱两可的事情,希望这里有人可以帮我解决。

If I make a char* and then feed the address into a function in the following way 如果我创建一个char *,然后按以下方式将地址输入函数

char* string;
os_IntToString(&string, string);

void os_IntToString(int value, char* str) {
    int scancode_offset = 48;

    char* start = *str;
    do{
        int piece = value % 10;
        *str++ = piece + scancode_offset;
        value = value / 10;
    } while(value);
    *str-- = '\0';
}

Then what exactly am I getting back? 那我到底要回来什么? I get real numbers, an example would be 589796. Obviously the address is backwards but what base system is it? 我得到实数,例如589796。显然地址是向后的,但是它是什么基本系统?

Memory addresses are in hex right? 内存地址是十六进制的吗? But the function uses Int which is decimal base 10? 但是函数使用的Int是十进制基数10? Is that correct? 那是对的吗? Does a conversion process happen or do I now have a deciaml address, I just don't know. 我不知道是在进行转换过程还是现在有一个下位地址。

Can anyone clear this up please. 谁能解决这个问题。 Thanks so much in advance. 非常感谢。

As far as the C language is concerned, addresses are not "in hex", nor are they numbers. 就C语言而言,地址不是“十六进制”,也不是数字。 Addresses are just addresses, or pointer values. 地址只是地址或指针值。 Depending on the addressing scheme of the system you're using, addresses might be represented in hex (for example, if printed with printf 's "%p" format). 根据您使用的系统的寻址方案,地址可能以十六进制表示 (例如,如果以printf"%p"格式打印)。 You can also convert an address to an integer, but the result of the conversion is implementation-defined -- and integers aren't "in hex" either, though you can generate a human-readable hexadecimal representation of an integer. 您还可以将地址转换为整数,但是转换的结果是实现定义的-整数也不是“十六进制”的,尽管您可以生成人类可读的整数十六进制表示形式。 (Integers are represented in binary.) (整数以二进制形式表示。)

As for your function, you've defined it with a void return type, and then attempted to return a char* value from it. 对于您的函数,您已经使用void返回类型定义了它,然后尝试从中返回char*值。 You should have gotten at least a warning from your compiler, if not a fatal error. 如果不是致命错误,则应该至少从编译器获得警告。

All data (memory addresses, ints, pointers, strings, floats, etc) are internally stored as binary, or base 2. 所有数据(内存地址,整数,指针,字符串,浮点数等)都内部存储为二进制或基数2。

Being base 10 or base 16 is not a question of how the hardware or software (libc, your program, the assmebly, or even CPU microcode) stores and manipulates it (except for binary-coded decimal which is rarely used except for certain display and conversion steps). 以10为基数还是以16为基数,不是硬件或软件(libc,程序,汇编甚至CPU微码)如何存储和操作(二进制编码的十进制除外,除了某些显示和转换步骤)。

When you are returned an int, while you assume it as base 10(and most calls that will print the int display it as base 10), for the hardware it holds its meaning as base 2, the same as the pointer or memory address. 当返回一个int时,假设它以10为底(大多数将打印int的调用将其显示为10),对于硬件,它的含义是以2为底,与指针或内存地址相同。 It just gets printed that way. 它只是以这种方式打印。

Moreover, for the memory address, base 2 is only due to the way the circuits operate in the CPU and northbridge. 此外,对于存储器地址,基数2仅是由于电路在CPU和北桥中的工作方式。 When you see it as base 16, it's just the debugger's representation. 当您将其视为基础16时,它只是调试器的表示形式。 We could hypothetically address it as base 10 and refer to 65536 in memory but it is just easier to use 10000 for display as trailing zeroes help clue the programmer into the alignment. 我们可以假设它以10为底数,并在内存中引用65536,但是使用10000进行显示会更容易,因为尾随零会帮助程序员提示对齐。 It's just a convenient representation. 这只是一个方便的表示。 We could use base 13 and call it 23AA3 but that would simply be inconvenient. 我们可以使用基数13并将其命名为23AA3,但这很不方便。 When we see it as a number, it is not necessarily a number. 当我们将其视为数字时,不一定是数字。 It is a location, and while in certain respects it could be considered a number, and it is convenient for the programmer to see it as a number, the nonlinear nature of memory mapping on today's systems can make that consideration somewhat incorrect. 它是一个位置,虽然在某些方面可以视为一个数字,并且程序员可以方便地将其视为一个数字,但当今系统中内存映射的非线性性质可能会使这种考虑有些不正确。

While there may be problems with the code itself I'm simply answering the point of number representation. 虽然代码本身可能存在问题,但我只是在回答数字表示的问题。

The address isn't backwards. 地址不后退。 Printf shoudl always print big endian. Printf shoudl始终打印大字节序。 If you print the address locations, if you get 589796 when an int, if you print it as hex, then you'll get 0x8FFE4 = 589796. Everything is working as it should, since no matter hwo you print it, its going to be the correct memory address, just with a different representation. 如果您打印地址位置,如果将int值打印为589796,将其打印为十六进制,则将得到0x8FFE4 =589796。一切都会正常进行,因为无论如何打印,它都将正确的内存地址,只是具有不同的表示形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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