【发布时间】: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返回一个unsignedint 而stringStat返回一个 int。 -
请用
-Wall -Wextra -Wpointer-arith编译 -
printf("%d", stringStat("hello", 2, 2));应该是int two=2; printf("%d", stringStat("hello", 2, &two));