Judging on your question and the observation that you are probably a beginner in C, I'll attempt to answer your question without using pointers like the other answers here.
First off, Jonathon Reinhart makes an excellent point, that sizeof
is not the proper usage here. Also, as others pointed out, the correct syntax for an array of characters, which is what you are using in your code, is as follows:
// empty character array
// cannot be changed or given a value
char text[];
// character array initialized to the length of its content
char text[] = "This is a string";
// character array with length 1000
// the elements may be individually manipulated using indices
char text[1000];
In your case, I would do something like this:
#include <string.h>
void print(char text[]);
int main()
{
char text[] = "This is a string";
print(text);
return 0
}
void print(char text[])
{
// ALWAYS define a type for your variables
int ch, len = strlen(text);
for(ch = 0; ch < len; ch++) {
do_something_with_character(text[ch]);
}
}
The standard library header string.h
provides the strlen
function which returns an integer value (actually an unsigned long) of the length of the string excluding the terminating NULL character \0
. In C, strings are just arrays of characters and the way you specify the end of a string is by including \0
at the end.