【问题标题】:Conflicting types error and pointer from integer without cast warning C来自整数的冲突类型错误和指针,没有强制转换警告C
【发布时间】:2019-05-12 01:49:34
【问题描述】:

我正在尝试学习 C 中的读/写程序,我在网上找到了一个我认为应该开始学习的示例。但是,当我尝试编译代码时,我收到了一些警告和一个类似这样的错误:

read.c:21:8: error: conflicting types for ‘write’
 void * write(void *temp) {
        ^
In file included from read.c:11:0:
/usr/include/unistd.h:369:16: note: previous declaration of ‘write’ was here
 extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
                ^
read.c: In function ‘write’:
read.c:25:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
 ret=pthread_rwlock_wrlock(&rwlock);
    ^
read.c: In function ‘write_2’:
read.c:45:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
 ret=pthread_rwlock_wrlock(&rwlock);
    ^
read.c: At top level:
read.c:106:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main() {

我对 C 编程还是很陌生,我不知道如何解决这些警告和错误,因为我只是在不太了解该语言的情况下跳入这个主题。这是我要编译的代码。

#include <stdio.h> 
#include <pthread.h> 
#include <stdlib.h>
#include <unistd.h>

/*
From the output we can see that the two writes were executed one after the other. But in case of reads, even though read_1 had not unlocked the rwlock, read_2 was allowed into the critical section and read the file. That shows us that multiple readers are allowed but only one writer is allowed into the critical section. 
*/
pthread_rwlock_t rwlock; // allows multiple readers to access the resource, but only one reader at any given time.




void * write(void *temp) {
char *ret;
FILE *file1;
char *str;
ret=pthread_rwlock_wrlock(&rwlock);
printf("\nFile locked, please enter the message \n");
str=(char *)malloc(10*sizeof(char));
file1=fopen("temp","w");
scanf("%s",str);
fprintf(file1,"%s",str);
fclose(file1);
pthread_rwlock_unlock(&rwlock);
printf("\nUnlocked the file you can read it now \n");
return ret;
}




void * write_2(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(3);
ret=pthread_rwlock_wrlock(&rwlock);
printf("\nFile locked, please enter the message \n");
str=(char *)malloc(10*sizeof(char));
file1=fopen("temp","a");
scanf("%s",str);
fprintf(file1,"%s",str);
fclose(file1);
pthread_rwlock_unlock(&rwlock);
printf("\nUnlocked the file you can read it now \n");
return ret;
}





void * read_1(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(5);
pthread_rwlock_rdlock(&rwlock);
printf("\n1 Opening file for reading\n");
file1=fopen("temp","r");
str=(char *)malloc(10*sizeof(char));
fscanf(file1,"%s",str);
printf("\nMessage from file is %s \n",str);
sleep(3);

fclose(file1);
printf("\nUnlocking rwlock\n");
pthread_rwlock_unlock(&rwlock);
return ret;
}





void * read_2(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(6);
pthread_rwlock_rdlock(&rwlock);
printf("\n2 Opening file for reading\n");
file1=fopen("temp","r");
str=(char *)malloc(10*sizeof(char));
fscanf(file1,"%s",str);
printf("\nMessage from file is %s \n",str);

fclose(file1);

pthread_rwlock_rdlock(&rwlock);

return ret;
}




main() {

pthread_t thread_id,thread_id1,thread_id3,thread_id4;
pthread_attr_t attr;
int ret;
void *res;
pthread_rwlock_init(&rwlock,NULL);
ret=pthread_create(&thread_id,NULL,&write,NULL);
ret=pthread_create(&thread_id1,NULL,&read_1,NULL);

ret=pthread_create(&thread_id3,NULL,&read_2,NULL);

ret=pthread_create(&thread_id4,NULL,&write_2,NULL);
printf("\n Created thread");
pthread_join(thread_id,&res);
pthread_join(thread_id1,&res);
pthread_join(thread_id3,&res);

pthread_join(thread_id4,&res);
pthread_rwlock_destroy(&rwlock);
}

问题是为什么这些警告和这个错误会出现在这段代码中?

附:不知道为什么,但是当我在收到这些警告和错误后尝试运行它时,它仍然以我认为的方式运行。这很奇怪。感谢您的阅读。

【问题讨论】:

  • write 是一个库函数,因此编译器不喜欢在定义另一个函数时使用它。将名称更改为write_1 或其他名称

标签: c pointers compiler-errors warnings read-write


【解决方案1】:

write 是系统调用的名称。函数的声明位于您已包含在 C 程序中的 &lt;unistd.h&gt; 中。

你的 C 程序继续工作,因为你没有在任何地方使用 actual write 系统调用; C 标准库使用它,但它静态链接到 other write 函数。


至于其他警告,

main()

根本不是正确的 C。它需要有int main(void)int main(int argc, char *argv[]) 的原型。

对于pthread_* 函数,您必须 #include &lt;pthread.h&gt;

pthread_rwlock_wrlock 的返回值不是指针,而是int,所以你必须将它分配给int 类型的对象,但你将它分配给ret类型为char *


总而言之,您应该在 C 编译器中打开所有通常有用的诊断 (-Wall),并将每个警告视为错误 (-Werror)。


最后,所有编译和链接命令行都应该带有-pthread 标志。

【讨论】:

  • 由于原帖没有提供这方面的信息,我会提到还需要-pthread 标志。
  • 谢谢。我更新了我的代码。我现在收到这些警告有什么原因吗?我更改了 ret 类型,但仍然收到有关返回类型的警告,使指针从整数而不进行强制转换。
  • @JustinK.Maddox 在问题得到回答后,您永远不应该更改,只能改进。请单独提出您的新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多