Quote:
Originally Posted by kev717
Any help is appreciated.
|
The first thing to understand is that sizeof() is not a function that is executed at runtime; it is used by the compiler to deduce the size of the given data type, or the size of the data type for a given variable. For example, if you have a variable 'foo' that is declared to be an int, sizeof(foo) and sizeof(int) will return the same result (typically 4).
When using sizeof() on a pointer type/variable, the size of the pointer for your system is returned (4 for 32-bit arch, 8 for 64-bit arch), not then length of the data that is referenced by that pointer. For this reason, your application will fail, as I'm sure you already have discovered.
Consider the following for gathering input:
Code:
void Iput(char** iput)
{
int tmp = 0;
int len = 0; /* overall length of the string that is read */
if (iput == NULL)
{
return;
}
*iput = NULL; /* always start with a clean slate */
/* read from stdin until either a newline is read, or EOF is reached */
while ((tmp = getchar()) != '\n' && tmp != EOF)
{
/* allocate one additional byte for the input */
*iput = realloc(*iput, ++len);
/* store the inputted byte of data */
(*iput)[len - 1] = tmp;
}
/* finish by inserting a null-character at the end of the string */
*iput = realloc(*iput, ++len);
(*iput)[len - 1] = '\0';
}
For copying the string, as you have proposed in Copystring(), this is really unnecessary. However if you wish to pursue that function I can tell you that it will be very similar to the function shown above. The exception will be that you will be parsing the input until a null-character is found.
If you have additional questions, please feel free to ask.