'How can I assign char pointer a value?

I'm doing my assignment about simulating swaping in OS and i have to run c files including code's below. It's about 8bit addressing with 4B page size.

char foo(void *PTBR, char va)
{
    char *entry;

    char pte_offset = (va & PFN_MASK) >> 2; /* PFM_MASK : 11111100 */

    ...

    /* page table */
    entry = (char *)PTBR + pte_offset;

    ...
 
}

Despite the fact that PTBR has a address value(e.g. 0x0000600003e00000) and pte_offset has a value(e.g. '\x19'), when I debug the executable file based on this source code entry value does not change. It just stay "" while foo() ends. I searched about the diff between char pointer and char array but that didn't help me.

I'll be so thanks about any response about this problem!!

I'm working on m1 mac, vscode.



Solution 1:[1]

Is this what you want to do? Add an offset to the start point address and save the char value at the computed address? Briefly you wanna do some pointer arithmetic, don't you? Since the pointer values are unsigned integer values it doesn't much different than the arithmetic operations.

#include <stdio.h>

#define PFN_MASK 0b11111100u

char foo(void *PTBR, char va)
{
    char *entry;

    int pte_offset = (va & PFN_MASK) >> 2; /* PFN_MASK : 11111100 */
    printf("start address %p, offset %d\n", PTBR, pte_offset);

    /* page table */
    entry = PTBR + pte_offset; // Pointer arithmetic
    printf("entry %p\n", entry);

    *entry = va;
    printf("entry address %p, value at that address %c\n", entry, *entry);
 
}


int main() {
    
    char charForPTBR; // This is the starting address
    
    foo(&charForPTBR, 'S');

    return 0;
}

Check the output out:

start address 0x7ffcbe7289cf, offset 20
entry 0x7ffcbe7289e3
entry address 0x7ffcbe7289e3, value at that address S

Note The above code is compiled using GCC in Linux. What compiler does the VSCode use?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1