有人可以解释为什么输出只打印4次 [英] Can someone explain why output is printing only 4 times

查看:75
本文介绍了有人可以解释为什么输出只打印4次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码来创建posix线程print函数是打印p,q用于4个循环,但它不是为第五个循环打印而是调用循环。有人可以解释原因。


#include< stdio.h>

#include< pthread.h>

#define max 5

struct data {

int a;

int b;

};

结构数据s [max];

void * print(void * t)

{

int p, q;

struct data * thread;

thread =(struct data *)t;

p = thread-> a;

q = thread-> b;

printf("%d%d \ n",p,q);

}

int main()

{

pthread_t th [max];

char m [100];

int rc,x = 45,y = 56,i;

for(i = 0; i< max; i ++)

{

scanf("%s",m);

s [i] .a = x;

s [i] .b = y;

//printf("%d",s[i].a);

rc = pthread_create(& th [i],NULL,print,(void *) & s [i]);

if(rc)

printf(" error:%d",rc);

}

}

解决方案

Becau你的使用少于5,因此它只会变为4.所以使它小于或等于5.


但是我从0开始


< blockquote>如果你使用=<


,它包括5

I was writing a code to create posix threads print function is printing p,q for 4 loops but it is not printing for fifth loop but the loop is called. Can someone explain the reason.


#include<stdio.h>
#include<pthread.h>
#define max 5
struct data{
int a;
int b;
};
struct data s[max];
void *print(void *t)
{
int p,q;
struct data *thread;
thread=(struct data *)t;
p=thread->a;
q=thread->b;
printf("%d %d\n",p,q);
}
int main()
{
pthread_t th[max];
char m[100];
int rc,x=45,y=56,i;
for(i=0;i<max;i++)
{
scanf("%s",m);
s[i].a=x;
s[i].b=y;
//printf("%d",s[i].a);
rc=pthread_create(&th[i],NULL,print,(void *)&s[i]);
if(rc)
printf("error:%d",rc);
}
}

解决方案

Because your using less than 5 and so it only goes to 4. So make it less than or equal to 5.


but i was starting from 0


it includes 5 if you use =<


这篇关于有人可以解释为什么输出只打印4次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆