> the problem is your use of the sizeof operator. This doesn't work on C-arrays like you might expect.
This isn't exactly true - it depends on the context of the 'array' in question as to whether it 'works' or not.
In the first case, the array is a proper array and sizeof will give you the answer you expect. sizeof will tell you the size of an array.
In the second case, the cString isn't an array (despite the use of [] in the declaration), it is simply a pointer (all arrays are passed as pointers to the first element). In this case, sizeof will only tell you the size of that pointer (which is 4 in most of common environments).
> ..or if you want to continue to use c-strings you could pass the size of the array into the function as a parameter.
Code:
#include <stdio.h>
#include <string.h>
void ReadString(char string[], int size);
int main()
{
printf("Please type a name:");
char cName[15] = "\0";
ReadString(cName,sizeof(cName));
printf("%s", cName);
return 0;
}
void ReadString(char cString[], int size)
{
int nCurrChar = 0;
for(int i = 0; (i<size) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
cString[i] = (char) nCurrChar;
cString[i] = '\0';
}