简体   繁体   English

结构/线程内的指针

[英]Pointer inside a struct / thread


I have this warning "warning: assignment from incompatible pointer type " in this line: 我在此行中收到以下警告:“警告:来自不兼容指针类型的赋值”:

data1->transformed_block[l] = &transformed_block[l];

- --

void print_message_function ( void *ptr )  
{  
  dt *data;  
  data = (dt *) ptr;  
  printf("Dentro da thread Numero0: %ld\n", data->L_norm_NewBlock);  
  pthread_exit(0);  
}  

typedef struct data_thread  
{  
    long L_norm_NewBlock;  
    int Bsize_X;      
    int Bsize_Y;  
    int *transformed_block[MAX_LEVEL];  
    long L_norm_OrigBlock;      
} dt;  

void function()  
{  
  int *transformed_block[MAX_LEVEL];  
  pthread_t thread1;  
  dt *data1;  
  pthread_attr_t attr;  
  pthread_attr_init(&attr);

  //Fills structure  
  data1 = (dt *) malloc(sizeof(dt));    
  data1->transformed_block[l] = &transformed_block[l];

  data1->L_norm_NewBlock=0;  
  data1->Bsize_Y = Bsize_Y;  
  data1->Bsize_X = Bsize_X;  

  pthread_create(&thread1, &attr, (void *) &print_message_function, (void *) &data1);  
}

I want to get rid of that warning, and the values i get inside the thread are wrong. 我想摆脱该警告,并且我在线程内得到的值是错误的。 For example data1->L_norm_NewBlock=0; 例如data1-> L_norm_NewBlock = 0; in the thread guives me a differente value (not 0 like it should be). 在线程中为我提供了一个differe值(不是应该的0)。

What is transformed_block ? 什么是transformd_block? Assuming that it is the same as the variable defined in the struct, you are trying to assign the address of the lth element (pointer to an int pointer) to the lth element of the data1->transformed_block (a pointer to an int). 假定它与在结构中定义的变量相同,则尝试将第l个元素(指向int指针的指针)的地址分配给data1-> transformed_block(指向int的指针)的第l个元素。

data1->transformed_block[l] = &transformed_block[l];
// int* = &(int*)

Aren't you trying to simply assing? 您不是在简单地想屁股吗?

data1->transformed_block[l] = transformed_block[l];

The values inside the thread are okay now. 线程中的值现在可以了。 I erased the & in the function 我删除了功能中的&

pthread_create(&thread1, &attr, (void *) &print_message_function, (void *) &data1); pthread_create(&thread1,&attr,(void *)&print_message_function,(void *)&data1);

to

pthread_create(&thread1, &attr, (void *) &print_message_function, (void *) data1); pthread_create(&thread1,&attr,(void *)&print_message_function,(void *)data1);

But I still haven't got rid of that warning... 但是我仍然没有摆脱那个警告...

You have an array of int pointers in your structure and declared as a local variable in your function, however you never assign it to point to anything. 您的结构中有一个int指针数组,并在函数中声明为局部变量,但是您绝不会将其分配为指向任何东西。

If you are trying to have a local array in function() you should declare int transform_block[MAX_LEVEL], rather than int* transform_block[MAX_LEVEL]. 如果要在function()中使用局部数组,则应声明int transform_block [MAX_LEVEL],而不是int * transform_block [MAX_LEVEL]。 That should make the warning go away. 那应该使警告消失。

void function()
{
int transformed_block[MAX_LEVEL]; pthread_t thread1;
// * removed from above line
dt *data1;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM