c - c + linux的练习

标签 c linux string

“在此任务中,您需要创建一个共享动态库 libsolution.so,它实现了一个具有以下原型(prototype)的函数:

int stringStat (const char * string, size_t multiplier, int * count);

函数返回字符串值的长度乘以乘数,值加1,表示要计数。"

我的代码:

#include <stdio.h>

unsigned strln(const char *str)
{
    unsigned int len = 0;

    while(*str!='\0')
    {
          str++;
          len++;
    }

    return len;
}

int stringStat(const char *string, int multiplier, int *count)
{ 
    *count = *count+1;
    return strln(string)*multiplier;
}

int main(void) { 
    printf("%d", stringStat("hello", 2, 2));
    return 0;
}

第 18-21 行的错误:

parallels@debian-gnu-linux-8:~/labs/lab1$ gcc -o solution solution.c -c solution.c: In function ‘main’: solution.c:19:38: warning: passing argument 3 of ‘stringStat’ makes pointer from integer without a cast printf("%d", stringStat("hello", 2, 2)); ^ solution.c:12:5: note: expected ‘int *’ but argument is of type ‘int’ int stringStat(const char *string, int multiplier, int *count){

没有第 18-21 行的错误:

我使用 -c 选项编译,它可以工作,但是由于某种原因,当您尝试启动 ./solution 时它报告“权限被拒绝”,更正了 chmod,现在在这里:

parallels @ debian-gnu-linux-8: ~ / labs / lab1 $ ./solution bash: ./solution: can not execute binary file: Exec format error

我在这里阅读了很多信息,我理解指针和声明的问题,但我不知道下一步该怎么做。 有点难,请在您的回答中给我一些示例。

最佳答案

您将 2 作为指针传递。 2 是一个整数而不是指向整数的指针。

把这个:

int k = 2;
stringStat("hello", 2, &k);

上面代码中,k是一个整数,等于2&k是变量k<的地址 等在 stringStat 函数中,计数指针开始指向 k

关于c - c + linux的练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279430/

相关文章:

linux - 如果其中包含一些文本,请删除 html 标签

c - execv 是怎么做到的?

c - 没有自引用结构的链表

linux - 通过 Apache 远程访问 JBOSS 管理控制台

Java - 从文件中的特定索引中提取相关字符串/字符

c++ - 将 vector<int> 转换为定界字符串

c - char *str 为 null 和 str[0] == '\0' 有什么区别

c - 如何枚举内核中指定进程的所有打开句柄(Windows)

数组中的矛盾指针

linux - 如何查找倒数第二个命令的退出状态?