Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 警告:wint到poiter cast_C++_Linux_Multithreading - Fatal编程技术网

C++ 警告:wint到poiter cast

C++ 警告:wint到poiter cast,c++,linux,multithreading,C++,Linux,Multithreading,我不知道为什么会出现编译器警告和分段错误 有可能去看看吗 struct thread_data{ int thread_id; char *message; }; void *print_message_function( void *ptr ) { int s,j; char *line; line = (char *) ptr; s=sizeof(line); struct thread_data td[s];

我不知道为什么会出现编译器警告和分段错误

有可能去看看吗

struct thread_data{
    int  thread_id;
    char *message;
};


void *print_message_function( void *ptr )
{    
    int s,j;
    char *line;
    line = (char *) ptr;
    s=sizeof(line);
    struct thread_data td[s];

    printf("%s \n",line[2]);

    for(j=0;j+3<s;j++) {
        td[j].thread_id = (int)line[j+3];
        td[j].message = (char *)line[j+1];
        printf("message : %s \n ",td[j].message);
    }
 }
struct-thread\u数据{
int-thread_-id;
字符*消息;
};
void*打印消息功能(void*ptr)
{    
int s,j;
字符*行;
行=(字符*)ptr;
s=sizeof(线);
结构线程_数据td[s];
printf(“%s\n”,第[2]行);

对于(j=0;j+3有一些不好的地方:

  • 为什么接收void*参数并将其强制转换,为什么不接受char*行作为参数
  • 为什么要使用
    sizeof
    而不是
    strlen
    sizeof
    行正好是指针的大小(我想是4个字节)
  • 这:
    struct-thread\u-data-td[s]
    非常糟糕。你不能静态分配一个未知数字的数组。在你的例子中,它是已知的,因为你犯了错误2,但我不确定编译器是否知道。使用
    malloc
  • 行[2]
    是一个字符,为什么要使用
    %s
    而不是
    %c
    ?-这可能是导致警告/分段错误的原因。它使用您在那里的任何字符作为指针,并访问非法内存
  • 由于2,循环将运行一次

  • 请阅读C教程。

    @ElhamKargar-记住接受(左侧投票按钮下的勾号)有助于解决您的问题的答案。