When you declare:
char ChAr[] = "A very long char!!!!";
the default size of ChAr[]
array is the size of the string you used in the definition/initialization of the array.
After that, in any expression ChAr[]
is not a valid, and you have to give some index value within []
; this the reason you are getting an error like:
"error: expected expression before ']' token"
It means, in call of function:
printChar(ChAr[]);
^ before ] you are missing some index value
Additionally, even if you call it like printChar(ChAr[i]);
it won't compile (not correct) and will give a type mismatch error. According to your function declaration below:
void printChar(char ChArr[]){
^ you need an char*
you should call this function as:
printChar(ChAr);
Because type of ChAr
is char[N]
which is what the function printChar
accepts as an argument.
Next error in function printChar()
is that evaluating length of string using sizeof
operator is wrong; use strlen()
instead of sizeof
operator. That means:
int iCharLen = sizeof(ChArr); // define ChArr[] length
should be:
int iCharLen = strlen(ChArr); // define ChArr[] length
Don't forget to #include <string.h>
.
Remember in function declaration char ChArr[]
is same as char* ChArr
.
The sizeof()
operator returns size of array only if an array name is given, but in your function you are passing the address of char*
that doesn't return length of your string; instead, it gives sizeof
pointer variable ChAr
(usually 4 bytes or 8 bytes).
To understand how sizeof()
operator works in both case read: What does sizeof(&arr) return?