有人可以解释为什么输出只打印4次 [英] Can someone explain why output is printing only 4 times
问题描述
我正在编写一个代码来创建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>如果你使用=<
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屋!