来自整数的冲突类型错误和指针没有强制转换警告 C

贾斯汀·K·马多克斯

我正在尝试用 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);
}

问题是为什么此代码会出现这些警告和此错误?

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

安蒂·哈帕拉

write是系统调用的名称。函数声明存在于<unistd.h>您已包含在 C 程序中的声明中。

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


至于其他警告,

main()

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

对于pthread_*函数,你必须 #include <pthread.h>

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


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


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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在c中创建单词搜索难题求解器,不兼容的指针类型和来自整数的指针,而在c中没有强制转换警告

来自指针的整数,在 C 中没有强制转换

C 上的字符和字符串赋值问题(来自指针的整数,没有强制转换?)

C:警告:赋值使指针从整数开始而没有强制转换[默认启用]

赋值使指针从整数开始而没有强制转换(c)

C语言中的强制转换警告:赋值使指针不进行强制转换而生成整数

在 gcc 中编译 c 时收到有关“从指针到不同大小的整数”强制转换的警告

C中的“预期的指针”和“冲突的参数类型”错误

Dev C ++ strtok_s抛出[警告]赋值使指针从整数开始而无需强制转换

赋值使指针产生整数,而没有强制转换警告

c错误:赋值使指针从整数变为无强制转换[-Werror = int-conversion]

整数没有在 C++ 中被类型转换为 bool

C链表,printf和“赋值从指针进行整数运算而无强制转换”问题

警告:初始化会使指针从整数开始而没有强制转换(整数指针)

C - 赋值从指针生成整数而不进行强制转换

c警告消息:非零整数到指针的转换

C错误:“类型冲突”

在C中强制转换函数指针的返回类型

C ++ 11智能指针的所有权和强制转换

gcc 警告赋值使指针来自整数而不进行强制转换

警告:赋值使指针从整数变为整数,而没有强制转换ARGV

在 C 中,有没有办法将 void 指针转换为定义的大小类型?

C ++中指针的类型转换和类型转换

C语言中指针和整数的警告比较

指针类型转换C ++

没有指针的C ++分段错误(HackerRank)

c ++指针强制转换重载

问题“来自整数的指针/没有转换的指针的整数”问题

来自不兼容指针类型错误的C赋值