简体   繁体   English

如何为 char 指针赋值?

[英]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.我正在做关于在操作系统中模拟交换的作业,我必须运行 c 个文件,包括下面的代码。 It's about 8bit addressing with 4B page size.它是关于 4B 页面大小的 8 位寻址。

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(eg 0x0000600003e00000) and pte_offset has a value(eg '\x19'), when I debug the executable file based on this source code entry value does not change.尽管PTBR有一个地址值(例如0x0000600003e00000), pte_offset也有一个值(例如'\x19'),当我根据这个源代码entry值调试可执行文件时,值并没有改变。 It just stay "" while foo() ends.它只是停留在""foo()结束。 I searched about the diff between char pointer and char array but that didn't help me.我搜索了 char 指针和 char 数组之间的差异,但这对我没有帮助。

I'll be so thanks about any response about this problem!!我会非常感谢关于这个问题的任何回应!

I'm working on m1 mac, vscode.我正在研究 m1 mac,vscode。

Is this what you want to do?这是你想做的吗? Add an offset to the start point address and save the char value at the computed address?在起始点地址上加一个偏移量,把char值保存在计算出的地址处? 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.由于指针值是无符号的 integer 值,因此它与算术运算没有太大区别。

#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:查看output:

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?注意上面的代码是使用Linux中的GCC编译的,VSCode使用的是什么编译器?

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

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