小编Nit*_*dam的帖子

为什么将 pid 保留在 char 中,始终为 0?

#include <stdio.h>
#include <string.h>
#include <unistd.h>

struct mystruct {
  char string[3];
  char pid
};

int main(int argc, char** argv) {
  struct mystruct info;
  info.pid = fork();
  strcpy(info.string, "Son");
  if (info.pid == 0)
    printf("%s", info.string);
  else
    printf("Father");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码打印

Son
Son
Run Code Online (Sandbox Code Playgroud)

我想知道为什么。

c fork

1
推荐指数
1
解决办法
102
查看次数

标签 统计

c ×1

fork ×1