473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Function argument as char []

void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()
{
foo("abcd");
}
Line:
str[0] = str[0]; throws an exception

whereas strA[0] = strA[0]; works fine.

Why?

Aug 21 '06 #1
13 1927


sh********@gmail.com wrote On 08/21/06 12:55,:
void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()
{
foo("abcd");
}
Line:
str[0] = str[0]; throws an exception

whereas strA[0] = strA[0]; works fine.
This is Question 1.32 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

--
Er*********@sun.com

Aug 21 '06 #2
In article <11**********************@m79g2000cwm.googlegroups .com>,
<sh********@gmail.comwrote:
>void foo(char str[])
{
char strA[] = "Hello";
strA[0] = strA[0];
str[0] = str[0];
}
>void main()
void is not one of the return types for main() which must be supported,
though it is allowed for a particular implementation to support
additional declarations for main() . Your code compiled this time;
in the next compiler release it might not.
>{
foo("abcd");
}
>Line:
str[0] = str[0]; throws an exception
whereas strA[0] = strA[0]; works fine.
Why?
This is undoubtedly in the C FAQ.

char strA[] = "Hello";
creates a char array of length 6 and initiazes it with the
characters 'H', 'e', 'l', 'l', 'o', and '\0'.
This array is writable, with the characters given in the string
considered to be just initial values -- it is short-hand for
using a list of char in the initializer.

foo("abcd");
passes to foo the address of a literal string that contains
"abcd\0" . It is the pointer that is passed, not the individual
characters. C implementations are allowed to put all literal
strings of this type into read-only memory.

If you were to change your foo() to use

char *strA = "Hello";

then you would find the same exception behaviour for it, as
in that case too what is stored is the pointer to the
literal string that is allowed to be read-only.

As a quick summary: if a given double-quoted string is acting
as a pointer to storage, then the string should be assumed to
be read-only; but if the given double-quoted string is in a declaration
to show what characters to put into something marked as a character array,
then the resulting character array is writable.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Aug 21 '06 #3

sh********@gmail.com wrote:
void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()
Not a valid declaration for main. Try:
int main (void);
{
foo("abcd");
Main returns int:
return 0;
}
Line:
str[0] = str[0]; throws an exception
There are no throwing of exceptions in C.
The line in questions produces undefined behavior, see the FAQ
<http://c-faq.com/>, question 1.32.
whereas strA[0] = strA[0]; works fine.

Why?
Robert Gamble

Aug 21 '06 #4
void foo(char str[])
This is equivalent to:

void foo(char *str)

You are NOT passing an array by value, but rather you're passing the
address of a single char by value.

int main()
{
foo("abcd");
}

This is equivalent to:

char const literal1[] = "abcd";

int main()
{
foo(literal1);
}

str[0] = str[0]; throws an exception

Look at the snippet immediately above, and notice that "literal1" is const.
The alteration of a const object results in undefined behaviour.

--

Frederick Gotham
Aug 21 '06 #5


Frederick Gotham wrote On 08/21/06 16:13,:
>[...]
>>int main()
{
foo("abcd");
}


This is equivalent to:

char const literal1[] = "abcd";

int main()
{
foo(literal1);
}
>>str[0] = str[0]; throws an exception


Look at the snippet immediately above, and notice that "literal1" is const.
The alteration of a const object results in undefined behaviour.
The equivalence is a little strained, because the
string in foo("abcd") is in fact not `const'. But that's
an accident of history, and not permission to modify the
string: You have to treat it as a constant despite its
non-`const'ness or suffer the undefined consequences.

--
Er*********@sun.com

Aug 21 '06 #6
Eric Sosman posted:
The equivalence is a little strained, because the
string in foo("abcd") is in fact not `const'. But that's
an accident of history, and not permission to modify the
string: You have to treat it as a constant despite its
non-`const'ness or suffer the undefined consequences.

I'll try again :)

#define STRING_LITERAL(arr_name) \
(*(char(*)[sizeof arr_name])&arr_name) \

char const literal1_X[] = {'a','b','c','d',0};
#define literal1 STRING_LITERAL(literal1_X)

int main()
{
foo(literal1);
}

--

Frederick Gotham
Aug 21 '06 #7
Frederick Gotham <fg*******@SPAM.comwrites:
[...]
>int main()
{
foo("abcd");
}
The above was written by sh********@gmail.com. Frederick, please
don't snip attributions.
This is equivalent to:

char const literal1[] = "abcd";

int main()
{
foo(literal1);
}

>str[0] = str[0]; throws an exception


Look at the snippet immediately above, and notice that "literal1" is const.
The alteration of a const object results in undefined behaviour.
No, string literals are not const (for historical reasons). But
attempting to modify a string literal does invoke undefined behavior,
because the standard exlicitly says so. (The language design would
have been cleaner, IMHO, if string literals *were* const, but it
wasn't possible to do that without breaking existing code.)

I suppose that "throwing" an "exception" is one possible consequence
of undefined behavior, even though C doesn't define exceptions.

--
Keith Thompson (The_Other_Keith) 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.
Aug 21 '06 #8
Keith Thompson posted:
The above was written by sh********@gmail.com. Frederick, please
don't snip attributions.

I usually don't do so, but the poster neglected to specify their name -- they
only specified an e-mail address. I didn't think it was customary to specify
their e-mail address in place of their name.

Do many of you specify the poster's e-mail address if no name is given?

--

Frederick Gotham
Aug 21 '06 #9
Frederick Gotham <fg*******@SPAM.comwrites:
Keith Thompson posted:
>The above was written by sh********@gmail.com. Frederick, please
don't snip attributions.

I usually don't do so, but the poster neglected to specify their
name -- they only specified an e-mail address. I didn't think it was
customary to specify their e-mail address in place of their name.

Do many of you specify the poster's e-mail address if no name is given?
If a poster doesn't specify a "real" name, most newsreaders will still
insert an attribution line containing just the e-mail address. I see
no point in snipping that.

A lot of people here use pseudonyms to identify themselves. Knowing a
poster's real name is less important, IMHO, than having a consistent
handle of some sort to identify the poster. There's no reason that
handle can't have an '@' character in it.

I would prefer that people pick some sort of name (possibly, but not
necessarily, a real name) to identify themselves, but I don't think
it's a huge deal.

sh********@gmail.com: You should be able to set a name in your gmail
account, in addition to your address. It would make it a bit easier
to follow discussions if you did so.

--
Keith Thompson (The_Other_Keith) 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.
Aug 21 '06 #10
Eric Sosman wrote:
sh********@gmail.com wrote On 08/21/06 12:55,:
>void foo(char str[])
{
char strA[] = "Hello";

strA[0] = strA[0];
str[0] = str[0];
}

void main()
{
foo("abcd");
}

Line:
str[0] = str[0]; throws an exception

whereas strA[0] = strA[0]; works fine.

This is Question 1.32 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/
The OP may not realize that str in foo is a pointer to a
non-writeable string, i.e. the constant "abcd".

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Aug 21 '06 #11
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
....
sh********@gmail.com: You should be able to set a name in your gmail
account, in addition to your address. It would make it a bit easier
to follow discussions if you did so.
Why? The contents of the From line should completely identify who is
writing what. (I said should, because it can be forged.)

There are various allowed formats, but *all* of them contain an e-mail
address. It is either:
e-mail-address
additional-info <e-mail-address>
or
e-mail-address (additional-info)

So if you attribute, you either attribute with the complete contents of the
From line (as I do), or if you want to be smart, you skip nothing for the
first format; the part between '<' and '>' for the second format and the
part before the '(', the '(' and the ')' for the third part. But in that
case you should not be surprised if you find:
In article blurb " " writes:
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 22 '06 #12
Frederick Gotham wrote:
Keith Thompson posted:
>The above was written by sh********@gmail.com. Frederick, please
don't snip attributions.

I usually don't do so, but the poster neglected to specify their
name -- they only specified an e-mail address. I didn't think it was
customary to specify their e-mail address in place of their name.

Do many of you specify the poster's e-mail address if no name is
given?
Yes. That is the identification that came with the article.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 22 '06 #13
"Dik T. Winter" <Di********@cwi.nlwrites:
In article <ln************@nuthaus.mib.orgKeith Thompson
<ks***@mib.orgwrites: ...
sh********@gmail.com: You should be able to set a name in your gmail
account, in addition to your address. It would make it a bit easier
to follow discussions if you did so.

Why? The contents of the From line should completely identify who is
writing what. (I said should, because it can be forged.)
Why not?

I'm not saying anyone is *required* to use a name in addition to an
e-mail address, just recommending it (as both you and I do). It just
makes it easier to follow conversations; I find names more memorable
than raw e-mail addresses.

--
Keith Thompson (The_Other_Keith) 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.
Aug 22 '06 #14

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

Similar topics

7
by: QQ | last post by:
I have a function defined in CC file like int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short g2p, bool s ); I put it in the header file as int D(char* p, char* e, char* c,...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
5
by: Martin Johansen | last post by:
Hello C programmers If I have a function, say void f(int a, char b); And I call this function with the following arguments char a; int b;
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
6
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
3
by: linarin | last post by:
#include <iostream> using namespace std; typedef bool (*CallableFunction)(int argc,char* argv); void DefineMyFunction(const char* name,CallableFunction func){ //here do the define action. }...
9
by: Yannick | last post by:
Hi everyone - I am not quite sure to understand what is really going on when a function defined in one translation unit calls a function defined in a different translation unit without knowing...
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.