Question on automatic variables and array arguments

M

main()

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;
 
K

Keith Thompson

main() said:
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>.
 
A

Ark

Keith said:
main() said:
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
 
K

Keith Thompson

Ark said:
Keith said:
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.
 
A

Ark

Keith said:
Ark said:
Keith said:
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
 
K

Keith Thompson

Ark said:
Keith said:
Ark said:
Keith Thompson wrote:
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.)
 
A

Ark

Keith said:
Ark said:
Keith said:
Keith Thompson wrote:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top