问题描述:

I am having trouble getting integer values from a struct via a pointer, instead of getting the intended value am getting pointer address.

I am in main.c

struct sonicPins *args1;

int main(){

pthread_t thr1, thr2, thr3, thr4;

//void(*foo1)(int, int, int);

//void(*foo2)(void *args);

//foo1 = &calcSonicS;

//foo2 = &threadFunc;

args1 = malloc(sizeof(struct sonicPins));

args1->trig1 = 21;

args1->echo1 = 20;

//front right.

args1->trig2 = 16;

args1->echo2 = 12;

//rear left;

args1->trig3 = 26;

args1->echo3 = 19;

//rear right.

args1->trig4 = 13;

args1->echo4 = 6;

//printf("print args %d", args1.trig1);

//if(args1!=NULL){

pthread_create(&thr1, NULL, &threadFunc,(void*) &args1);

//}

sonicThread.h

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<signal.h>

#include<string.h>

#include<errno.h>

#include<sys/time.h>

#include<wiringPi.h>

typedef struct sonicPins{

//front left.

int trig1;

int echo1;

//front right.

int trig2;

int echo2;

//rear left;

int trig3;

int echo3;

//rear right.

int trig4;

int echo4;

}args;

extern void* threadFunc(void *sonicPinsPtr);

void* threadFunc(void *sonicPinsPtr)

{

struct sonicPins *args = sonicPinsPtr;

int trig = args->trig1;

int echo = args->echo1;

printf("threadFunc got trigger value: %d and echo value %d\n", echo, trig);

value = setup(trig, echo);

printf("distance measured %d\n", value);

}

Printing the distance will not work at this stage due to the echo and trig not getting their intended pin value. The other print test is out putting incorrect values entirely.

Its outputting

threadFunc got trigger value: 0 and echo value 26222600

Should only be 2 digit integers, I have no idea how to fix this issue.

网友答案:

You have an extra level of indirection when you pass args1 to pthread_create. args1 is already a pointer.

Change:

pthread_create(&thr1, NULL, &threadFunc,(void*) &args1);

to:

pthread_create(&thr1, NULL, &threadFunc,(void*) args1);
相关阅读:
Top