473,807 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to understand character arrays.

drM
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[]);
char helloworld[11];

int main () {
printf("%s", test(helloworld )); // <----Invalid use of void
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';

}
>>>


Apr 3 '06 #1
20 1466
drM schrieb:
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[]);
char helloworld[11];

int main () {
printf("%s", test(helloworld )); // <----Invalid use of void
expression
1) This nicely illustrates why you should not use // comments in
usenet messages
2) test()'s return type is void -- "nothing".
Nothing cannot be assigned to "anything" (anything non-void).
You can either change test() to
char *test (char *s);
or you have to do it in two steps:
test(helloworld );
printf("%s", helloworld);
3) Note that you should output '\n' as last character in order
to make sure that you see any output at all. 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';

1) If you want to change test() to char *test(char*), then
insert
return &t[0];
here
2) There are also strcpy() and strncpy()...
}


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 3 '06 #2
drM wrote:

Could anyone please offer some advice:
Trying to play with character arrays in C, the code below generates
this error.
Anyone see something wrong? [...] void test(char s[]);
char helloworld[11]; [...] printf("%s", test(helloworld )); // <----Invalid use of void expression

[...]

Hint 1: What does the error say?
Hint 2: What type does test() return, and how does that relate to (1)?
Hint 3: Can the type answered in (2) be passed to a function?
Hint 4: What does printf's "%s" expect?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Apr 3 '06 #3
drM
Have just started in C, so your reply is most welcome.

"1) This nicely illustrates why you should not use // comments in
usenet messages "....Point taken.

Apr 3 '06 #4
drM opined:
Have just started in C, so your reply is most welcome.
Whose reply? To what?

"1) This nicely illustrates why you should not use // comments in
usenet messages "....Point taken.


You should also make a point of quoting what and who you're replying
to. Read the link in my sig, and all the links in it...

--
Parallel lines never meet, unless you bend one or both of them.

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

Apr 3 '06 #5
drM
2) test()'s return type is void -- "nothing".
Nothing cannot be assigned to "anything" (anything non-void).
You can either change test() to
char *test (char *s);
or you have to do it in two steps:
test(helloworld );
printf("%s", helloworld);
Michael....If I understand you correctly, the type (void) is producing
an error, not because of the (void) test(helloworld ) function, but
because of the printf function...or am I completely misunderstandin g
you.
tks

Apr 3 '06 #6
"drM" <md**@comcast.n et> writes:
Have just started in C, so your reply is most welcome.

"1) This nicely illustrates why you should not use // comments in
usenet messages "....Point taken.


Please learn to quote properly. <http://cfaj.freeshell. org/google/>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 3 '06 #7
drM

Keith Thompson wrote:

Please learn to quote properly. <http://cfaj.freeshell. org/google/>

Boy...this is a tough crowd!! :-) Thanks for all the input.

Apr 3 '06 #8
drM
Michael Mair wrote:
Nothing cannot be assigned to "anything" (anything non-void).
You can either change test() to
char *test (char *s);
or you have to do it in two steps:
test(helloworld );
printf("%s", helloworld);


Michael....If I understand you correctly, the type (void) is producing
an error, not because of the (void) test(helloworld ) function, but
because of the printf function...or am I completely misunderstandin g
you.
tks

Apr 3 '06 #9
"drM" <md**@comcast.n et> writes:
Michael Mair wrote:
Nothing cannot be assigned to "anything" (anything non-void).
You can either change test() to
char *test (char *s);
or you have to do it in two steps:
test(helloworld );
printf("%s", helloworld);


Michael....If I understand you correctly, the type (void) is producing
an error, not because of the (void) test(helloworld ) function, but
because of the printf function...or am I completely misunderstandin g
you.


It's not really one or the other, it's the combination. Your test
function was declared to return void; you tried to pass the
(nonexistent) result of that function to printf. You called printf
with a "%s" format, which means you need to pass it a char* as its
second argument.

If you had passed, say, an integer value:

printf("%s", 42);

then you would have been invoking undefined behavior. Since the types
for the arguments to printf are determined by the value of the format
string, they can't generally be checked by the compiler. But you
can't pass a nonexistent value (the result of a void function) as an
argument, so the compiler rejects the call.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2690
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement -> full copy or virtual array spread in chunks over mem). The reason of my question is the following: // many function calls with smaller char arrays and less concats echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf');
12
2856
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go with the idea that a "dict" variable can be looked at as (effectively) two parallel arrays?...
7
7417
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars in string no matter how long I'm staring at it. This is the method(I printed out some lines for better understanding(by me)):
7
1677
by: Yodai | last post by:
Hi all... I am trying to construct an 2x14 array that compares a given character with the 1st column of data type "t" and returns the second column's appropriate value "v". I've been trying this, but doesn't seem to work.... Any idea of what I am missing? void Detect_type(void) { volatile char r1tipus = *R1TIPUS; unsigned char cadena = {
4
2500
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") != 0){
3
1992
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
8
7400
by: lasek | last post by:
Hi all, a simple question, look at this code below: char acName="Claudio"; unsigned int uiLen; uiLen=strlen(acName); printf("Length of acName variable %u",uiLen); //uiLen >>>> 7
14
4099
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
19
2061
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing character strings, test1.c #include <stdio.h> #define LEN 20 struct info {
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.