“在此任务中,您需要创建一个共享动态库 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/