Question on malloc() and reading strings
Hi,
I've just switched from java programming to c programming and am currently facing some problems (probably as I'm unfamilar with pointers and c syntax).
Let's same my program has to read in an unknown number of strings (with 40 chars max) until it reads the string "halt". I have to use the malloc function to ensure that the correct amount of memory is allocated to the input string, and then print out the size of this new string (as proof).
Why is it that the size of the new string is fixed at the size of the very first string processed? Guess that I'm doing something seriously wrong here.
Code:
int main() {
char[41] currString;
char *newString;
scanf("%s",currString);
while (strcmp(currString,"stop")!=0) {
newString=(char *) malloc (strlen(currString)+1);
printf("%lu\n",sizeof(newString));
scanf("%s",currString);
return 0;
}
[/code]
Hope to get some advice. Thanks.