简体   繁体   English

如何将函数中的一个参数转换为void指针参数到整数

[英]How to cast one parameter in function as void pointer parameter to an integer

I am having this function fun, where I am passing the nums structure as a parameter. 我在使用此函数很有趣,在这里我将nums结构作为参数传递。 The thing is that I need to convert this field into an integer inside the function. 问题是我需要将此字段转换为函数内部的整数。 How can I do this without changing the way I am passing the structure in the function? 如何在不更改在函数中传递结构的方式下执行此操作?

Here is what i am trying to do: 这是我想做的事情:

struct node{
    char *str;
    struct node *next;
};

struct numbers{
    struct node *head;
    int *new_a;
};

void *fun(void *args);

int main(int argc , char *argv[])
{
        int *new_a, num_a;
        struct node *head=NULL;

        struct numbers *args = (struct numbers *)malloc(sizeof(struct numbers));

        num_a = returnNum();

        pthread_t pthread;
        new_a = malloc(1);
        *new_a = num_a;
        args->new_a=new_a;

        if( pthread_create( &pthread , NULL , (void *) &fun , (void *) &args) < 0)
        {
            perror("could not create thread");
            return 1;
        }

}

void *fun(void *args){

    //void *num_a = (int *) args->new_a;
    //int num_a = *(int*)(args->new_a);
    struct numbers *temp_str = (struct numbers *) (*args);
    int num_a = (int) *(args->new_a);
    ...
}

Also, how can I do the casting for the head node? 另外,如何对头节点进行转换? Can anyone please advice? 任何人都可以请教吗?

Since a struct numbers * is being passed to fun , you need to assign the parameter to a variable of this type. 由于将struct numbers *传递给fun ,因此您需要将参数分配给这种类型的变量。 Then you can use the struct. 然后,您可以使用该结构。

void *fun(void *arg){
    struct numbers *temp_str = arg;   // no need to cast from void *
    int num_a =  temp_str->new_a;
    ...
}

There's also a problem with how you populate the struct: 如何填充结构也存在问题:

    int *new_a, num_a;
    ...
    new_a = malloc(1);
    *new_a = num_a;
    args->new_a=new_a;

You're not allocating enough space for new_a . 您没有为new_a分配足够的空间。 You only allocate 1 byte, but an int on most systems is 4 bytes. 您只分配1个字节,但是在大多数系统上,一个int是4个字节。 When you subsequently read and write from this memory location, you read/write past the end of the memory that was allocated. 当您随后从该内存位置进行读写时,您将在已分配内存的末尾进行读写。 This invokes undefined behavior which in this case manifests as a crash.. 这将导致未定义的行为 ,在这种情况下将显示为崩溃。

You can fix this by allocating the proper amount of space: 您可以通过分配适当的空间来解决此问题:

new_a = malloc(sizeof(*new_a));

However, you don't need to use dynamic memory allocation for this field at all. 但是,您根本不需要为此字段使用动态内存分配。 Just declare new_a as int and write to it directly: 只需将new_a声明为int并直接写入即可:

struct numbers{
    struct node *head;
    int new_a;
};

...

args->new_a = returnNum();

You also don't need to take the address of args . 您也不需要使用args的地址。 It's a pointer, so pass it to pthread_create directly: 它是一个指针,因此将其直接传递给pthread_create

if( pthread_create( &pthread , NULL , fun , args) < 0)

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

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