C-指针问题和void方法问题

C-指针问题和void方法问题,c,pointers,pthreads,void,C,Pointers,Pthreads,Void,我对C中的以下代码有问题。基本上,我想创建两个线程,并给它们两个“ergebnis”的整数值。在此之后,线程必须单独计算此值并打印各自的结果 编译时,我遇到以下错误: 3.2.c: In function ‘erhoehenumeins’: 3.2.c:10:13: warning: dereferencing ‘void *’ pointer [enabled by default] 3.2.c:10:13: error: void value not ignored as it ought

我对C中的以下代码有问题。基本上,我想创建两个线程,并给它们两个“ergebnis”的整数值。在此之后,线程必须单独计算此值并打印各自的结果

编译时,我遇到以下错误:

3.2.c: In function ‘erhoehenumeins’:
3.2.c:10:13: warning: dereferencing ‘void *’ pointer [enabled by default]
3.2.c:10:13: error: void value not ignored as it ought to be
3.2.c: In function ‘verdoppeln’:
3.2.c:20:18: error: invalid operands to binary * (have ‘void *’ and ‘int’)   
代码:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void *erhoehenumeins(void * dummy) {
    printf("Thread erhoehenumeins() wurde gestartet\n");
    int temp;
    temp= dummy+1;
    printf("Thread erhoehenumeins() wurde beendet\n");
    printf("Ergebnis=%d\n",temp);
}
void *verdoppeln(void * dummy) {
    printf("Thread verdoppeln() wurde gestartet\n");
    int temp=dummy*2;
    printf("Thread verdoppeln() wurde beendet\n");
    printf("Ergebnis=%d\n",temp);
}

int main() {
    int ergebnis=3;
    pthread_t thread1, thread2;
    // Thread 1 erzeugen
    pthread_create( &thread1, NULL, &erhoehenumeins, &ergebnis );

    // Thread 2 erzeugen
    pthread_create( &thread2, NULL, &verdoppeln, &ergebnis );

    // Main-Thread wartet auf beide Threads.
    pthread_join( thread1, NULL );
    pthread_join( thread2, NULL );
    printf("\nHaupt-Thread main() wurde beendet\n");
    exit(0);
}
#包括
#包括
#包括
#包括
#包括
void*erhoehenumeins(void*dummy){
printf(“线程erhoehenumeins()wurde-gestaret\n”);
内部温度;
温度=虚拟+1;
printf(“线程erhoehenumeins()wurde beendet\n”);
printf(“Ergebnis=%d\n”,temp);
}
void*verdoppeln(void*dummy){
printf(“Thread verdoppeln()wurde-gestaret\n”);
int temp=虚拟*2;
printf(“Thread verdoppeln()wurde beendet\n”);
printf(“Ergebnis=%d\n”,temp);
}
int main(){
int ergebnis=3;
pthread_t thread1,thread2;
//螺纹1 erzeugen
pthread_create(&thread1,NULL,&erhoehenumeins,&ergebnis);
//螺纹2 erzeugen
pthread_create(&thread2,NULL,&verdoppeln,&ergebnis);
//主螺纹瓦特奥夫贝德螺纹。
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf(“\n上传线程main()wurde beendet\n”);
出口(0);
}
谢谢你的帮助

这样做:

void * erhoehenumeins(void * dummy)
{
    int * p = (int *) dummy;
    ++*p;

    return NULL;
}
以及:

当然,这是一种未定义的行为,完全被破坏了,但现在应该这样做

int temp=dummy*2;
dummy
是一个
void*
——编译器不能将其乘以2

也许你应该这样做“


好吧,对于初学者来说,您不会从函数中返回任何东西……只是一个旁注:缩进(或者更确切地说是缺少缩进)使其比需要的更难阅读。关于您的问题:非常感谢您的快速回答。We当我按照您的解释进行操作时,我得到了错误:3.2.c:在函数“verdoppeln”中:3.2.c:20:8:警告:赋值从整数生成指针而不使用强制转换[默认启用]我不明白为什么;也许你应该用新代码编辑你的问题。
int temp=dummy*2;
int temp = (*(int *)dummy)*2;