当前位置:首页 > 通信资讯 > 正文

c语言调用数组中的数(C语言数组操作)

C语言setgroups()函数:设置组代码函数
头文件:

?
1 #include <grp.h>

定义函数:

?
1 int setgroups(size_t size, const gid_t * list);

函数说明:setgroups()用来将list 数组中所标明的组加入到目前进程的组设置中. 参数size 为list()的gid_t 数目, 最大值为NGROUP(32)。

返回值:设置成功则返回0, 如有错误则返回-1.

错误代码:

  • EFAULT:参数list 数组地址不合法.
  • EPERM:权限不足, 必须是root 权限
  • EINVAL:参数size 值大于NGROUP(32).

C语言getgroups()函数:获取组代码函数
头文件:

?
1 #include <unistd.h> #include <sys/types.h>

定义函数:

?
1 int getgroups(int size, gid_t list[]);

函数说明:getgroup() 用来取得目前用户所属的组代码. 参数size 为list() 所能容纳的gid_t 数目. 如果参数size 值为零, 此函数仅会返回用户所属的组数。

返回值:返回组识别码, 如有错误则返回-1.

错误代码:

  • EFAULT:参数list 数组地址不合法.
  • EINVAL:参数size 值不足以容纳所有的组.

范例

?
1 2 3 4 5 6 7 8 9 10 11 #include <unistd.h> #include <sys/types.h> main() { gid_t list[500]; int x, i; x = getgroups(0, list); getgroups(x, list); for(i = 0; i < x; i++) printf("%d:%d\n", i, list[i]); }

执行:

?
1 2 3 4 5 6 7 0:00 1:01 2:02 3:03 4:04 5:06 6:10
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。