我试图在C中编写一个程序,该程序从用户那里获取未知数量的字符串(每个都是未知大小)作为输入并存储它们,然后在用户输入完字符串后打印它们。
首先,我使用指向字符指针(char **字符串)的指针,并使用malloc为其分配1个char *大小的内存块。然后我将1个字符大小的块分配给字符串指向的指针((字符串)),同时使用malloc。从那里我使用一个名为get_String()的用户定义函数从用户输入一个字符串输入,并将它放入char * 字符串指向的char指针。然后我使用for循环继续为char **字符串分配额外的char *内存,并为新指针分配1个内存char。
但是,我在for循环的第二次迭代中遇到错误,在line strings =(char **)realloc(strings,index + 1);并且我收到错误消息:堆00558068处的块阻止在00558096处修改过去请求的大小为26.看起来我正在将已分配的内存写入char **字符串,但我不知道我在哪里或如何做此
这是我的整个代码:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void get_strings( char* strings); // function to take in a string of an unknown size
int main( void )
{
char** strings;
int index, count;
(strings) = (char**) malloc( sizeof(char*)); // this is the pointer that holds the addresses of all the strings
*strings = (char*) malloc( sizeof(char)); // the address of the first string
printf( "Enter a list of stringss. Quit by pressing enter without entering anything\n" );
get_strings( *strings );
for( index = 1; strlen(*(strings+index-1))!=1; index++) // stores strings from the user until they enter a blank line which is detected when string length is 1 for the \n
{
strings = (char**) realloc (strings, index+1); // adds an extra character pointer for another string
*(strings + index) = (char*) malloc(sizeof(char)); // allocates memory to the new character pointer
get_strings( *(strings + index) ); // stores the string from the user
}
printf( "You entered:\n" );
for( count = 0; strlen(*(strings + count)) != 1; count++ ) //prints every string entered by the user except for the terminating blank line
{
printf( "%s", *(strings + count ) );
free( *(strings + count ));
}
free( strings );
system( "PAUSE" );
return 0;
}
void get_strings( char* strings )
{
fgets( strings, 1, stdin );
while( strings[ strlen( strings ) - 1 ] != '\n' )
{
strings = (char*) realloc( strings, strlen(strings)+2 );
fgets( strings + strlen(strings), 2, stdin );
}
}
如前所述,在执行行时,for循环的第二次迭代发生堆块:strings =(char **)realloc(strings,index + 1);
for( index = 1; strlen(*(strings+index-1))!=1; index++) // stores strings from the user until they enter a blank line which is detected when string length is 1 for the \n
{
strings = (char**) realloc (strings, index+1); // error occurs here
*(strings + index) = (char*) malloc(sizeof(char)); // allocates memory to the new character pointer
如果有人能向我解释这个错误的原因以及解决问题的方向,我将非常感激。谢谢。
答案 0 :(得分:0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *get_strings(void);
int main( void ){
char **strings = NULL;
char *string;
int index, count;
printf( "Enter a list of stringss. Quit by pressing enter without entering anything\n" );
for(index = 0; string = get_strings(); ++index){
strings = (char**) realloc (strings, (index+1)*sizeof(*strings));
strings[index] = string;
}
printf( "You entered:\n" );
for( count = 0; count < index; ++count ){
printf("%s\n", strings[count]);
free( strings[count] );
}
free( strings );
system( "PAUSE" );
return 0;
}
char *get_strings(void){
char *string = NULL;//or calloc(1,sizeof(char)) for return "" (test by caller: *string == '\0')
int ch;
size_t len = 0;
while(EOF != (ch=fgetc(stdin)) && ch != '\n' ) {
string = (char*)realloc( string, len + 2);//To realloc to each character is inefficient
string[len++] = ch;
}
if(string)
string[len] = '\0';
return string;//The value is NULL when there is no input substantial.
}