为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

一叶可遮天 的学生作业:

#include int main() { int a[2][3] = {10, 20, 30, 40, 50, 60}; int (*p)[3] = a; // 方法1:使用数组下标访问 printf("方法1:数组下标\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", a[i][j]); } } printf("\n\n"); // 方法2:使用指针p的数组下标 printf("方法2:指针p的数组下标\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", p[i][j]); } } printf("\n\n"); // 方法3:使用指针运算(一级指针) printf("方法3:一级指针遍历\n"); int *ptr = (int *)a; // 或 int *ptr = &a[0][0]; for (int i = 0; i < 6; i++) { printf("%d ", *(ptr + i)); } printf("\n\n"); // 方法4:使用p进行指针运算 printf("方法4:行指针p运算\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", *(*(p + i) + j)); } } printf("\n\n"); // 方法5:纯指针运算(不使用下标) printf("方法5:纯指针运算\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", *(*(a + i) + j)); } } printf("\n\n"); // 方法6:将二维数组当作一维数组访问 printf("方法6:强制转换为一维数组\n"); int *arr = (int *)a; for (int i = 0; i < 2 * 3; i++) { printf("%d ", arr[i]); } printf("\n\n"); // 方法7:通过指针递增 printf("方法7:指针递增\n"); int *q = &a[0][0]; for (int i = 0; i < 6; i++) { printf("%d ", *q++); } printf("\n\n"); // 方法8:通过p访问,使用不同的解引用方式 printf("方法8:混合方式\n"); for (int i = 0; i < 2; i++) { int *row = p[i]; // 获取第i行的首地址 for (int j = 0; j < 3; j++) { printf("%d ", row[j]); } } printf("\n"); return 0; }

得分 100
学习任务

学无止境呀呀呀 的学生作业:

#include #include #include #include #include #include #include #include #define buf_size 1024 struct msgbuf { long mtype; char mtext[buf_size]; }; int main(void) { char pn[] = {"../"}; int pi = 2; char buf[buf_size]; char *ret; pid_t childA,childB; //准备消息队列的key key_t key = ftok(pn, pi); if(key ==-1) { perror("[ERROR] ftok()"); exit(EXIT_FAILURE); } //创建消息队列 int msgid = msgget(key,IPC_CREAT | 0666); if(msgid == -1) { perror("[ERROR] msgid():"); exit(EXIT_FAILURE); } //创建进程A childA = fork(); if(childA == -1) { perror("[ERROR] fork:"); exit(EXIT_FAILURE); } else if (childA == 0) { struct msgbuf rcv_msgA; ssize_t rbytesA; while(msgrcv(msgid,(void *)&rcv_msgA,buf_size,100,0)) { if(rbytesA == -1) { perror("[ERROR] msgrcv:"); exit(EXIT_FAILURE); } printf("mtypeA:%d\n",(int)rcv_msgA.mtype); printf("mtextA:%s\n",rcv_msgA.mtext); } } //进程B childB = fork(); if(childB == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if (childB == 0) { //子进程B struct msgbuf rcv_msgB; while(msgrcv(msgid,(void *)&rcv_msgB,buf_size,200,0)) { printf("mtypeB:%d\n",(int)rcv_msgB.mtype); printf("mtextB:%s\n",rcv_msgB.mtext); } } printf("请输入(quit 退出):\n"); while(fgets(buf,buf_size,stdin)){ if(strcmp("quit\n",buf) == 0 ) { //清空队列 msgctl(msgid,IPC_RMID,NULL); kill(childA,SIGKILL); kill(childB,SIGKILL); exit(EXIT_SUCCESS); } struct msgbuf snd_msg; snd_msg.mtype = 100; strcpy(snd_msg.mtext,buf); msgsnd(msgid,&snd_msg,strlen(snd_msg.mtext)+1,0); snd_msg.mtype = 200; //发送到200 strcpy(snd_msg.mtext,buf); msgsnd(msgid,&snd_msg,strlen(snd_msg.mtext)+1,0); } return 0; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号