Reputation: 13941
I'm trying to write a function for printing text in C for PIC microcontrollers (I think it's based on gcc).
void print(char[] text);
void print(char[] text)
{
for(ch = 0; ch < sizeof(text); ch ++)
{
do_something_with_character(text[ch]);
}
}
and call it like this:
print("some text");
I'm getting compiler complaints about wrong brackets.
What is wrong with this?
How can I use char array pointer in this case?
Upvotes: 3
Views: 48483
Reputation: 5673
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.
Upvotes: 1
Reputation: 137527
As mentioned in other answers, your syntax is incorrect. The brackets belong after text
.
Also of significant importance, sizeof
does not do what you're expecting here:
void print(char[] text)
{
for(ch = 0; ch < sizeof(text); ch ++)
^^^^^^
Remember, sizeof
is a compile-time operator - the compiler replaces it with the size while the code is being built. It can't possibly know the size at runtime.
In this case, sizeof(text)
is always going to return sizeof(void*)
, or 4 on most 32-bit systems. Your platform may vary.
You have two options:
char[]
as a "C string", where the length is unknown, but the string is terminated by NUL character.The latter is your best bet, and you should probably replace char[]
with char*
.
Here we use a NUL-terminated string, and iterate over it with a pointer:
void print(const char* text)
{
const char* p;
// Iterate over each character of `text`,
// until we reach the NUL terminator
for (p = text; *p != '\0'; p++) {
// Pass each character to some function
do_something_with_character(*p);
}
}
Upvotes: 6
Reputation: 56539
Pass C-style string by pointers, or brackets after variable name:
void print(char *text);
^
or
void print(char text[]);
^^
To calculate the length of a string, use strlen
not sizeof
.
int len = strlen(text);
for(ch = 0; ch < len; ch ++)
^^^^
Upvotes: 1
Reputation: 6013
I would do it like this:
void print(char *text);
void print(char *text)
{
while(*text)
{
do_something_with_character(*text);
++text;
}
}
Upvotes: 6
Reputation: 15121
The correct syntax is
void print(char text[])
or
void print(char *text)
In print()
, you cannot use sizeof
operator to find out the length of string text
, you need to use strlen()
(include <string.h>
first) or test whether text[ch]
is \0
.
Upvotes: 9
Reputation: 754920
You have to place the square brackets in the correct place:
void print(char[] text);
void print(char[] text)
void print(char text[]);
void print(char text[])
or use pointer notation:
void print(char *text);
void print(char *text)
Also, note that even if you use the array notation, the parameter is effectively rewritten to a pointer, and then the code in the body of the function:
for(ch = 0; ch < sizeof(text); ch ++)
is wrong. You almost certainly don't want the 4 or 8 bytes that is the size of the pointer. You probably want:
size_t len = strlen(text);
for (size_t ch = 0; ch < len; ch++)
If you can't declare variables in your loop (C99 or later required), declare it separately. You didn't show the declaration of ch
in your code. If it compiled, that means ch
is a global variable — which is pretty horrid.
Note that the ++
operator binds tightly and should not be separated from its operand by spaces.
Upvotes: 2