Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

Getting error when using puts to output 2d char array in c when sending array as void pointer


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#define size 5


void *displayName(void *received_array){ 


char *name = received_array;


for(int i=0;i<5;i++)

     puts(name[i]);


 pthread_exit(0);

 }
 
int main(){
 pthread_t threadid1;

char name[10][50];
strcpy(name[2], "raj");
strcpy(name[3], "pol");
strcpy(name[4], "sara");*/

pthread_create(&threadid1,NULL,displayName, name);

}


In function ‘displayName’: q2v2.c:42:15: warning: passing argument 1 of ‘puts’ makes pointer from integer without a cast [-Wint-conversion] 42 | puts(name[i]); | ~~~~^~~ | | | char

question from:https://stackoverflow.com/questions/66064574/getting-error-when-using-puts-to-output-2d-char-array-in-c-when-sending-array-as

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should match the type of passed pointers.

Replace

char *name = received_array;

with

char (*name)[50] = received_array;

Also don't forget to initialize name[0] and name[1] in the main() function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...