By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Question on automatic variables and array arguments

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.