简体   繁体   English

尝试复制阵列C时出现分段错误(核心已转储)

[英]Segmentation Fault (core dumped) when trying to copy array C

When i run the following Segmentation Fault... The goal is to copy the argv array to allowdip array. 当我运行以下Segmentation Fault ...时,目标是将argv数组复制到allowdip数组。

char *allowdip;
int *allowdipcount;


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

  allowdipcount = argc;

  for(xer=0; xer<allowdipcount; xer++) {
    allowdip[xer]=argv[xer];
  }

  for(xir=0; xir<allowdipcount -1; xir++) {
    printf("%s\n", allowdip[xir]);
  }


exit(EXIT_SUCCESS);
}

Any ideas on what I'm doing wrong? 关于我在做什么错的任何想法吗?

UPDATE UPDATE

Thanks, now my code is: 谢谢,现在我的代码是:

char **allowdip;
int allowdipcount;

int main(int argc, char *argv)
{
  int xer;
  int xir;

  allowdipcount = argc;
  char **allowdip = malloc(allowdipcount * sizeof(char*));
  for(xer=0; xer<argc; xer++) {
      allowdip[xer]=argv[xer];
  }

    for(xir=1; xir<allowdipcount; xir++)
    printf("%s\n", allowdip[xir]);
    exit(EXIT_SUCCESS);

}

it returns: 它返回:

 testscript2.c:51: warning: assignment makes pointer from integer without a cast

the line 51 contains: 第51行包含:

 allowdip[xer]=argv[xer];

allowdip is an uninitialised pointer. allowdip是未初始化的指针。 You need to allocate memory for it 您需要为其分配内存

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

int main(int argc, char *argv[])
{
  int xer;
  int allowdipcount = argc;
  char **allowdip = malloc(allowdipcount * sizeof(char*));

  for(xer=0; xer<allowdipcount; xer++) {
      allowdip[xer]=argv[xer];
  }

  for(xer=0; xer<allowdipcount; xer++) {
    printf("%s\n", allowdip[xer]);
  }
  free(allowdip);
  return EXIT_SUCCESS;
}

Note that I've made a few other changes in your code 请注意,我在代码中做了一些其他更改

  • allowdipcount should be of type int (otherwise you need to allocate storage for it too) allowdipcount应为int类型(否则,您也需要为其分配存储空间)
  • allowdip has changed type to be an array of char pointers allowdip已将类型更改为char指针数组
  • the signature of main wasn't quite right - argv should be a char* array main的签名不太正确argv应该是char*数组
  • Changed your global variables to be local to main since there wasn't an obvious need for them to be global 将全局变量更改为局部变量到main变量,因为显然不需要全局变量
  • Changed the printf loop to iterate over all program arguments. 更改了printf循环以遍历所有程序参数。 It was skipping the final arg in your question. 它跳过了您问题中的最后一个参数。
  • freed the memory we allocated for allowdip once we're finished with it 完成后,释放我们分配给allowdip的内存
  • Simplified return from main as suggested by Vincent 文森特(Vincent)建议简化main

You're not allocating memory, and your types are all wrong. 您没有分配内存,并且您的类型都是错误的。

The counter should be an integer, not a pointer: 计数器应该是整数,而不是指针:

int allowdipcount;

and the array should be an array of pointers, not of characters: 并且该数组应该是一个指针数组,而不是字符数组:

char **allowdip;

Then you can allocate: 然后,您可以分配:

allowdip = malloc(argc * sizeof *allowdip);

and copy the array: 并复制数组:

memcpy(allowdip, argv, argc * sizeof *allowdip);

Note that this doesn't copy the actual argument strings, only the array of pointers to strings. 请注意,这不会复制实际的参数字符串,只会复制指向字符串的指针数组。 It also doesn't include the NULL pointer at argv[argc] which terminates the array. 它还在argv[argc]处不包含用于终止数组的NULL指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM