c - "Makes Pointer from Integer without a Cast"- 如何满足 GCC?

标签 c pointers casting x11

我正在用 C 编写我的第一个 X11 应用程序,在尝试检索我的应用程序窗口的大小时遇到​​了一个主要问题。

temp.c:30:2 警告:传递“XGetGeometry”的参数 3 使指针来自整数而不进行强制转换

我知道这只是一个警告,但它仍然会导致段错误,这并不好玩。这是我的代码:

static void loop();
static void initialize();
static void cleanUp();
static void run();

/* Variables */

static int screenNumber;
unsigned long White;
unsigned long Black;
long eventMask;
static Display *currentDisplay;
static Window currentWindow;
static unsigned int windowWidth;
static unsigned int windowHeight;
static GC graphicsController;
static XEvent XEvents;

void loop() {
        XGetGeometry(currentDisplay, currentWindow, DefaultRootWindow(currentDisplay), NULL, NULL, &windowWidth, &windowHeight, NULL, NULL);

        XDrawLine(currentDisplay, currentWindow, graphicsController, 0, 0, (int)windowWidth, (int)windowHeight);
        XDrawLine(currentDisplay, currentWindow, graphicsController, 0, (int)windowHeight, (int)windowWidth, 0);
}

void initialize() {
        currentDisplay = XOpenDisplay(NULL);
        screenNumber = DefaultScreen(currentDisplay);

        White = WhitePixel(currentDisplay, screenNumber);
        Black = BlackPixel(currentDisplay, screenNumber);

        currentWindow = XCreateSimpleWindow(    currentDisplay,
                                                DefaultRootWindow(currentDisplay),
                                                0, 0,
                                                500, 500,
                                                0, Black,
                                                White);

        XMapWindow(currentDisplay, currentWindow);
        XStoreName(currentDisplay, currentWindow, "rGot - X11");

        eventMask = StructureNotifyMask;
        XSelectInput(currentDisplay, currentWindow, eventMask);

        do{
                XNextEvent(currentDisplay, &XEvents);
        }while(XEvents.type != MapNotify);

        graphicsController = XCreateGC(currentDisplay, currentWindow, 0, NULL);

        XSetForeground(currentDisplay, graphicsController, Black);
}

void run() {
        eventMask = ButtonPressMask|ButtonReleaseMask;
        XSelectInput(currentDisplay, currentWindow, eventMask);
        do{
                XNextEvent(currentDisplay, &XEvents);
                loop();
        }while(1==1);
}

void cleanUp() {
        XDestroyWindow(currentDisplay, currentWindow);
        XCloseDisplay(currentDisplay);
}

int main(){
        initialize();
        run();
        cleanUp();
        return 0;
}

我知道我的指针做错了,但我对此还很陌生......这是我的设置:

  • Ubuntu 12.04 LTS
  • 编译:gcc tempc -o temp -lX11

对于后来发现这一点的人 - 我最初尝试使用 XGetGeometry 是完全错误的!

要正确使用它,我必须执行以下操作:

XGetGeometry(currentDisplay, currentWindow, ¤tRoot, &windowOffsetX, &windowOffsetY, &windowWidth, &windowHeight, &windowBorderWidth, &windowDepth);

这是基于我的发现 here .

最佳答案

根据这两个函数的文档,DefaultRootWindow() 返回一个 WindowXGetGeometry() 需要一个 Window* 第三个参数。因此,正如警告所说,您正在传递一个普通类型,其中需要指向该类型的指针。

关于c - "Makes Pointer from Integer without a Cast"- 如何满足 GCC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335583/

相关文章:

java - 具有构建器模式的通用对象

c - 在 C 中隐藏类型定义

c - 操作系统中的任务的启动和停止是如何完成的?

c - 来自不兼容指针类型的警告初始化 - C 函数指针数组

objective-c - 在 cocoa 中进行类型转换以修复警告

arrays - 需要不必要的 postgres 数组转换吗?

c - pthread_self 返回不同的值

c - 一遍又一遍地在String中使用struct(C语言)

c++ - 指针 vector 中的内存泄漏

pointers - []struct{} 和 []*struct{} 有什么区别?