How to write an address (pointer) in a text file that can be read and dereferenced in C

I would like to write an address in a text file that can be read fscanfand dereferenced by C after reading the pointer. How to write an address in a file?

I don’t want to be rude to change , but I know that this is exactly what I need, so if someone can just simply indicate the answer and not the ethical reasons why I should not do this, that would be great!

further editing: ah, I did not understand what I want to do. In emacs, I want (with my fingers !!) to write in an address that C can read with fscanf and use as a pointer. How I physically (with my fingers !!) write an address in emacs. For example, if I wanted C to read in 0x11111111, I try to write 0x11111111in emacs, but it does not become the correct address in C when I read it.

+3
source share
5 answers

I see no problem, here is a snippet using a line instead of a file:

#include <stdio.h>

int main() {
    void *p1,*p2;
    p1 = (void*)0x12FE0FE0A;
    char str[] = "12FE0FE0A";

    sscanf(str, "%p", &p2);
    printf("%p %p\n",p1,p2);
}

pointers are identical.

By the way, if you write "0x12345678", the format string must be "0x%p"for the "0x" prefix to be processed correctly.

+4
source
fprintf(file, "%p", pointer);

; , , ...

, /IO- , : . Segfault, ...

[]

fscanf(... "%x" ...)

, a.k.a..

+8

. , , - , . , ?

+6

- ?

#include<stdio.h>

int main()
{
    FILE *out = fopen("test.out","w");
    int i = 6;
    int *pi = &i;

    fprintf(out, "%d", pi);
    fclose(out);

    printf("pi points to an int that has the value of %d\n", *pi);

    FILE *in = fopen("test.out","r");
    fscanf(in, "%d", &pi);
    fclose(in);

    printf("After reading the value of pi from the file, it points to an int that has the value of %d\n", *pi); 
}

, , :

int *pi;
fscanf(in, "%d", &pi);
// use *pi to dereference normally

: "%x" .

+3

You can assign an integer value to a pointer type, so you use a type listing like

unsigned char *vram = (unsigned char *)0xA0000000;

The caveat is that the result is determined by the implementation (not undefined, mind you) and may not even be used.

+1
source

Source: https://habr.com/ru/post/1745421/


All Articles