错误:从“ int*”转换为“ int” [-fpermissive]

error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

本文关键字:int -fpermissive 错误 转换      更新时间:2023-10-16

获取错误:

错误:从" int*"转换为" int" [-fpermissive] 在G

在以下代码上:

void* func(void *s)
{
    int i = 0;
    int self = (int *)s;
    printf("Thread Entered: %dn", self);
    sm.lock(self);
    // Critical section (Only one thread
    // can enter here at a time)
    for (i=0; i<MAX; i++)
        ans++;
    sm.unlock(self);
}

您需要将int self = (int *)s;更改为int self = *((int *)s);int * self = (int *)s;

您需要将这些视为两种不同的东西。一个是通往存储值的内存的指针(int*),另一个是实际值(int)。

查看您的函数声明void* func(void *s),您的s参数是类型void,如果您想转换它,则需要为int

您的数据类型似乎有些混杂,在C/C 中的飞行效果不佳。可以清洁它吗?如果您使用pthread_create()使用此功能,则根据该功能的示例,请尝试..

// your pthread_create call..
int SOME_INT = 123;
s = pthread_create(&thread_id, &attr, &thread_start, &SOME_INT);
//...and your function
void* func(void *s)
{
    int self = (int*) s;

指针可能会令人困惑。查看上面的代码是否看起来相似,特别是将pthread_create作为指针参考的最后一个参数。然后尝试您的原始代码。可能只是不是作为参考。

看看您的产生的东西,否则尝试将其存储为指针,然后在使用中转换。

void* func(void *s)
{
    int *self = s;
    sm.lock(*self);  // but can give a potential race condition.