Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c语言的动态数组中添加/删除字符串_C_Arrays_String - Fatal编程技术网

如何在c语言的动态数组中添加/删除字符串

如何在c语言的动态数组中添加/删除字符串,c,arrays,string,C,Arrays,String,我有一个已定义的数组示例: char *arguments[] = {"test-1","test-2","test-3"}; 我试图添加命令行提供的参数输入。我尝试了strcpy函数,并将其传递给数组元素,例如arguments[num+1]=argv[1],但再次失败 我知道这是一个非常简单的问题,但我不是一个经验丰富的程序员,我所有的经验都来自更高级的编程语言(PHP、Perl) 我在网上找到的最接近的工作样本是和。但这并不是我想要的,他们正在和我需要的人物合作 我的目标是找到一种从动态

我有一个已定义的数组示例:

char *arguments[] = {"test-1","test-2","test-3"};
我试图添加命令行提供的参数输入。我尝试了
strcpy
函数,并将其传递给数组元素,例如
arguments[num+1]=argv[1]
,但再次失败

我知道这是一个非常简单的问题,但我不是一个经验丰富的程序员,我所有的经验都来自更高级的编程语言(PHP、Perl)

我在网上找到的最接近的工作样本是和。但这并不是我想要的,他们正在和我需要的人物合作

我的目标是找到一种从动态数组中添加和删除字符串的方法,该数组可以根据脚本的过程进行增长和收缩

感谢大家花时间和精力帮助我

工作代码示例如下所示:

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

/* Set as minimum parameters 2 */
#define MIN_REQUIRED 2
#define MAX_CHARACTERS 46

/* Usage Instructions */
int help() {
  printf("Usage: test.c [-s <arg0>]\n");
  printf("\t-s: a string program name <arg0>\n");
  printf("\t-s: a string sample name <arg1>\n");
  return (1);
}

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

  if ( argc < MIN_REQUIRED ) {
    printf ("Please follow the instructions: not less than %i argument inputs\n",MIN_REQUIRED);
    return help();
  }
  else if ( argc > MIN_REQUIRED ) {
    printf ("Please follow the instructions: not more than %i argument inputs\n",MIN_REQUIRED);
    return help();
  }
  else {
    int size, realsize;
    char *input = NULL;

    char *arguments[] = {"test-1","test-2","test-3"};

    int num = sizeof(arguments) / sizeof(arguments[0]);

    printf("This is the number of elements before: %i\n",num);

    int i;
    for (i=0; i<num; i++) {
      printf("This is the arguments before: [%i]: %s\n",i,arguments[i]);
    }

    printf("This is the input argument: %s\n",argv[1]);
    printf("This is the array element: %i\n",num+1);

    input = (char *)malloc(MAX_CHARACTERS);
    if (input == NULL) {
      printf("malloc_in failled\n");
      exit(0);
    }

    memset ( input , '\0' , MAX_CHARACTERS);

    int length_before = strlen(input);
    printf("This is the length before: %i\n",length_before);
    strcpy(input , argv[1]);
    int length_after = strlen(input);
    printf("This is the length after: %i\n",length_after);

    //arguments[num+1] = input;

    strcpy(arguments[num+1],input);

    int num_2 = sizeof(arguments) / sizeof(arguments[0]);

    printf("This is the number of elements after: %i\n",num);

    for (i=0; i<num_2; i++) {
      printf("This is the arguments after [%i]: %s\n",i,arguments[i]);
    }

  } // End of else condition

  return 0;
} // Enf of int main ()
#包括
#包括
#包括
/*设置为最小参数2*/
#定义所需的最小值2
#定义最大字符数46
/*使用说明*/
int help(){
printf(“用法:test.c[-s]\n”);
printf(“\t-s:字符串程序名\n”);
printf(“\t-s:字符串示例名称\n”);
申报表(1);
}
int main(int argc,char*argv[]){
如果(argcminu需要){
printf(“请按照说明操作:不超过%i个参数输入”,需要最小值);
返回帮助();
}
否则{
int大小,realsize;
char*input=NULL;
字符*参数[]={“test-1”、“test-2”、“test-3”};
int num=sizeof(参数)/sizeof(参数[0]);
printf(“这是:%i\n”之前的元素数,num);
int i;

对于(i=0;iC中没有动态数组,
参数具有静态大小,能够容纳3个元素

strcpy(arguments[num+1],input);
这只是一种未定义的行为,表达式
参数[num+1]
访问一个超出范围的数组(最后一个元素之后的两个元素);不会发生神奇的重新分配或其他事情

通常,您有三种选择:

  • 您有一个上限,即希望在数组中存储多少项,并声明数组具有该大小。请跟踪实际存储在数组中的数字,或添加一些指示结束的哨兵值(需要额外空间!)
  • 您有一个上限,如果要存储的项目数恰好超过此限制,您将中止(返回错误指示器,告诉用户输入数据太大,…)
  • 查找
    malloc
    realloc
    free
  • “我的目标是找到一种从动态数组中添加和删除字符串的方法:”

    char*arguments[]={…}
    是静态分配的,因此不能用作“动态数组”

  • strcpy(参数[num+1],输入)

    当此数组只有
    num
    项时,无法访问
    参数[num+1]


  • 建议修复-根据
    argc
    的值动态分配和初始化
    参数

    char* strings[] = {"test-1","test-2","test-3"};
    int i, num = sizeof(strings) / sizeof(*strings);
    char** arguments = malloc((num+argc-1)*sizeof(char*));
    if (arguments == NULL)
        ; // Exit with a failure
    
    for (i=0; i<num; i++)
    {
        arguments[i] = malloc(strlen(strings[i])+1);
        if (arguments[i] == NULL)
            ; // Deallocate what's already been allocated, and exit with a failure
        strcpy(arguments[i],strings[i]);
    }
    
    for (i=0; i<argc-1; i++)
    {
        arguments[num+i] = malloc(strlen(argv[i+1])+1);
        if (arguments[num+i] == NULL)
            ; // Deallocate what's already been allocated, and exit with a failure
        strcpy(arguments[num+i],argv[i+1]);
    }
    
    ...
    
    // Deallocate everything before ending the program
    
    char*strings[]={“test-1”、“test-2”、“test-3”};
    int i,num=sizeof(strings)/sizeof(*strings);
    char**arguments=malloc((num+argc-1)*sizeof(char*);
    if(参数==NULL)
    ;//失败退出
    
    对于(i=0;i您不能以这种方式使用数组。要么声明数组足够大(例如,
    char*arguments[MAX\u arguments]
    ,其中常量是某个正常的数字),要么在您知道有多少个参数的点处动态分配它。请注意,如果您要在失败后(或在执行某项操作后)实际退出,无需取消分配所有内容,因为退出将自动完成。如果您打算在出现错误的情况下继续操作,则需要取消分配内容。@chrisddol:在我看来,这似乎取决于操作系统,而不是标准定义的内容。谢谢,我想我已经了解了如何使用这个小型专业软件问题。我会试一试。好的,好的,我明白你的意思。我认为我不需要指定数组的大小。我的印象是,可以根据数组的元素增加或减少。在这种情况下,高级编程语言中的场景是如何指定数组的?谢谢你的时间和努力来帮助我。哟不客气:)数组总是有一个固定的大小,没有办法调整它的大小。更高级的语言实际上没有类似C数组的东西。例如,Java数组更像是指向
    malloc
    ed(新的
    等效)内存的C指针(后者更灵活,例如,你可以调整这样的内存大小)例如Python或Haskell中的列表根本不是数组(在O(1)随机访问的意义上),它们是链表(通过快速的web搜索,您可以在C中找到大量链表实现的示例)。您可以自己编写数据结构,如果需要,可以自动调整其大小,以获得与其他语言中的列表类似的行为。许多容器格式已经在一些库中实现,例如Glib。在C项目开始时,您通常会花一些时间规划要使用的内存策略。这是您应该做的d熟悉。也与您的问题有关: