C: PThread_create Parsing Char[] parameter to function - c

Hallo All,
I have this method:
void *readFileLocal(char filename[]){
.....
}
Now i want to start this method a a thread:
char input[strlen(argv[1])];
strcpy(input,argv[1]);
pthread_t read,write;
pthread_create(&read, NULL, &readFileLocal, &input);
But during compilation it gives this warning:
file.c:29: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(char *)’
How can I parse an char array to my funcation over pthread_create without this warning ?
Thanks for helpt

Just use this:
pthread_create(&read, NULL, readFileLocal, input);
And consider changing your function's signature to:
void *readFileLocal(void *fileName) { }
When you are passing a pointer to function (like the one you are using in readFileLocal parameter) you don't need to put &.
Also, when you have an array (like input in your case) you don't need & since in C arrays can be used as pointers already.

Functions for threads need to be prototyped:
void *func(void *argv);
As with all void pointers you then need to interpret ("cast") the pointer to a meaningful entity. You readFileLocal functions then becomes:
void *readFileLocal(void *argv)
{
char *fname = argv; // Cast to string
// Rest of func
}

Related

C ADT function pointer as parameter?

I have a function in an ADT:
Pgroup new_group(int size, void (*foo)(void *));
In my other class I have this function to send in:
void foo(Pstruc x);
x is a pointer to a struct. When I try to call new_group however, I receive an error "expected 'void (*)(void )' but argument is of type 'void ()(struct struc_ *)". This is how I've been calling it:
Pgroup group = new_group(num, &foo);
Any suggestions?
You can cast the argument to the correct type to get rid of the diagnostic:
Pgroup group = new_group(num, (void (*)(void *)) foo);
Note that this is not portable as C does not guarantee that the representation between different pointers is the same. The best would be to use types that match in their declarations.
You can rewrite your original function from void foo(Pstruc x); to void foo(void* x);.
Or convert its type: (void(*)(void*))foo.

expected 'void (**)(void *, const char *)' but argument is of type 'void (*)(void *, const char *)

I can't figure out what
void (**)(void *, const char *)
/* ^^ why are there 2 asterisks here?
means, it's a pointer to a function but I fail to
The exact error message is
expected 'void (**)(void *, const char *)' but argument is of type 'void (*)(void *, const char *)'
initGenericErrorDefaultFunc (xmlGenericErrorFunc *handler);
^
/usr/include/libxml2/libxml/xmlerror.h:866:
this is the default error message function in libxml2, the function that I am trying to call is
initGenericErrorDefaultFunc (xmlGenericErrorFunc *handler);
and my handler argument function is
void
skipErrorPrinting(void *ctx, const char *msg, ...)
{
}
then I call initGenericErrorDefaultFunc() like this
initGenericErrorDefaultFunc(skipErrorPrinting);
and here the definition of xmlGenericErrorFunc
typedef void (XMLCDECL *xmlGenericErrorFunc) (void *ctx,
const char *msg,
...) LIBXML_ATTR_FORMAT(2,3);
It is pretty wonky, it wants to return the default error handler. So you have to pass a pointer to a variable. Like this (untested):
xmlGenericErrorFunc handler;
initGenericErrorDefaultFunc(&handler);
If I understand your intentions properly, this is not the function you actually want to use to suppress errors. Use xmlSetGenericErrorFunc() instead. You can use initGenericErrorDefaultFunc() to restore it again. Pass NULL.
You use xmlGenericErrorFunc *handler. Note the asterisk. xmlGenericErrorFunc is already typedefined with one. Simply remove the asterisk after xmlGenericErrorFunc, it is already a pointer.

error in pointer & multithread expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’

this code check sudoku ,with multithread.
when i running program after compile:
Segmentation fault (core dumped)
int main(){
char t0,t1,t2;
pthread_t row,col,sub1;
t0=pthread_create(&row,NULL,row,NULL); //eror iz here!
t1=pthread_create(&col,NULL,col,NULL);
t2=pthread_create(&sub1,NULL,sub,NULL);
pthread_join(row, NULL);
pthread_join(col, NULL);
pthread_join(sub1, NULL);
exit(EXIT_SUCCESS);
return 0;
}
and Error:
su.c: In function ‘main’:
su.c:87:2: warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
In file included from su.c:4:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’
su.c:88:2: warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
In file included from su.c:4:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’
my func is give 0 argument :void *sub();
and i'm sorry,my English is not good
void * my_row_function(void *param){
Row * myrow = (Row*) param;
//bla bla
}
int main(){
Row * a_row= & row8outof9;
pthread_create(&row,(const pthread_t*)NULL,
my_row_function, a_row);
return 0;
}
You need to pass as third argument a function (that returns void* and accept 1 argument of type void*), that function will receive a pointer that you basically have to cast back to the pointer type you passed in when calling pthread_create
Prototype of pthread_create is this.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
In this, third argument must be a function that returning the void pointer, that takes an argument void pointer.
So make the function as above then pass that to the third argument of pthread_create.
void * thr_fn2(void *arg)// your function must be like this.
Then use that function as argument.

C warning: incompatible pointer types passing [duplicate]

This question already has answers here:
c pthread passing array of type int
(2 answers)
Closed 7 years ago.
I keep getting an error when trying to compile my code. The error is as follows :
warning: incompatible pointer types passing
'void *(threadData *)' to parameter of type 'void * (*)(void *)'
[-Wincompatible-pointer-types]
pthread_create(&threads[id], NULL, start,&data[id]);
I'm trying to pass a struct to the function, void * start(threadData* data), and this keeps throwing me off. Any ideas?
It's complaining about the thread function (bound to the third parameter of pthread_create), you can modify that to take a void * argument and then cast it back before doing anything with it:
void *start (void *voidData) {
threadData *data = voidData;
// rest of code here, using correctly typed data.
You may also opt to coerce the data pointer (fourth parameter) to the expected type:
(void*)(&(data[id]))
but I don't think that's necessary since a void * is supposed to be freely convertible to and from most other pointers.
You can see the problem in this small yet complete program:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
struct sData { char text[100]; };
void *start (struct sData *data) {
printf ("[%s]\n", data->text);
}
int main (void) {
struct sData sd;
pthread_t tid;
int rc;
strcpy (sd.text, "paxdiablo");
rc = pthread_create (&tid, NULL, start, &sd);
pthread_join (tid, NULL);
return 0;
}
When compiling that, you see:
prog.c: In function 'main':
prog.c:20:2: warning: passing argument 3 of 'pthread_create' from
incompatible pointer type [enabled by default]
In file included from prog.c:3:0:
/usr/include/pthread.h:225:12: note: expected
'void * (*)(void *)' but argument is of type
'void * (*)(struct sData *)'
Keep in mind that it's just a warning, not an error but, if you want your code to compile cleanly, it's worth getting rid of. Making the changes mentioned at the top of this answer (bar the data parameter casting) gives you the following thread function:
void *start (void *voidData) {
struct sData *data = voidData;
printf ("[%s]\n", data->text);
}
This compiles without warnings, and runs just fine.

Error incompatible pointer type?

#include<stdio.h>
#include<string.h>
char getInput(char *x[50]);
main (){
char string[50];
getInput(&string);
}
char getInput(char *x[50]){
printf("What is the string?");
gets(*x);
}
I keep getting these errors...
exer7.c:20:2: warning: passing argument 1 of ‘getInput’ from incompatible pointer type [enabled by default]
getInput(&string);
^
exer7.c:5:6: note: expected ‘char *’ but argument is of type ‘char ()[50]’
char getInput(char *x[50]);
I've been changing the pointers and ampersands but I really don't know the proper pointer type, pls help :(
BTW, that's just a code snippet, I have many other user-declared functions I don't need to post here.
void getInput(char (*x)[50]);
int main (){
char string[50];
getInput(&string);
return 0;
}
char *x[50] declares x as array of pointers.&string` is of type pointer to an array. Both types are incompatible.
Change your function declaration to
char getInput(char x[50]);
and call it as
getInput(string);
getInput(&string); You shouldn't pass & of string. Just base address string of the char array need to be passed as the argument.
char getInput(char *x[50]); This formal argument isn't correct too. This should be a pointer to char or array of char of 50 bytes.

Resources