【问题标题】:An exercise for c + linuxc + linux的一个练习
【发布时间】:2017-02-16 16:23:46
【问题描述】:

“在这个任务中,你需要创建一个共享动态库 libsolution.so 来实现一个具有以下原型的函数:

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 解决方案.c:在函数'main'中: 解决方案.c:19:38:警告:传递'stringStat'的参数3使指针从整数而不进行强制转换 printf("%d", stringStat("hello", 2, 2)); ^ solution.c:12:5:注意:预期为“int *”,但参数的类型为“int” int stringStat(const char *string, int multiplier, int *count){

没有第 18-21 行的错误:

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

parallels @ debian-gnu-linux-8: ~/labs/lab1 $ ./solution bash: ./解决方案:无法执行二进制文件:执行格式错误

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

【问题讨论】:

  • 你将2(一个值)传递给int *count的stringStat函数......这是完全错误的。你应该开始研究一些关于先驱者的东西
  • 你使用了-c,所以gcc会生成一个目标文件而不是一个可执行文件。
  • 旁注:strln 返回一个 unsigned int 而stringStat 返回一个 int。
  • 请用-Wall -Wextra -Wpointer-arith编译
  • printf("%d", stringStat("hello", 2, 2)); 应该是int two=2; printf("%d", stringStat("hello", 2, &amp;two));

标签: c linux string


【解决方案1】:

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

把这个:

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

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

【讨论】:

  • 大卫,你能写出正确的变体吗?我需要查看代码才能捕捉到它。
【解决方案2】:

我希望stringStat() 的返回值为size_t。但是,正如您所说,该任务需要遵守函数原型:

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

这就是我们将使用的。值得注意的是,标准库函数strlen() 的返回类型为size_t,这是一个保证能够容纳任何数组索引的unsigned 整数类型(因此适合此用途)。我还想将len 和count 更改为size_t 和size_t *,但我们将它们保留为int 和int * 以匹配函数原型。

现在,您提供的规范中提到了 stringStat() 函数:

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

我将此解释为函数的返回值是 string 的长度,它是输入字符串的multiplier 倍。本来以为count这个地址要存储的值应该是存放这样一个字符串的字符数组的大小,也就是这个字符串的乘积加上空格\0 终结者。这不太有意义,但是保持一个计数器 stringStat() 被调用的次数也没有多大意义,所以我会遵从你的代码,它似乎在后一种意义上保持一个计数器。

把这一切放在一起,strln() 函数看起来不错,除了在其他情况下我建议在这里使用size_t 而不是int。 stringStat() 函数看起来不错。我更改了main() 中的代码,以更清楚地了解不同变量的情况。特别是,您将整数常量 2 作为第三个参数传递给 stringStat(),这导致了原始错误。你需要在这里传递一个指向int 的指针。所以我声明了int 变量main_counter,将它初始化为零。 main_counter 的地址是指向int 的指针,并被传递给函数。在stringStat() 函数中,存储在count 所持有的地址的值(存储在main_counter 中的值)在返回乘积后的字符串长度之前递增。每次调用stringStat(),main_counter中的值都会增加。

#include <stdio.h>

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

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

int stringStat(const char *string, int multiplier, int *count)
{
    int new_len = multiplier * strln(string);

    *count = *count + 1;
    return new_len;
}

int main(void)
{
    int main_counter = 0;
    char *str = "hello";
    size_t mult = 2;
    int newstr_len = stringStat(str, mult, &main_counter);

    printf("Original string: %s  ---  length: %d\n",
           str, strln(str));
    printf("String length multiplied by %zu\n", mult);
    printf("New string length: %d\n", newstr_len);
    printf("Times stringStat() called: %d\n", main_counter);

    return 0;
}

程序输出:

Original string: hello  ---  length: 5
String length multiplied by 2
New string length: 10
Times stringStat() called: 1

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 2022-11-21
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2020-10-21
    相关资源
    最近更新 更多