Getting a string using C function

I need to get rows dynamically, but since I need to get more than one row, I need to use functions. So far I have written this (I put // **** in places that may be wrong)

char* getstring(char *str); int main() { char *str; strcpy(str,getstring(str));//***** printf("\nString: %s", str); return 0; } char* getstring(char str[]){//***** //this part is copy paste from my teacher lol char c; int i = 0, j = 1; str = (char*) malloc (sizeof(char)); printf("Input String:\n "); while (c != '\n') {//as long as c is not "enter" copy to str c = getc(stdin); str = (char*)realloc(str, j * sizeof(char)); str[i] = c; i++; j++; } str[i] = '\0';//null at the end printf("\nString: %s", str); return str;//****** } 

printf in a function works, but does not return to main . I tried to return void , get rid of *s or add by doing another str2 and tring before strcpy there or not using strcpy at all. Nothing is working. Did I miss something? Or perhaps this is not possible at all // Thank you so much for your answers.

+5
source share
4 answers

Getting part of the string can be taken from this. Insert only \n as the value for the getline function.

 char * p = getline('\n'); 

Three things: - do not drop malloc, check if malloc/realloc and sizeof succeed is not a function.

+3
source

The problem is not the function that you are using, but the way you are trying to copy its result into an uninitialized pointer.

The good news is that you don’t need to copy - your function already allocates a line in dynamic memory, so you can copy the pointer directly:

 char *str = getstring(str); 

This should cause a crash. A few points to consider to improve your function:

  • main requires free(str) when done to avoid memory leak
  • Save realloc will lead to a temporary pointer and do a NULL check to handle memory situations normally.
+3
source

There are two things that need to be removed from the lesson when it is standing now:

(1) You should have one way to return a link to a new line, either as an argument passed by a reference to an OR function as a return value; you should not implement both.

(2) Since the routine that your teacher gave you allocates memory in a heap, it will be available for any part of your program, and you do not need to allocate any memory yourself. You should study the difference between heap memory, global memory and automatic memory (stack) so that you understand the differences between them and know how to work with each type.

(3) Since the memory is already allocated on the heap, there is no need to copy the line.

Given these facts, your code can be simplified something like this:

 int main() { char *str = getstring(); printf( "\nString: %s", str ); return 0; } char* getstring(){ .... etc 

Going forward, you want to think about how you allocate memory in your programs. For example, in this code a line is never highlighted. It’s a good habit to think about your strategy to allocate your allocated memory.

+3
source

Simplify the code a bit:

 #include <stdio.h> #include <stdlib.h> #include <string.h> char* getstring() { char c = 0; int i = 0, j = 2; char *str = NULL; if ((str = (char*) malloc(sizeof(char))) == NULL) return NULL; printf("Input String: "); while (c = getc(stdin)) { if (c == '\n') break; str = (char*) realloc(str, j * sizeof(char)); str[i++] = c; j++; } str[i] = '\0'; printf("getstring() String: %s\n", str); return str; } int main() { char *str = getstring(); printf("main() String: %s\n", str); free(str); return 0; } 

Then do:

 $ make teststring && ./teststring cc teststring.c -o teststring Input String: asdfasfasdf getstring() String: asdfasfasdf main() String: asdfasfasdf 
+1
source

Source: https://habr.com/ru/post/1261848/


All Articles