开发者

address to string constant

I get an addres of string from assembler into C, and I need to get content of this address. How to 开发者_如何转开发do it? Google gives C++ examples with reinterpret_cast, but it not working in C (I suppose). I will appreciate if you will note needed libs too, tenx

#include <stdio.h> 
#include <stdlib.h> 

unsigned long const1(void); 

int main()
{ 
    printf("Const: %d\n", const1()); 
    return 0; 
}


If you've already got the address and you know it's a null terminated string, then all you need to do is treat it like a string.

printf("%s", (char*)alleged_string_address);


Preliminary answer while I wait for your info:

   char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */

This answer is geared toward embedded systems. If you need a pointer to something like a peripheral register, use volatile to avoid compiler optimizations.

   volatile char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */


If you know there's a byte with zero in it after the string then try this:

char* p = (char*) <your address here>;
// use p for whatever here

If there's no zero following the string then the standard string functions in C will fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜