【发布时间】:2021-02-25 12:53:49
【问题描述】:
问题
嗨,我有这个函数来检查当前路径并返回带有路径的 char 指针。但是当我使用 GCC 进行编译时,它会返回这两个警告。我尝试了一些解决方案,但无法解决。
应该如何处理这个警告?
警告
In file included from C:\Users\Lsy\Documents\C\murtza_debug\main.c:10:0:
C:\Users\Lsy\Documents\C\murtza_debug\system/path.h:6:10: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int *p = cwd;
^~~
C:\Users\Lsy\Documents\C\murtza_debug\system/path.h: In function 'get_path':
C:\Users\Lsy\Documents\C\murtza_debug\system/path.h:9:7: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
p = &cwd;
^
代码
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
char cwd[8024];
int *p = cwd;
int* get_path() {
p = &cwd;
if (getcwd(cwd, sizeof(cwd)) != NULL) {
return p;
}
}
【问题讨论】:
-
您正在尝试将 char 指针分配给 int 指针。
-
您将一个
char指针分配给一个int指针,您为什么希望它能够工作?另外,p = &cwd;在任何情况下都是错误的,请删除&。 -
所以是的,
int *类型与char *不兼容(在特定的 C 语言“兼容”意义上)。我不清楚你为什么首先将p声明为int *而不是char *。 -
下一个问题:当
if不为真时,你的函数返回什么? -
@mch 这只是一个测试功能
标签: c