duangu1878 2017-09-01 10:03
浏览 1466

将字符串数组作为参数从C函数传递到

I have one C function:

int cgroup_change_cgroup_path(const char * path, pid_t pid, const char *const  controllers[])

I want to call it in go language by using cgo. How to pass the third parameter as it accepts a C array of string.

  • 写回答

1条回答 默认 最新

  • doucanshou6998 2017-09-01 14:36
    关注

    You can build the arrays using c helper functions and then use them.

    Here is a solution to the same problem:

    // C helper functions:
    
    static char**makeCharArray(int size) {
            return calloc(sizeof(char*), size);
    }
    
    static void setArrayString(char **a, char *s, int n) {
            a[n] = s;
    }
    
    static void freeCharArray(char **a, int size) {
            int i;
            for (i = 0; i < size; i++)
                    free(a[i]);
            free(a);
    }
    
    // Build C array in Go from sargs []string
    
    cargs := C.makeCharArray(C.int(len(sargs)))
    defer C.freeCharArray(cargs, C.int(len(sargs)))
    for i, s := range sargs {
            C.setArrayString(cargs, C.CString(s), C.int(i))
    }
    

    golangnuts post by John Barham

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致