将(void const * x)转换为(unsigned char const * y)

时间:2013-07-29 13:50:36

标签: methods types parameter-passing

我正在浏览一个源代码,分析其实现,我已经定义了一个方法:

unsigned int rs_calc_weak_sum(void const *p, int len) {
unsigned char const    *buf = (unsigned char const *) p;
}

应该将哪种类型的参数传递给此方法??

请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:2)

任何指针都可以传递给void *参数。应该传递什么,取决于代码对该参数的作用。

char array[12] = "Hello World";
unsigned in res = 0;
res = rs_calc_weak_sum(array, 12);

#include <stdio.h>
int main ( void )
{
  char filename[] = "file.txt";
  FILE *file = fopen ( filename, "r" );

  if (file != NULL) {
    char line [1000];
    while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ {
      res = rs_calc_weak_sum(line, 1000);
    }

    fclose(file);
  }
  else {
    perror(filename); //print the error message on stderr.
  }

  return 0;
}
相关问题