drM said:
Could anyone please offer some advice:
Trying to play with character arrays in C, the code below generates
this error.
Anyone see something wrong?
thank you in advance.
#include <stdio.h>
void test(char s[]);
When you pass an array as an argument to a function, what actually gets
passed is a pointer to the first element of the array; the above
prototype is a synonym for
void test(char *s);
Because of how array subscripting is defined, you can still access
individual elements of s as s[0], s[1], etc., but the *type* of s is
"pointer to char", not "array of char". This has nothing to do with
your specific error, but it's something that I guarantee is going to
trip you up soon.
Remember that when you declare an N-element array, the array subscripts
run from 0 to N-1; in this case, you've declared helloworld to hold 11
characters, which means the you can access elements from 0 to 10.
However, in your test function below, you try to write to elements 0 to
11. Writing to that last element writes beyond the end of the array,
which leads to undefined behavior. Change this declaration so that
helloworld contains 12 elements.
There's no good reason to declare helloworld at file scope (i.e., as a
global variable). As a general rule, you want to limit the scope of
variables as much as possible to avoid maintenance headaches. Since
you're passing it as an argument to test, there's no reason to have it
globally visible to both main() and test(). Move the declaration so
that it's within main.
int main () {
printf("%s", test(helloworld)); // <----Invalid use of void
Okay, here's the actual problem. The "%s" conversion specifier expects
an argument of type "char *"; however, you've defined test() to return
void (I'm pretty sure that printf() doesn't like *any* of its arguments
to be void, which is why you get a compile-time diagnostic; how do you
format an expression that has no value?). There are two ways around
this:
1. Change the return type of test to char *, and return the value of
the input parameter (it's a good idea for the parameter names in the
function declaration match those in the function definition, btw; makes
life easier for other people reading the code):
char *test(char *t)
{
/* copy "Hello World" to t */
return t;
}
2. Leave the return type as it is, call test() before calling
printf(), and pass the modified helloworld as the argument to printf(),
like so:
test(helloworld);
printf("%s\n", helloworld);
Note: because of how C I/O works, if you don't have printf() emit a
newline, the text may not be flushed to output immediately, and it will
look like your program isn't doing anything. So you either need a
newline in the control string, as I've done above, or if you don't want
a newline at that point, you should add the line
fflush(stdout);
after the printf() statement.
expression
return 0;
}
void test (char t[])
{
t[0]='H';
t[1]='e';
t[2]='l';
t[3]='l';
t[4]='o';
t[5]=' ';
t[6]='W';
t[7]='o';
t[8]='r';
t[9]='l';
t[10]='d';
t[11]='\0';
There's a library function that allows you to do this with much less
pain:
strcpy(t, "Hello, World");
(be sure to #include <string.h>).