gpt4 book ai didi

c - 分配动态二维数组

转载 作者:行者123 更新时间:2023-11-30 19:19:59 24 4
gpt4 key购买 nike

动态创建数组时一切似乎都工作正常但在尝试向后打印时核心被转储。它设法只打印最后一个字符串,然后打印段错误。

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

void init_array(void ***pt, int *ptlen) {
*pt=NULL;
*ptlen=0;
}

void trim_array(void ***pt, int *ptlen, int len) {
*pt=(void**)realloc(*pt, len*sizeof(void*));
*ptlen=len;
}

void write_array(void ***pt, int *ptlen, int pos, void *v) {
if (pos >= *ptlen)
trim_array(pt, ptlen, pos+1);

*pt[pos]=v;
}

void *read_array(void ***pt, int *ptlen, int pos) {
return(*pt[pos]);
}

void destroy_array(void ***pt, int *ptlen) {
trim_array(pt, ptlen, 0);
*pt=NULL;
}

int main(int argc, char *argv[]) {
void **t;
int tlen;

void ***pt = &t;
int *ptlen = &tlen;

char s[256],*p; int i;

init_array(pt, ptlen);

i = 0;
do {
printf("give name:\n");
scanf("%255s",s);
write_array(pt, ptlen, i, (void*)strdup(s));
i++;
} while (strcmp(s,"end"));

for (--i; i>=0; i--) {
p = (char*)read_array(pt, ptlen, i);
printf("%s\n",p);
free(p);
}

destroy_array(pt, ptlen);
return(0);
}

最佳答案

[] 运算符的优先级高于 * 运算符。您需要更改:

*pt[pos]

至:

(*pt)[pos]

在它发生的两个地方。

此错误是编写几乎故意将代码与失控间接混淆所导致的直接结果。如果您将大量此类内容包装在 struct 中并为其创建一些适当的接口(interface)函数,您将为自己省去很多麻烦,并使事情变得更加容易。

这样的形式会更好一些(尽管“数组”对于这种数据结构来说并不是一个很好的名字):

main.c:

#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "array.h"

#define MAX_BUFFER_LEN 255

int main(void) {
Array myarray = array_init(10, true);

/* Loop for input until user enters "end" */

char buffer[MAX_BUFFER_LEN];
while ( true ) {
printf("Give name: ");
fflush(stdout);

/* Get input and remove trailing '\n' if necessary */

fgets(buffer, MAX_BUFFER_LEN, stdin);
size_t last = strlen(buffer) - 1;
if ( buffer[last] == '\n' ) {
buffer[last] = '\0';
}

/* Terminate loop on "end" without adding to array... */

if ( !strcmp(buffer, "end") ) {
break;
}

/* ...or append input to array and continue loop */

array_append(myarray, strdup(buffer));
};

/* Output contents of array */

size_t n = array_size(myarray);
for ( size_t i = 0; i < n; ++i ) {
char * data = array_getdata(myarray, i);
printf("%zu: %s\n", i + 1, data);
}

/* Clean up and exit */

array_destroy(myarray);

return EXIT_SUCCESS;
}

array.h:

#ifndef ARRAY_TYPE_H
#define ARRAY_TYPE_H

#include <stdbool.h>

typedef struct array_type * Array; /* Opaque type for user */

Array array_init(const size_t capacity, const bool free_on_delete);
void array_append(Array array, void * data);
size_t array_size(const Array array);
void * array_getdata(Array array, const size_t index);
void array_deletetop(Array array);
void array_destroy(Array array);

#endif /* ARRAY_TYPE_H */

array.c:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "array.h"

/* Struct definition is visible only to implementation */

struct array_type {
void ** elements;
size_t capacity;
size_t top;
bool free_on_delete;
};

/* Static functions used by the implementation */

static bool array_isfull(Array array) {
return (array->top + 1) == array->capacity;
}

static void array_resize(Array array, const size_t new_capacity) {
array->capacity = new_capacity;
array->elements = realloc(array->elements,
array->capacity * sizeof (*array->elements));
if ( array->elements == NULL ) {
fputs("Error allocating memory.", stderr);
exit(EXIT_FAILURE);
}
}

/* Interface functions */

Array array_init(const size_t capacity, const bool free_on_delete) {
struct array_type * new_array = malloc(sizeof *new_array);
if ( new_array == NULL ) {
fputs("Error allocating memory.", stderr);
exit(EXIT_FAILURE);
}

new_array->elements = malloc(capacity * sizeof (*new_array->elements));
if ( new_array->elements == NULL ) {
fputs("Error allocating memory.", stderr);
exit(EXIT_FAILURE);
}

new_array->capacity = capacity;
new_array->top = 0;
new_array->free_on_delete = free_on_delete;

return new_array;
}

void array_append(Array array, void * data) {
if ( array_isfull(array) ) {
array_resize(array, array->capacity * 2);
}
array->elements[array->top++] = data;
}

size_t array_size(const Array array) {
return array->top;
}

void * array_getdata(Array array, const size_t index) {
return array->elements[index];
}

void array_deletetop(Array array) {
if ( array->free_on_delete ) {
free(array->elements[array->top - 1]);
}
array->elements[--array->top] = NULL;
}

void array_destroy(Array array) {
while ( array->top > 0 ) {
array_deletetop(array);
}
free(array->elements);
free(array);
}

示例输出:

paul@local:~/src/c/scratch/array$ ./array
Give name: Dave Dee
Give name: Dozy
Give name: Beaky
Give name: Mick
Give name: Titch
Give name: end
1: Dave Dee
2: Dozy
3: Beaky
4: Mick
5: Titch
paul@local:~/src/c/scratch/array$

关于c - 分配动态二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com