c - 当我们尝试在函数中复制字符数组时出现段错误

标签 c arrays pointers

<分区>

当我尝试将一些值复制到从主函数传递的 char 数组时,我遇到了段错误。

请找到附件中的代码示例,请帮助我哪里出错了。

#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 工作正常但通过函数调用时它给出了段错误。请帮助我。

#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; 
}

最佳答案

这是不正确的:

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");

Don't ignore warnings and recommend compiling with warning level at maximum and to treat warnings as errors. A segmentation fault is occurring because the value of val[0] is, incorrectly, being used as a memory address which strcpy() is attempting to write to.

To correct, change to:

strcpy(val,"abcdef");

关于c - 当我们尝试在函数中复制字符数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476009/

相关文章:

arrays - Ruby 可能的数组值组合 - 性能

C#:使用指针类型作为字段?

c++ - 通过引用传递解引用的指针时了解C++堆/堆栈分配

检查字符串输入中定义的字符

c - 什么?在 gdb backtrace 中意味着以及如何获得实际的堆栈帧?

c - pthread_cond_wait 期间 SIGINT 的 Valgrind 错误

java - 有没有办法用其他数组初始化二维数组?

c++ - 如何使用带有标题和 .so 文件的库?

java - 将负值的 byte[] 转换为 String

c - 你如何在 C 中创建一个指针数组?