繁体   English   中英

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

[英]How to add/remove a string in a dynamic array in c language

我有一个已定义的数组样本:

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

我试图添加命令行给出的参数输入。 我尝试了strcpy函数,并将其传递给数组元素,例如arguments[num+1] = argv[1]但同样没有成功。

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

我在网上找到的最接近的工作样本是C程序,用于在数组中插入元素C程序用于从数组中删除元素 但这并不是我正在寻找的东西,他们正在与intigers合作,而不是我需要的角色。

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

感谢大家的时间和精力来帮助我。

工作代码示例如下:

#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 ()
  1. “我的目​​标是找到一种从动态数组中添加和删除字符串的方法”:

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

  2. strcpy(arguments[num+1],input)

    当此数组只有num个条目时,您无法访问arguments[num+1]


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

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

C中没有动态数组, arguments有静态大小,能够容纳3个元素,

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

只是未定义的行为,表达式arguments[num+1]访问一个超出界限的数组(最后一个之后的两个元素); 没有神奇的重新分配或会发生什么。

通常,您有三种选择:

  1. 您有一个上限,表示您希望能够在数组中存储多少项,并声明该数组具有该大小。 要么跟踪实际存储在其中的数字,要么添加一些指示结束的标记值(需要额外的空间!)。
  2. 您有一个上限,如果您要存储的项目数量超过此限制,您将中止(返回错误指示符,告诉用户输入数据太大,...)。
  3. 寻找mallocreallocfree

暂无
暂无

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

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