473,406 Members | 2,281 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,406 software developers and data experts.

Confused by a c-faq example

According to www.c-faq.com (Q6.6)

--------
Q: If you can't assign to arrays, then how can

int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}

work?

A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.

----------

I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?
Or am I missing something here ? (Tried the example in C99 mode as well
as C89) mystr[]'s value has not changed after:

char mystr[10];
mystr[0]='\0';
f(mystr);
printf("%s\n",mystr);

I have sofar always used strcpy in cases like this, but now I am in
doubt whether I overlooked something...

Thanks in advance.
Sh
Aug 7 '06 #1
7 1240
Schraalhans Keukenmeester <fi********************@xsfourall.ennelwrote:

(WRT FAQ 6.6)
I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?
You are not wrong; function arguments are always passed by value.
The only thing assigning directly to a function argument is likely to
accomplish is possibly saving you a variable declaration.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 7 '06 #2
Schraalhans Keukenmeester <fi********************@xsfourall.ennel>
writes:
According to www.c-faq.com (Q6.6)

--------
Q: If you can't assign to arrays, then how can

int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}

work?

A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.

----------

I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?
No you are not wrong. You are misunderstanding the reason its done as it
is above. It is only to show that there are cases where arrays can be
reassigned - in function calls where the compiler treats the parameter
as a char *. The pointer is being rewritten : not the contents of what
it originally pointed to.

A good text is "Expert C programming" by Van Der Linden - very
accessible. It spends a lot of time clarying something that many
programmers think they understand but dont at the end of the day. It is
confusing.

best of luck!

Or am I missing something here ? (Tried the example in C99 mode as well
as C89) mystr[]'s value has not changed after:

char mystr[10];
mystr[0]='\0';
f(mystr);
printf("%s\n",mystr);

I have sofar always used strcpy in cases like this, but now I am in
doubt whether I overlooked something...

Thanks in advance.
Sh
--
Lint early. Lint often.
Aug 7 '06 #3
In article <44**********************@news.xs4all.nl>
Schraalhans Keukenmeester <fi********************@xsfourall.ennelwrote:
>According to www.c-faq.com (Q6.6)
int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}
[snippage]
>I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?
You are correct about this, as others have noted.

As for why one might write f() as shown above ... well, consider
the following minor rewrite (to make "str" obviously a pointer)
with a partial expansion of the "...":

/*
* Perform various transformations. If the argument is "none"
* or "", the transformation is just a copy.
*/
int f(char *str) {
if (str[0] == '\0')
str = "none";
printf("please wait, doing <%stransformation\n", str);
... big nested loop ...
...
if (strcmp(str, "none") != 0)
... do some extra work ...
...
...
...
}

Without the assignment, this prints "doing <transformation"; with
it, it prints "doing <nonetransformation" (assuming str is an empty
string). Then, inside the nested loop, a single strcmp() against
"none" suffices. Without the assignment to "str", we might have
to write this as:

int f(char *str) {
printf("please wait, doing <%stransformation\n",
str[0] ? str : "none");
... big nested loop ...
...
if (strcmp(str, "") != 0 && strcmp(str, "none") != 0)
... do some extra work ...
...
...
...
}

(or similar). So changing the local variable "str", without changing
the caller's array, is sometimes a convenient way to reduce the
number of cases that must be handled inside the function.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 7 '06 #4
Richard <rg****@gmail.comwrites:
Schraalhans Keukenmeester <fi********************@xsfourall.ennel>
writes:
>According to www.c-faq.com (Q6.6)

--------
Q: If you can't assign to arrays, then how can

int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}

work?

A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.

----------

I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?

No you are not wrong. You are misunderstanding the reason its done as it
is above. It is only to show that there are cases where arrays can be
reassigned - in function calls where the compiler treats the parameter
as a char *. The pointer is being rewritten : not the contents of what
it originally pointed to.
[...]

This is not a case where an array can be reassigned. It's a case
where something whose declaration *looks like* an array declaration
can be reassigned. str is a pointer, not an array.

--
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 7 '06 #5
Keith Thompson <ks***@mib.orgwrites:
Richard <rg****@gmail.comwrites:
>Schraalhans Keukenmeester <fi********************@xsfourall.ennel>
writes:
>>According to www.c-faq.com (Q6.6)

--------
Q: If you can't assign to arrays, then how can

int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}

work?

A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.

----------

I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?

No you are not wrong. You are misunderstanding the reason its done as it
is above. It is only to show that there are cases where arrays can be
reassigned - in function calls where the compiler treats the parameter
as a char *. The pointer is being rewritten : not the contents of what
it originally pointed to.
[...]

This is not a case where an array can be reassigned. It's a case
where something whose declaration *looks like* an array declaration
can be reassigned. str is a pointer, not an array.
Isn't that what I said? Maybe my terminology is wrong? Its what I
intended to say. Oh, I see what you mean - fair enough. I suppose "the
array parameter" would have been better. Or?

,----
| >is above. It is only to show that there are cases where arrays can be
| >reassigned - in function calls where the compiler treats the parameter
| >as a char *. The pointer is being rewritten : not the contents of what
| >it originally pointed to.
`----
Aug 7 '06 #6
Richard <rg****@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrites:
>Richard <rg****@gmail.comwrites:
>>Schraalhans Keukenmeester <fi********************@xsfourall.ennel>
writes:
According to www.c-faq.com (Q6.6)

--------
Q: If you can't assign to arrays, then how can

int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}

work?

A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.

----------

I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?

No you are not wrong. You are misunderstanding the reason its done as it
is above. It is only to show that there are cases where arrays can be
reassigned - in function calls where the compiler treats the parameter
as a char *. The pointer is being rewritten : not the contents of what
it originally pointed to.
[...]

This is not a case where an array can be reassigned. It's a case
where something whose declaration *looks like* an array declaration
can be reassigned. str is a pointer, not an array.

Isn't that what I said? Maybe my terminology is wrong? Its what I
intended to say. Oh, I see what you mean - fair enough. I suppose "the
array parameter" would have been better. Or?
Not really, IMHO.

A point of terminology: the argument is the expression that appears in
the call, the parameter is the object declared in the function
declaration. (I don't mean to imply that you don't know this just
making sure everyone's talking about the same thing.)

Given the above function and a call like:

char arg[] = "hello";
...
str(arg);

there is neither an array argument (the expression "arg" is implicitly
converted to the address of its first element before the call, for
reasons that have nothing to do with the fact that it's a function
argument) nor an array parameter (in the prototype, "char str[]" isn't
just converted to a pointer it *means* "char *str"). In the body
of f(), the assignment
str = "none";
is simply a pointer assignment.

You're quite correct that

| The pointer is being rewritten : not the contents of what it
| originally pointed to.

but incorrect when you say before that that

| It is only to show that there are cases where arrays can be
| reassigned.

The code merely bears a superficial resemblance to what an array
assignment might look like, if such a thing existed in C.

--
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 7 '06 #7
Keith Thompson wrote:
Richard <rg****@gmail.comwrites:
>Keith Thompson <ks***@mib.orgwrites:
>>Richard <rg****@gmail.comwrites:
Schraalhans Keukenmeester <fi********************@xsfourall.ennel>
writes:
According to www.c-faq.com (Q6.6)
>
--------
Q: If you can't assign to arrays, then how can
>
int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}
>
work?
>
[snap]
>
Given the above function and a call like:

char arg[] = "hello";
...
str(arg);

there is neither an array argument (the expression "arg" is implicitly
converted to the address of its first element before the call, for
reasons that have nothing to do with the fact that it's a function
argument) nor an array parameter (in the prototype, "char str[]" isn't
just converted to a pointer it *means* "char *str"). In the body
of f(), the assignment
str = "none";
is simply a pointer assignment.

You're quite correct that

| The pointer is being rewritten : not the contents of what it
| originally pointed to.

but incorrect when you say before that that

| It is only to show that there are cases where arrays can be
| reassigned.

The code merely bears a superficial resemblance to what an array
assignment might look like, if such a thing existed in C.
Thanks a lot guys for your clear & concise explanations. Glad to see I
was right and at least start to have a grasp of this. Took me quite a
while, but after many hours of studying, experimenting and especially
reading the info on the site http://publications.gbdirect.co.uk/c_book/
I can honestly say I learned a lot. (Very good explanations imho, much
better than many of the books & texts I had read thusfar. It's old, not
updated for recent standards but good reading for novices I think,
especially the pointers & arrays parts)

I will also have a look at the text mentioned by Richard.

Cheers!
Sh.
Aug 8 '06 #8

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

Similar topics

6
by: ree32 | last post by:
I am a bit confused with capabilities of XML. I have an XML document with information on images(photos). Is there way to use XSL/XSLT to create a page that will display the images as gallery. ...
10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
2
by: Kevin C. | last post by:
Can someone explain why the file output produces all zeros? It seems to work fine in memory (e.g. passing char pointers to printf) but when I output the file, it comes out as zeros. Bookkeeping...
6
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
3
by: C CORDON | last post by:
I am verry confused about classes. I understand that classes can encapsulate properties, methods, events (don't know hoy to add events to a class), etc. I am confused with this: if you can...
4
by: SteveW | last post by:
Now that vista and the 3.0 framework are gold, I'm a bit confused, so any clarification would be very helpful. The 3.0 framework is gold and is downloadable etc. What is not 'gold' and...
2
by: abshirf2 | last post by:
Hello all, I am really confused, please help! :( I have tomcat 5.5 installed on my machine and i have the JDK/JRE installed. Everything is working fine. Now what i want to do is install php...
0
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
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: 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...
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
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,...
0
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...
0
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...
0
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...
0
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...

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.