【问题标题】:getting warning "assignment makes integer from pointer without a cast"收到警告“赋值使指针从没有强制转换的整数”
【发布时间】:2023-03-31 01:25:01
【问题描述】:

我在第 59 行收到此警告:

赋值从没有强制转换的指针生成整数。

我该如何解决?这是整个文件(从http://pastebin.com/BrmjBAS0复制):

/* bkerndev - Bran's Kernel Development Tutorial
*  By:   Brandon F. (friesenb@gmail.com)
*  Desc: Global Descriptor Table management
*
*  Notes: No warranty expressed or implied. Use at own risk. */


/* Defines a GDT entry */
struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
} __attribute__((packed));

struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
} __attribute__((packed));

/* Our GDT, with 3 entries, and finally our special GDT pointer */
struct gdt_entry gdt[3];
struct gdt_ptr gp;

/* This is in start.asm. We use this to properly reload
*  the new segment registers */
extern void _gdt_flush();

/* Setup a descriptor in the Global Descriptor Table */
void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
{
    /* Setup the descriptor base address */
    gdt[num].base_low = (base & 0xFFFF);
    gdt[num].base_middle = (base >> 16) & 0xFF;
    gdt[num].base_high = (base >> 24) & 0xFF;

    /* Setup the descriptor limits */
    gdt[num].limit_low = (limit & 0xFFFF);
    gdt[num].granularity = ((limit >> 16) & 0x0F);

    /* Finally, set up the granularity and access flags */
    gdt[num].granularity |= (gran & 0xF0);
    gdt[num].access = access;
}

/* Should be called by main. This will setup the special GDT
*  pointer, set up the first 3 entries in our GDT, and then
*  finally call gdt_flush() in our assembler file in order
*  to tell the processor where the new GDT is and update the
*  new segment registers */
void gdt_install()
{
    /* Setup the GDT pointer and limit */
    gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
    gp.base = &gdt;

    /* Our NULL descriptor */
    gdt_set_gate(0, 0, 0, 0, 0);

    /* The second entry is our Code Segment. The base address
    *  is 0, the limit is 4GBytes, it uses 4KByte granularity,
    *  uses 32-bit opcodes, and is a Code Segment descriptor.
    *  Please check the table above in the tutorial in order
    *  to see exactly what each value means */
    gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);

    /* The third entry is our Data Segment. It's EXACTLY the
    *  same as our code segment, but the descriptor type in
    *  this entry's access byte says it's a Data Segment */
    gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);

    /* Flush out the old GDT and install the new changes! */
    _gdt_flush();
}

【问题讨论】:

  • 请给我一个线索...代码的哪一行?
  • 对不起,我忘了写!现在是 59,“gp.base = &gdt;”!

标签: c compiler-warnings


【解决方案1】:

好的,让我把这个放在上下文中......

struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
} __attribute__((packed));

struct gdt_entry gdt[3];
struct gdt_ptr gp;

gp.base = &gdt;

正在将指针 (&gdt) 分配给整数 (gp.base) - 因此出现错误 :-)

我怀疑你实际上想要(或类似的东西):

struct gdt_ptr
{
    unsigned short limit;
    struct gdt_entry *base;
} __attribute__((packed));

或者,由于您稍后将地址作为其组成字段进行评估,因此保持定义不变,然后进行赋值

【讨论】:

  • 嗯,然后我在同一行收到此警告:“来自不兼容指针类型的赋值”
  • 如果赋值真的正确,你需要对指针进行类型转换
  • @KamilŠrot 他不应该将base 的类型更改为struct gdt_entry (*)[3],而不是在分配确实正确的情况下进行任何转换(尽管我怀疑它是否正确)?
【解决方案2】:

我听说地址应该保存在size_t,但我真的想把地址保存在int 中,也许只是投射它? gp.base = (int) &gdt;

【讨论】:

  • 只有 intptr_tuintptr_t 保证保存 (void *) 指针的值。
【解决方案3】:

我之前遇到过这个问题,类型转换工作:

gp.base = (unsigned int)&gdt

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 2016-05-29
    • 2015-10-19
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多