473,546 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between the 2 sentences?

Hi, all:

The 1st sendtence:

int main(){
char string[20]={""};
string[20] = {" connected "};
.....
}
The second sentence:
int main(){
char string[20]={""};
string[20]=" connected ";
....
}

Thanks for any comments;

bin YE

Nov 23 '05 #1
23 1886
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
yezi <ye*****@hotmai l.com> wrote:
The 1st sendtence: int main(){
char string[20]={""};
You set up an array of 20 char and initialize it with \0 in the first
position.
string[20] = {" connected "};
You then attempt to assign into the 21st char of that array. The
value you attempt to assign is, however, not a valid expression,
because {} in an expression context is a block grouping which does not
have an associated value. Unlike, for example, (a,b,c) in which the
associated value is the value of c (the comma operator).
....
} The second sentence:
int main(){
char string[20]={""};
You set up an array of 20 char and initialize it with \0 in the first
position.
string[20]=" connected ";
You then attempt to assign into the 21st char of that array. The
value you attempt to assign is, however, a pointer to a character
string.
...
}

You might want to explore the differences (if any) between

char string1[20] = "connected" ;
char string2[20] = { "connected" };
char string3[] = "connected" ;
char string4[] = { "connected" };
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Nov 23 '05 #2
yezi wrote:
int main(){
char string[20]={""};
string[20] = {" connected "};
....
}


$ cat test.c
int main()
{
char string[20] = {""};
string[20] = {" connected "};
return 0;
}
$ gcc test.c
test.c: In function ¡®main¡¯:
test.c:4: error: syntax error before ¡®{¡¯ token
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Nov 23 '05 #3
yezi wrote:
Hi, all:

The 1st sendtence:

int main(){
char string[20]={""};
string[20] = {" connected "};
....
}
The second sentence:
int main(){
char string[20]={""};
string[20]=" connected ";
...
}


Both "sentences" 1 and 2 are wrong because you attempt to assign a
string to a char. The value of string[0] or string[10] or string[19]
etc.. can only be a char.

Also trying to access string[20] is a memory access violation. This may
or may not crash your program depending on your OS and CPU. An array
that has the size of 20 elements may only have intexes up to 19.
Remember that in C, arrays are zero based. So, the maximum you can get
is string[19].

In addition, "sentence" 1 is wrong because the {brackets} are not valid
C constructs in that particular case.

Nov 23 '05 #4
yezi <ye*****@hotmai l.com> wrote:
int main(){
char string[20]={""};
string[20] = {" connected "};
}


In addition to the more helpful comments you have already received,
"string" is an identifier reserved for use by the implementation, and
you should thus not use it. (IIRC, that statement only applies if
you've included <string.h>.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #5

Christopher Benson-Manica wrote:
yezi <ye*****@hotmai l.com> wrote:
int main(){
char string[20]={""};
string[20] = {" connected "};
}


In addition to the more helpful comments you have already received,
"string" is an identifier reserved for use by the implementation, and
you should thus not use it. (IIRC, that statement only applies if
you've included <string.h>.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.


str... is apparently only reserved for function names.
from n869:

7.26.10 General utilities <stdlib.h>

[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.

7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.

-David

-David

Nov 23 '05 #6

David Resnick wrote:
Christopher Benson-Manica wrote:
yezi <ye*****@hotmai l.com> wrote:
int main(){
char string[20]={""};
string[20] = {" connected "};
}


In addition to the more helpful comments you have already received,
"string" is an identifier reserved for use by the implementation, and
you should thus not use it. (IIRC, that statement only applies if
you've included <string.h>.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.


str... is apparently only reserved for function names.
from n869:

7.26.10 General utilities <stdlib.h>

[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.

7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.

-David


I posted the above hastily. But isn't a variable at block scope
called str... OK? Or can the header make its functions macros, in
which
case this wouldn't fly either?

-David

Nov 23 '05 #7
"David Resnick" <ln********@gma il.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.

-David


I posted the above hastily. But isn't a variable at block scope
called str... OK? Or can the header make its functions macros, in
which
case this wouldn't fly either?


If strings could fly, we wouldn't need kites.

-Mike
Nov 23 '05 #8
David Resnick <ln********@gma il.com> wrote:
I posted the above hastily. But isn't a variable at block scope
called str... OK? Or can the header make its functions macros, in
which
case this wouldn't fly either?


IANALL, but I believe this paragraph from 7.1.3 applies...

"All identifiers with external linkage in any of the following
subclauses (including the future library directions) are always reserved
for use as identifiers with external linkage."

The wording of the text you posted seems to preclude the reserved str*
names being macros, so I think the paragraph concerning macro names
immediately preceding the above does not apply. So it seems that str*
are acceptable at block scope (as in OP's code) and at file scope, if
I've read this correctly.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #9
Mike Wahler <mk******@mkwah ler.net> wrote:
If strings could fly, we wouldn't need kites.


And here I was reading your post anticipating an illuminating response
:-)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #10

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

Similar topics

220
18830
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
4
4378
by: Tony | last post by:
Hello, Can someone please point me toward a regular expression that goes through a string and contructs a list of sentences and part sentences, where words are gradually dropped from the front of the current sentence? Sound confusing? Well perhaps an example would help? Given... "Different countries have different ideas. Merry...
40
3331
by: | last post by:
Could someone cite some offical rule or documentation with regard to the <P> tag? I've seen folks put it in between paragraphs... and others wrap it around a paragraph. I'd think to use it effectively, it'd have to be wrapped, correct? Thanks, - Michael
6
7479
by: Patrick | last post by:
Hi I am a newbie struggling a little with css.It is hard to get it right in all browsers, so i decided to read the CSS2 specification on the w3 site. What is the following from the CSS2 specification: "Quote starts": 5.6 Child selectors
15
29742
by: Randall Parker | last post by:
I've noticed when exporting from Microsoft Word XP into an HTML file that Word uses a span style of mso-spacerun: yes. This has the effect of making there be about 2 spaces between sentences. So you will see a sentence and then the tag.<span style='mso-spacerun: yes'> </span>Well, this next sentence will occur two spaces later because...
2
1261
by: devil_online | last post by:
Hi, I want to put sentences in a page...like start at 9pm and ads more sentences like 5 in 5minutes, not erasing the others. how can I do it in javascript? thanks
669
25603
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
17
12140
by: Umesh | last post by:
Please try to do it while I try myself!
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
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...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3491
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
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...

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.