[英]Creation of Dynamic Array of Strings in C
因此,我非常想創建一個功能如下的數組:1)我輸入一個字符串(例如在 char temp[...] 等臨時數組中)2)我創建一個當第一個字符時結束的 while 循環temp 是 \x0 3)在循環內我使用 malloc function 為每個字符串創建空間 4)我打印字符串
問題是,每當我嘗試運行我的程序時,它都會崩潰,並且編譯器對我的幫助不大。
這是代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE 80
int main(){
char **pin;
char temp[LINE];
int index = 0;
puts("Gimme Books' Titles:");
gets(temp);
while(temp[0] != '\x0'){
pin[index] = (char*)malloc(strlen(temp)+1);
strcpy(pin[index], temp);
index++;
gets(temp);
}
puts("\nBooks' List:");
for(int k = 0; k < index; k++){
puts(pin[k]);
}
}
這里有人可以幫助我找出我做錯了什么嗎?
上面評論中的鏈接 [大部分] 假設我們在分配數組之前知道元素的數量。
因此,當我們只能通過讀取輸入文件來獲取元素的數量時,它就沒有那么有用了。 對於來自stdin
輸入的 TTY 輸入,這不會很好,因為我們不能倒回文件。
動態數組通常很有用。 所以,我創建了一個允許任意大小的數組元素。 而且,它可以隨着我們添加新元素而增長/擴展。
而且,為了演示的目的,我創建了一個“書”結構來存儲作者的姓名和書名。
這是代碼。 注釋如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
// general dynamic array control
typedef struct {
void *arr_base; // pointer to data
size_t arr_size; // bytes / element
size_t arr_count; // number of elements in use
size_t arr_capacity; // number of elements allocated
} arr_t;
// book control
typedef struct {
char *book_last; // author's last name
char *book_first; // author's first name
char *book_title; // book title
} book_t;
// arrnew -- allocate new array
arr_t *
arrnew(size_t siz)
{
arr_t *arr = calloc(1,sizeof(*arr));
// store the number of bytes in each element
arr->arr_size = siz;
return arr;
}
// arrappend -- get new element in array
// RETURNS: pointer to element
void *
arrappend(arr_t *arr)
{
size_t index;
void *ptr;
// get index of element to store into and increase the in use count
index = arr->arr_count++;
do {
// we have enough space for the new element already
if (arr->arr_count < arr->arr_capacity)
break;
// increase allocated amount
// NOTE: the increment is arbitrary
arr->arr_capacity += 10;
// grow the array
arr->arr_base = realloc(arr->arr_base,
arr->arr_capacity * arr->arr_size);
// got the larger array
if (arr->arr_base != NULL)
break;
perror("arrappend");
exit(1);
} while (0);
// point to element we can store into
ptr = arr->arr_base;
ptr += index * arr->arr_size;
return ptr;
}
// arrtrim -- trim array to number of elements actually used
void
arrtrim(arr_t *arr)
{
arr->arr_capacity = arr->arr_count;
arr->arr_base = realloc(arr->arr_base,arr->arr_capacity * arr->arr_size);
if (arr->arr_base == NULL) {
perror("arrtrim");
exit(1);
}
}
// arrfree -- free up array storage
void
arrfree(arr_t *arr)
{
free(arr->arr_base);
free(arr);
}
int
main(void)
{
char buf[300];
char *cp;
book_t *book;
// echo line if input is _not_ a tty [mostly for demo]
struct termios tio;
int echo = tcgetattr(fileno(stdin),&tio) < 0;
// get new array
arr_t *arr = arrnew(sizeof(book_t));
printf("Gimme Books's Titles:\n");
while (1) {
// get next line -- stop on EOF
char *cp = fgets(buf,sizeof(buf),stdin);
if (cp == NULL)
break;
// echo line if input is _not_ a tty
if (echo)
fputs(buf,stdout);
// strip newline
buf[strcspn(buf,"\n")] = 0;
// stop on blank line
if (buf[0] == 0)
break;
// get new book struct
book = arrappend(arr);
// get author's last name
char *tok = strtok(buf," \t");
book->book_last = strdup(tok);
// get author's first name
tok = strtok(NULL," \t");
book->book_first = strdup(tok);
// get the book title
tok += strlen(tok);
++tok;
for (; *tok != 0; ++tok) {
if (*tok != ' ')
break;
}
book->book_title = strdup(tok);
}
arrtrim(arr);
// show all the books
book = arr->arr_base;
for (size_t idx = 0; idx < arr->arr_count; ++idx, ++book)
printf("Last: %s First: %s Title: %s\n",
book->book_last,book->book_first,book->book_title);
// release storage for each book
book = arr->arr_base;
for (size_t idx = 0; idx < arr->arr_count; ++idx, ++book) {
free(book->book_last);
free(book->book_first);
free(book->book_title);
}
// release array storage
arrfree(arr);
return 0;
}
這是程序輸入:
Lambstewer Abel A Tale of Two Cities
Smith John A baker's tale
Jones Fred Never On Sunday
這是程序 output:
Gimme Books's Titles:
Lambstewer Abel A Tale of Two Cities
Smith John A baker's tale
Jones Fred Never On Sunday
Last: Lambstewer First: Abel Title: A Tale of Two Cities
Last: Smith First: John Title: A baker's tale
Last: Jones First: Fred Title: Never On Sunday
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.