1

I'm trying to create a function in C to print the content of the char variable.

Here is my code for the function itself:

void printChar(char ChArr[])
{
    int iCharLen = sizeof(ChArr); // define ChArr[] length
    int i;
    for(i = 0; i<iCharLen; i++)
    {
        printf("%c",ChArr[i]);
    }     
}

Here is the code for "int main()":

int main()
{  
  char ChAr[] = "A very long char!!!!";
  printChar(ChAr[]);  
  return 0;
}

And the error I got is: "error: expected expression before ']' token"

The error is for line where I call the printChar().

Note: This code is for C only.

  • This code is broken on so many levels, you should probably pick up a text book on C or at least take some tutorials first. To fix the compilation error you need to remove the [] after ChAr, but the code won't do what you want anyway (hint sizeof does not work that way). – Bishop Jul 14 '13 at 10:19

4 Answers4

3

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?

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
2

Remove the [] when passing the character array to the function. Hence your function call will look like this:

printChar(ChAr);

This will solve the compiler error.

In addition, use strlen(ChArr) in the function definition instead of sizeof(ChArr).

Only then will all the characters in the array be displayed.

Nithin Bhaskar
  • 686
  • 1
  • 5
  • 9
1

Drop the [] when calling the function:

printChar(ChAr);

Also, sizeof in the function will return the size of a pointer to char, not the number of chars in the array. This is probably not what you expected.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

you can also do a my_putchar func, here it is:

void my_putchar(char c)
{
write(1, &c, 1);
}

int main()
{
my_putchar('a'); // for example
}

and with that you can also more fancy stuff like my_putstr or my_puts func

void my_putstr(char *str)
{
int i;

i = 0;
while(str[i] != '\0')
{
my_putchar(str[i]);
i++;
}
}

or more short

void my_putstr(char *str)
{
write(1, str, strlen(str));
}

it can do:

int main()
{
my_putstr("abcdef");
}

hope this help you :)

Saxtheowl
  • 4,136
  • 5
  • 23
  • 32