how to create expander dynamically?

How to create a char array dynamically according to the length of user input string?

  • The user should not have to enter the length of the string. e.g if user enters "abcd" the program should create a char array of 5 bytes to store it (1 byte for NULL). similarly if user enters "123456789" the program should create a char array of 10 bytes.

  • Answer:

    char *RS() { char x[100], *p; scanf("%s", x); //else you may make x as argument p=(char *) malloc(strlen(x)+1); strcpy(p,x); return p; } replace the following in case of C++ p=new char[strlen(x)+1]; Do make sure you include string.h Now we get the address of the string for which memory is allocated dynamically and occupies number of elements +1 bytes

Ali at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.