0

I want to get a string from the user, in a char array that have no fixed length. The length should be equal to the, length of the string that the user enters. I tried malloc(), but that also requires the size to be specified. Please help. Please mark it, I want to use a char array, not a string type.

  • 1
    You have to wait for it to be entered, and see how long it is. You can still treat whatever you use as an array, it's just that you can't specify the length like this: `[27]`. – Hot Licks Apr 03 '14 at 03:18
  • Possible duplicate of: http://stackoverflow.com/questions/8164000/how-to-dynamically-allocate-memory-space-for-a-string-and-get-that-string-from-u – Baldrick Apr 03 '14 at 03:23
  • can you please show me how to do that? – Rupam Ranjan Rana Apr 03 '14 at 03:28

5 Answers5

0

C strings do not pack their length with them. Every C string is a plain array of characters, with a null after the last char to indicate its end. Standard functions from the C IO library will generally receive, therefore, an array of chars and write data into it. The array will have to be big enough to hold everything that is typed by the user. Most functions won't even check for buffer overflows.

Now what you can do is ask first for the max length of the string the user is going to type and allocate exact memory, or you can declare a huge array and define its size as the max string length.

char bigBuffer[2048];
fgets(bigBuffer, 2048, stdin);

fgets() allows you to specify the maximum number of chars you are taking in. If the user types more than 2048 chars, in this example, fgets() it will return with an error and prevent your program from crashing.

glampert
  • 4,371
  • 2
  • 23
  • 49
0

It is not possible to allocate a memory with infinite length. Every memory is bound by size one way or other.

There are two ways to handle your situation.
1. Allocate a large memory which will not overrun any possible user input.
2. [Better Option] Use reasonable size memory and use function with length check like, fgets, to get user input.

Murli
  • 728
  • 5
  • 15
0

What you need is a basic unlimited input function. The idea is to allocate a reasonably sized buffer for input, begin reading one char at a time, and if you exceed the buffer size to realloc it and increase its size.

You could optimize this a bit by reading strings the length of the remaining space, but that gets complicated and fiddly. Mainly not worth it.

I wrote this code off the top of my head so it probably won't compile and work as-is, but it should give you the basic idea.

char *buffer = malloc(100);
size_t bufferLen = 100;    
size_t currLen = 0;
int c;
while ((c = getchar()) != EOF && c != '\n')
{
    if (currLen > bufferLen-1) // -1 because must leave room for null terminator
    {
        bufferLen += 100;
        buffer = realloc(buffer, bufferLen);
        buffer[currLen++] = c;
    }
    else
        buffer[currLen++] = c;
}
buffer[currLen] = '\0';
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
-1

This can be done in indirect way.

Read one character at a time from input. Using malloc/realloc allocate memory in increasing fashion. It is not of constant order time and constant order memory algo but your functionality can be achieved. Here is the code snippet for that.

char ch;
int count=0;
char *charArray=NULL;
printf("Enter string\n");
while((ch=getchar())!='\n')//This condition can be changed according to needs
{
    count=count+1;
    charArray=(char *)realloc(charArray,count);
    charArray[count-1]=ch;
}
Vinay
  • 188
  • 9
-2

You create the array after the user has entered the string. Can't remember exact C syntax but something along the lines of

string word = "";
scanf("%s", word");
char myArray[word.length];
Raza Jamil
  • 264
  • 1
  • 3
  • 12