//write a function that reads from standard input a string, the string is copied to the dynamic allocation of
//memory, and return copies of the string, the function should not be read in the length of the string to make any limit
#include
#include Char * readstring (void); Int main (void) { Char * STR=readstring (); If (STR==NULL) Printf (" error "\ n"); The else Printf (" % s \ n ", STR); return 0; } Char * readstring (void) { Char * array, * p; Char * PTR. Int length=0; Array=(char *) malloc (sizeof (char)); Gets (PTR); If (PTR==NULL | | array==NULL) return NULL; While (* + length (PTR)!='\ 0') length++; Array=(char *) realloc (array, (length + 1) * sizeof (char)); If (array==NULL) return NULL; for(int i=0; i Array [I]=* (PTR + I); Array [length]='\ 0'; P=array; Free (array); return p; } CodePudding user response:
PTR unallocated space Change the char * PTR to char PTR [256]. Then put the PTR==NULL * PTR==NULL instead CodePudding user response:
array=(char *) malloc (sizeof (char)); Gets (PTR); To apply for a byte, enter a character will cause memory ~ CodePudding user response:
reference 2 building self-confidence boy reply: array=(char *) malloc (sizeof (char)); Gets (PTR); To apply for a byte, enter a character will lead to memory ~ Why is it that you can run the following method Char * readstring (void) { Char * array; Char * PTR. Int length=1; Array=(char *) malloc (sizeof (char)); Gets (PTR); If (PTR==NULL | | array==NULL) { return NULL; } While (* PTR!='\ 0') { length++; Array=(char *) realloc (array, length * sizeof (char)); If (array==NULL) return NULL; Array [length - 2]=* PTR. ptr++; } Array [] length - 1='\ 0'; Return array; } CodePudding user response:
reference 1st floor qq_41515678 response: PTR unallocated space Change the char * PTR to char PTR [256]. And then change the PTR==NULL to * PTR==NULL Thought is time out, realloc, and thank you CodePudding user response:
# include #include Char * readstring (void); Int main (void) { Char * STR=readstring (); If (STR==NULL) Printf (" error "\ n"); The else Printf (" % s \ n ", STR); if (! STR) Free (STR); return 0; } Char * readstring (void) { Char * array, * p; Char * PTR. Int length=0; PTR=(char *) malloc (sizeof (char) * 32); if (! PTR) return NULL; The fgets (PTR, 32, stdin); Array=(char *) malloc (sizeof (char)); //if (PTR==NULL | | array==NULL) If (array==NULL) return NULL; While (* + length (PTR)!='\ 0') length++; Array=(char *) realloc (array, (length + 1) * sizeof (char)); If (array==NULL) return NULL; for(int i=0; i Array [I]=* (PTR + I); Array [length]='\ 0'; P=array; //free (array); return p; } For your reference ~ Memory also useless over don't try so hard to release, the release of the concept is no part of this space, but will be output in the main function, that is still in use, CodePudding user response:
reference 5 building self-confidence boy reply: # include #include Char * readstring (void); Int main (void) { Char * STR=readstring (); If (STR==NULL) Printf (" error "\ n"); The else Printf (" % s \ n ", STR); if (! STR) Free (STR); return 0; } Char * readstring (void) { Char * array, * p; Char * PTR. Int length=0; PTR=(char *) malloc (sizeof (char) * 32); if (! PTR) return NULL; The fgets (PTR, 32, stdin); Array=(char *) malloc (sizeof (char)); //if (PTR==NULL | | array==NULL) If (array==NULL) return NULL; While (* + length (PTR)!='\ 0') length++; Array=(char *) realloc (array, (length + 1) * sizeof (char)); If (array==NULL) return NULL; for(int i=0; i Array [I]=* (PTR + I); Array [length]='\ 0'; P=array; //free (array); return p; } For your reference ~ Memory also useless over don't try so hard to release, the release of the concept is no part of this space, but will be output in the main function, that is still in use,