473,396 Members | 1,965 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,396 software developers and data experts.

Question on automatic variables and array arguments

I'm a newbie.
These questions arose out of my curiosity, Please pardon me if this
questions sound silly.

1. Why are the automatic variables are left uninitialized by default(
as i understand only global and static variables are initialized to
zero) ? What prevents a complier in doing so? If there are performance
issues , why are global and static variables initialized to zero ?

2. In the following code:

#include <stdio.h>

void func1(char str[])
{
/*Do something with str*/
}

void func2(char *str)
{
/*Do something with str*/
}

int main(void)
{
char a[10]; /*array*/
char *b; /*pointer , memory is allocated somewhere below*/
.......
.......
func1(a);
func2(a);
func1(b);
func2(b);
}

Both func1 and func2 perform the same operation(the lines of code are
also the same). The difference is that one takes array as the argument
and second one takes pointer as the argument. All the four function
calls works fine.
My question is, which function (func1 or func2) is better ?
Is it a matter of style ?

3. If i can pass a pointer to a function expecting a array ( say
func1(b) is the previous question) , why can't i do the following ?
char *b;
char c[] = b;

Aug 2 '06 #1
6 1697
"main()" <dn****@gmail.comwrites:
I'm a newbie.
These questions arose out of my curiosity, Please pardon me if this
questions sound silly.

1. Why are the automatic variables are left uninitialized by default(
as i understand only global and static variables are initialized to
zero) ? What prevents a complier in doing so? If there are performance
issues , why are global and static variables initialized to zero ?
Typically, it's very easy for an implementation to initialize all
variables with static storage duration at once, when the program is
loaded. Initialization of local variables would have to be done via
explicitly generated code. Of course, if you want a local variable to
be initialized, you can always do it yourself.
2. In the following code:

#include <stdio.h>

void func1(char str[])
{
/*Do something with str*/
}

void func2(char *str)
{
/*Do something with str*/
}
func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.

You should read section 6 of the comp.lang.c FAQ, <http:/www.c-faq.com>.

--
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 2 '06 #2
Ark
Keith Thompson wrote:
"main()" <dn****@gmail.comwrites:
>I'm a newbie.
These questions arose out of my curiosity, Please pardon me if this
questions sound silly.

1. Why are the automatic variables are left uninitialized by default(
as i understand only global and static variables are initialized to
zero) ? What prevents a complier in doing so? If there are performance
issues , why are global and static variables initialized to zero ?

Typically, it's very easy for an implementation to initialize all
variables with static storage duration at once, when the program is
loaded. Initialization of local variables would have to be done via
explicitly generated code. Of course, if you want a local variable to
be initialized, you can always do it yourself.
>2. In the following code:

#include <stdio.h>

void func1(char str[])
{
/*Do something with str*/
}

void func2(char *str)
{
/*Do something with str*/
}

func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.

You should read section 6 of the comp.lang.c FAQ, <http:/www.c-faq.com>.
Hmmmm...
In a hairsplitting way, I think func1 is equivalent to void func3(char
*const str).
- Ark
Aug 2 '06 #3
Ark <ak*****@macroexpressions.comwrites:
Keith Thompson wrote:
>"main()" <dn****@gmail.comwrites:
[...]
>>2. In the following code:

#include <stdio.h>

void func1(char str[])
{
/*Do something with str*/
}

void func2(char *str)
{
/*Do something with str*/
}
func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.
You should read section 6 of the comp.lang.c FAQ,
<http:/www.c-faq.com>.
Hmmmm...
In a hairsplitting way, I think func1 is equivalent to void func3(char
*const str).
Nope.

C99 6.7.5.3p7:

A declaration of a parameter as "array of type" shall be adjusted
to "qualified pointer to type", where the type qualifiers (if any)
are those specified within the [ and ] of the array type
derivation.

--
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 2 '06 #4
Ark
Keith Thompson wrote:
Ark <ak*****@macroexpressions.comwrites:
>Keith Thompson wrote:
>>"main()" <dn****@gmail.comwrites:
[...]
>>>2. In the following code:

#include <stdio.h>

void func1(char str[])
{
/*Do something with str*/
}

void func2(char *str)
{
/*Do something with str*/
}
func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.
You should read section 6 of the comp.lang.c FAQ,
<http:/www.c-faq.com>.
Hmmmm...
In a hairsplitting way, I think func1 is equivalent to void func3(char
*const str).

Nope.

C99 6.7.5.3p7:

A declaration of a parameter as "array of type" shall be adjusted
to "qualified pointer to type", where the type qualifiers (if any)
are those specified within the [ and ] of the array type
derivation.
Qualified.
I don't think
void func1(char str[])
{
while(str[0])
str++[0] = 'Z'; //or, as normal people write, *str++ = 'Z';
}
will compile. func2(char *str) will.
- Ark
Aug 2 '06 #5
Ark <ak*****@macroexpressions.comwrites:
Keith Thompson wrote:
>Ark <ak*****@macroexpressions.comwrites:
>>Keith Thompson wrote:
"main()" <dn****@gmail.comwrites:
[...]
>>>>2. In the following code:
>
#include <stdio.h>
>
void func1(char str[])
{
/*Do something with str*/
}
>
void func2(char *str)
{
/*Do something with str*/
}
func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.
You should read section 6 of the comp.lang.c FAQ,
<http:/www.c-faq.com>.

Hmmmm...
In a hairsplitting way, I think func1 is equivalent to void func3(char
*const str).
Nope.
C99 6.7.5.3p7:
A declaration of a parameter as "array of type" shall be adjusted
to "qualified pointer to type", where the type qualifiers (if any)
are those specified within the [ and ] of the array type
derivation.
Qualified.
I don't think
void func1(char str[])
{
while(str[0])
str++[0] = 'Z'; //or, as normal people write, *str++ = 'Z';
}
will compile. func2(char *str) will.
And what is your basis for this belief? Did you try it?

They really are equivalent. Your "func1" compiles without error with
"gcc -c -ansi -pedantic -Wall -W -O3" after I remove the "//" comment,
and with "cc -c -Xc" on Solaris. This of course doesn't prove
anything, but it is consistent with the idea that the C99 6.7.5.3p7
really means what it says. The word "qualified" refers to any type
qualifiers appearing between the "[" and "]"; in this case, there are
none.

These are equivalent:

void foo(char str[]) { ... }
void foo(char *str) { ... }

So are these:

void bar(char str[const]) { ... }
void bar(const char *str) { ... }

(The "[const]" form is not supported in C90.)

--
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 2 '06 #6
Ark
Keith Thompson wrote:
Ark <ak*****@macroexpressions.comwrites:
>Keith Thompson wrote:
>>Ark <ak*****@macroexpressions.comwrites:
Keith Thompson wrote:
"main()" <dn****@gmail.comwrites:
[...]
>2. In the following code:
>>
> #include <stdio.h>
>>
> void func1(char str[])
> {
> /*Do something with str*/
> }
>>
> void func2(char *str)
> {
> /*Do something with str*/
> }
func1 and func2 are equivalent. In a parameter declaration (and
*only* in a parameter declaration), "char str[]" really means "char
*str". Personally, I prefer "char *str" because it indicates what's
really going on.
You should read section 6 of the comp.lang.c FAQ,
<http:/www.c-faq.com>.
>
Hmmmm...
In a hairsplitting way, I think func1 is equivalent to void func3(char
*const str).
Nope.
C99 6.7.5.3p7:
A declaration of a parameter as "array of type" shall be adjusted
to "qualified pointer to type", where the type qualifiers (if any)
are those specified within the [ and ] of the array type
derivation.
Qualified.
I don't think
void func1(char str[])
{
while(str[0])
str++[0] = 'Z'; //or, as normal people write, *str++ = 'Z';
}
will compile. func2(char *str) will.

And what is your basis for this belief? Did you try it?

They really are equivalent. Your "func1" compiles without error with
"gcc -c -ansi -pedantic -Wall -W -O3" after I remove the "//" comment,
and with "cc -c -Xc" on Solaris. This of course doesn't prove
anything, but it is consistent with the idea that the C99 6.7.5.3p7
really means what it says. The word "qualified" refers to any type
qualifiers appearing between the "[" and "]"; in this case, there are
none.

These are equivalent:

void foo(char str[]) { ... }
void foo(char *str) { ... }

So are these:

void bar(char str[const]) { ... }
void bar(const char *str) { ... }

(The "[const]" form is not supported in C90.)
I admit that I was wrong (all along)
- Ark
Aug 2 '06 #7

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

Similar topics

7
by: chad phillips | last post by:
Hi, (Newbie to php). I am trying to read in a list of ids. Here is my url http://localhost/a.php?id=1&id=2&id=3 Then I just want to print out the ids, but I can't seem to get it work. I...
32
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
5
by: mdh | last post by:
This little program, which is heavily adapted from K&R (Exc 4-12) converts an integer to a string, recursively. My questions are these. 1) I think that as each successive recursion occurs, the...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
7
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter framework. Basically, the site will examine the URI and...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.