当我们尝试复制函数中的字符数组时出现分段错误

时间:2014-05-05 15:16:43

标签: c arrays pointers

当我尝试将一些值复制到从main函数传递的char数组时,我正面临seg错误。

请查找随附的代码示例,请在我出错的地方帮助我。

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

void getVal(char val[]){
strcpy(val[0],"abcdef");      //segfault.
}


int main(int argc, char **argv)
{
char val[64] = { 0 };
getVal(&val[0]);
printf("%s",val);
}

发布实际代码,当数组strcpy的char工作正常但通过函数调用它给出了segfault。请帮帮我。

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "ezxml.h"

static ezxml_t root;

void load_xml(char *root_node){

     root = ezxml_parse_file(root_node);
     printf("%p\n",root);
}

void getParamVal(char *objname, char val[]){

  char *char_strip;
  const char *tmp_buf[20];
  int i=0;

  ezxml_t get_child_tmp;
  const char *attrvalue;

  char_strip = strtok (objname,".");
  while (char_strip != NULL)
  {
    tmp_buf[i] = char_strip;
    char_strip = strtok (NULL, ".");
    i++;
  }



  for (get_child_tmp = ezxml_child(root,tmp_buf[1]); get_child_tmp; get_child_tmp = get_child_tmp->next) {
     attrvalue = ezxml_attr(get_child_tmp, "instance");
     if(!(strcmp(attrvalue,tmp_buf[2]))){
     ezxml_t get_child_level1;
     for (get_child_level1 = ezxml_child(get_child_tmp,tmp_buf[3]); get_child_level1; get_child_level1 = get_child_level1->next) {
         attrvalue = ezxml_attr(get_child_level1, "instance");
             if(!(strcmp(attrvalue,tmp_buf[4]))){       
        ezxml_t get_child_level2;
        for (get_child_level2 = ezxml_child(get_child_level1,tmp_buf[5]); get_child_level2; get_child_level2 = get_child_level2->next) {
          attrvalue = ezxml_attr(get_child_level2,"instance");
                  if(!(strcmp(attrvalue,tmp_buf[6]))){
                       printf("%s\n",ezxml_child(get_child_level2,tmp_buf[7])->txt);
            char s[10];
            strcpy (s,ezxml_child(get_child_level2,tmp_buf[7])->txt);  // fine with local array of char 
            printf("%s\n",s);
            strcpy(val, ezxml_child(get_child_level2,tmp_buf[7])->txt); // problem gives segfault only diff is val is from other function.
                   }
        }
         }
     }
     }
  }
}


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

  if (argc < 2){
  printf("usage: ./test <xml file> ");
  exit(1);
  }

 struct timeval tv;
 unsigned int seconds=0, mseconds=0;

  char val[64] = { 0 };
  char objname[]="a.b.c.d.e.f.c"; 

  load_xml(argv[1]);

 gettimeofday(&tv, NULL);
 seconds = tv.tv_sec;
 mseconds = tv.tv_usec;
 printf("START: [%u][%u]\n", seconds, mseconds);

 getParamVal(&objname[0],&val[0]);  

 gettimeofday(&tv, NULL);
 seconds = tv.tv_sec;
 mseconds = tv.tv_usec;
 printf(" END: [%u][%u]\n", seconds, mseconds);
return 0; 
}

2 个答案:

答案 0 :(得分:5)

这是不正确的:

strcpy(val[0],"abcdef");

因为val[0]的类型是char而不是char*。编译器应该发出关于类型不匹配的警告:

$ gcc main.c -o prog
main.c: In function ‘getVal’:
main.c:6:5: warning: passing argument 1 of ‘strcpy’ makes pointer from 
integer without a cast [enabled by default]
     strcpy(val[0],"abcdef");

请勿忽略警告并建议最高编译警告级别,并将警告视为错误。发生分段错误,因为val[0]的值被错误地用作strcpy()尝试写入的内存地址。

要更正,请更改为:

strcpy(val,"abcdef");

答案 1 :(得分:1)

您需要将指针传递给 strcpy

strcpy(&val[0], "abcdef"); // '&' gets the address of firt element of 'val'

或简单地写

strcpy(val, "abcdef");