Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting string with C function

Tags:

c

function

string

I need to get strings dynamically but as I need to get more than one string, I need to use functions. So far I wrote this (I put //**** at places i think might 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 the function is working but not back in main function. I tried returning void, getting rid of *s or adding, making another str2 and tring to strcpy there or not using strcpy at all. Nothing seems to working. Am I misssing something? Or maybe this is not possible at all //Thank you so much for your answers

like image 614
R. Arda Avatar asked Dec 24 '16 18:12

R. Arda


People also ask

Can a function return a string in C?

Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid.

How do you find the string of a function?

Returns the null-terminated character string at the specified memory location as a CHARACTER value (not including the null terminator) or the number of bytes specified starting from the specified memory location as a CHARACTER value.

Is there string function in C?

There are predefined string functions in the C language, namely string handling functions. There is a header file defined for these functions named as string. h. While string handling it is important to use a header file of string.

How do you print out a string in C?

using printf() If we want to do a string output in C stored in memory and we want to output it as it is, then we can use the printf() function. This function, like scanf() uses the access specifier %s to output strings. The complete syntax for this method is: printf("%s", char *s);


2 Answers

Getting the string part can be taken from this answer. Only put a \n as input to the getline funtion.

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

Three things :- don't cast malloc, check if malloc/realloc is successful and sizeof is not a function.

like image 63
user2736738 Avatar answered Oct 03 '22 21:10

user2736738


The problem is not with the function that you are using, but with the way you try copying its result into an uninitialized pointer.

Good news is that you don't have to copy - your function already allocates a string in dynamic memory, so you can copy the pointer directly:

char *str = getstring(str);

This should fix the crash. A few points to consider to make your function better:

  • main needs to free(str) when it is done in order to avoid memory leak
  • Store realloc result in a temporary pointer, and do a NULL check to handle out-of-memory situations properly
like image 24
Sergey Kalinichenko Avatar answered Oct 03 '22 21:10

Sergey Kalinichenko