从虚空*转换为char **用C [英] Converting from void* to char ** in C
问题描述
我有铸造,我传递给线程处理器function.I我不能强制转换乌塞林
返回字符串array.Suggestions请在字符串数组中重类问题
静态字符*命令;
无效* thread_handler1(无效*乌塞林)
{
字符文件名1 [20] =(字符*)乌塞林;
看跌期权(内螺纹);
的printf(%U(INT)乌塞林);
//的printf(%U,文件名1);
如果(STRCMP(指挥,监控)== 0)
{
dms_monitor(文件名);
的printf(%S,文件名1);
}
}无效transact_client(INT袜子)
{
INT I;
焦炭recvbuffer [MAX_BUFFER_SIZE]
字符分隔符[] =;
字符*结果= NULL;
字符*文件名[10];
INT rdcount = 0;
INT索引= 0;
//结构文件的用户文件[20];
//字符*命令;
PID的pthread_t;
如果((rdcount =的recv(袜子,recvbuffer,sizeof的(recvbuffer),0))== - 1)
PERROR(问题与文件描述符);
结果=的strtok(recvbuffer,分隔符);
命令=结果;
的printf(命令为%s \\ n,文件名[0]);
而(结果!= NULL)
{
//输出(结果是\\%s \\的\\ n,文件名[指数]);
结果=的strtok(NULL,分隔符);
文件名[++指数] =结果;
的printf(结果是\\%s \\的\\ n,文件名[索引1]);
} 的printf(%U,文件名);
在pthread_create(安培; PID,NULL,thread_handler1,&安培;文件名);
pthread_detach(pthread_self());
现在的问题是这一行:
字符文件名1 [20] =(字符*)乌塞林;
右侧是一个char *和左侧字符的阵列。
如果乌塞林
指向一个字符串,没有必要去尝试为它分配新的空间。
试试这个:
的char *文件名1 =(字符*)乌塞林;
编辑:
有在 transact_client
以及一些错误。
文件名
被声明为指针数组。真的是你想要的吗?
文件名[0]
从未赋值。
&安培;文件名
(这是字符***
)发送给需要一个char功能*。
I am having problem in re type casting the string array which I passed to the thread handler function.I am not able to typecast userIn
back to string array.Suggestions please.
static char *command;
void *thread_handler1 (void *userIn)
{
char filename1[20]= (char *)userIn;
puts("inside thread");
printf("%u",(int)userIn);
//printf("%u",filename1);
if(strcmp(command,"monitor")==0)
{
dms_monitor(filename1);
printf("%s",filename1);
}
}
void transact_client(int sock)
{
int i;
char recvbuffer[MAX_BUFFER_SIZE];
char delimiter[]=" ";
char *result=NULL;
char *filename[10];
int rdcount=0;
int index=0;
//struct file userFile[20];
// char *command;
pthread_t pid;
if((rdcount=recv(sock,recvbuffer,sizeof(recvbuffer),0))==-1)
perror("Problem with file descriptor");
result=strtok(recvbuffer,delimiter);
command=result;
printf("the command is %s\n",filename[0]);
while(result != NULL)
{
// printf("result is \"%s\"\n",filename[index]);
result=strtok(NULL,delimiter);
filename[++index]=result;
printf( "result is \"%s\"\n", filename[index-1]);
}
printf("%u",filename);
pthread_create(&pid,NULL,thread_handler1,&filename);
pthread_detach(pthread_self());
The problem is with this line:
char filename1[20]= (char *)userIn;
The right side is a char* and the left side an array of char.
If userIn
points to a string, there is no need to try to allocate new space for it.
Try this instead:
char *filename1 = (char *)userIn;
Edit:
There are a number of bugs in transact_client
as well.
filename
is declared as an array of pointers. Is that really what you want?
filename[0]
is never assigned a value.
&filename
(which is a char***
) is sent to a function that expects a char*.
这篇关于从虚空*转换为char **用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!