将字符串从一个指针复制到另一个指针

时间:2013-02-08 20:37:43

标签: c pointers

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * find_dot();
char * find_end();

int main(int argc, char * argv[]){

  char *file_extension[10];

  int i;
  for(i = 1; i < argc; i++){


    //if an option
    if(argv[i][0] == '-'){
      switch(argv[i][0]){

        default:;
      }


    //otherwise, should be the file
    }else{ 
      char *dot_location_ptr;
      char *end_location_ptr;
      char *filename_ptr = argv[i];

      dot_location_ptr = find_dot(filename_ptr);
      end_location_ptr = find_end(filename_ptr);

      memcpy(file_extension, dot_location_ptr, end_location_ptr - dot_location_ptr);

find_dot返回指向'。'的指针。在参数中,使用strrchr,并且find_end返回指向参数中'\ 0'的指针。

它编译,但我遇到了分段错误。我要做的就是将文件扩展名捕获为字符串,并将该扩展名与其他字符串进行比较。

1 个答案:

答案 0 :(得分:1)

char *file_extension[10];
     ^

您没有声明file_extension正确。你需要一个char数组,而不是一个指针数组。放下*