从zabbix里面摘出来的c语言数组,很经典
/*
* strarray.cpp
*
* Created on: 2017年1月18日
* Author: yangling
*/
#include "../../include/types.h"
#include "../../include/defs.h"
#include "../../include/common.h"
/******************************************************************************
* *
* Function: str_strarr_init *
* *
* Purpose: initialize dynamic string array *
* *
* Parameters: arr - a pointer to array of strings *
* *
* Return value: *
* *
* Author: Vladimir Levijev *
* *
* Comments: allocates memory, calls assert() if that fails *
* *
******************************************************************************/
void str_strarr_init(char ***arr)
{
*arr = (char **)ext_malloc(*arr, sizeof(char *));
**arr = NULL;
}
/******************************************************************************
* *
* Function: str_strarr_add *
* *
* Purpose: add a string to dynamic string array *
* *
* Parameters: arr - a pointer to array of strings *
* entry - string to add *
* *
* Return value: *
* *
* Author: Vladimir Levijev *
* *
* Comments: allocates memory, calls assert() if that fails *
* *
******************************************************************************/
void str_strarr_add(char ***arr, const char *entry)
{
int i;
assert(entry);
for (i = 0; NULL != (*arr)[i]; i++)
;
*arr = (char **)ext_realloc(*arr, sizeof(char *) * (i + 2));
(*arr)[i] = ext_strdup((*arr)[i], entry);
(*arr)[++i] = NULL;
}
/******************************************************************************
* *
* Function: str_strarr_free *
* *
* Purpose: free dynamic string array memory *
* *
* Parameters: arr - array of strings *
* *
* Return value: *
* *
* Author: Vladimir Levijev *
* *
******************************************************************************/
void str_strarr_free(char **arr)
{
char **p;
for (p = arr; NULL != *p; p++) {
ext_free(*p);
}
ext_free(arr);
}