免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1922 | 回复: 9
打印 上一主题 下一主题

[VMware] Vmware 4.5 中Linux下C编程奇怪的问题?求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-09 08:59 |只看该作者 |倒序浏览
各位大哥,俺最近在学习Linux下C 编程的过程中发现两个问题:

问题1:俺在使用scanf("please enter a num:%d", &num); 函数时发现:编译链接都正常,但是在运行时,根本没有提示输入的这句话"lease enter a num:"出现,也就是说程序执行到这行代码时就停下了。本来该提示输入数字的啊?可就没反映,然后随便输入一个数值的话,num的值也不是输入值。

注:程序是书上的例子,我检查过,应该没问题,我怀疑是不是在vmware运行linux的缘故?

问题2:在学习信号量那一节时,竟发现

11_4.c:11:warning: 'union semnu' declared inside in paramter list
11_4.c:11:warning: its scope is only this definition or declaration which is probably not you want.
11_4.c:in function 'main':
11_4.c:17:storage size of 'semopt' isn't known.
....
....

这个问题也是书上的例子
各位大哥帮忙看看是怎么 回事?

论坛徽章:
0
2 [报告]
发表于 2004-07-09 09:08 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

第二个问题主要是不明白,为什么正常的使用联合体时就出现了这种情况?

程序如下:
#include <sys/types.h>;
#include <sys/ipc.h>;
#include <sys/sem.h>;
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;

#define SEM_NUM 10
#define SEM_MODE (IPC_CREAT|0660)

void printfmode(union semun *arg);
void changemode(int sid, char* mode);

int main(void)
{
        int semid;
        union semun semopts;
        struct semid_ds semds;

        if((semid = semget(IPC_PRIVATE, SEM_NUM, SEM_MODE)) == -1)
        {
                fprintf(stderr, "semget error!\n";
                exit(1);
        }
       
        semopts.buf = &amp;
        if(semctl(semid, IPC_STAT, semopts) == -1)
        {
                fprintf(stderr, "get semid_ds error!\n";
                exit(1);
        }
        printfmode(&semopts);

        changemode(semid, "0600";

        if(semctl(semid, IPC_STAT, semopts) == -1)
        {
                fprintf(stderr, "get semid_ds error!\n";
                exit(1);
        }

        printfmode(&semopts);

        if(semctl(semid, IPC_RMID, 0) < 0)
        {
                fprintf(stderr, "semctl error!\n";
                exit(1);
        }

        exit(0);

}

论坛徽章:
0
3 [报告]
发表于 2004-07-09 09:13 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

少了定义semun的头文件!

论坛徽章:
0
4 [报告]
发表于 2004-07-09 09:13 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

11_4.c:11: warning: `union semun' declared inside parameter list
11_4.c:11: warning: its scope is only this definition or declaration, which is probably not what you want.
11_4.c: In function `main':
11_4.c:17: storage size of `semopts' isn't known
11_4.c:17: warning: unused variable `semopts'
11_4.c: At top level:
11_4.c:54: warning: `union semun' declared inside parameter list
11_4.c:55: conflicting types for `printfmode'
11_4.c:11: previous declaration of `printfmode'
11_4.c: In function `printfmode':
11_4.c:56: dereferencing pointer to incomplete type
11_4.c: In function `changemode':
11_4.c:63: storage size of `semopts' isn't known
11_4.c:63: warning: unused variable `semopts'

论坛徽章:
0
5 [报告]
发表于 2004-07-09 09:15 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

[quote]原帖由 "kj501"]少了定义semun的头文件![/quote 发表:


semnu是信号量里面的吧,应该已经包含在sys/sem.h里了啊。

论坛徽章:
0
6 [报告]
发表于 2004-07-09 09:22 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

既然这样,你自己为什么不去看看sys/sem.h中有没有这个定义?

论坛徽章:
0
7 [报告]
发表于 2004-07-09 09:37 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

[quote]原帖由 "kj501"]既然这样,你自己为什么不去看看sys/sem.h中有没有这个定义?[/quote 发表:


这位大哥果然高人,sys/sem.h果然没union semun的定义啊,小弟新手究竟该如何修改呢?

论坛徽章:
0
8 [报告]
发表于 2004-07-09 10:43 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

第二个问题该如何处理?到底包含哪个头文件?

论坛徽章:
0
9 [报告]
发表于 2004-07-09 11:00 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

man semctl

论坛徽章:
0
10 [报告]
发表于 2004-07-09 11:28 |只看该作者

Vmware 4.5 中Linux下C编程奇怪的问题?求助

多谢两位,已经搞定了,原来书上也写错了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP