473,769 Members | 6,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1718
"main()" <dn****@gmail.c omwrites:
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_Keit h) 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.c omwrites:
>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*****@macroe xpressions.comw rites:
Keith Thompson wrote:
>"main()" <dn****@gmail.c omwrites:
[...]
>>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_Keit h) 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*****@macroe xpressions.comw rites:
>Keith Thompson wrote:
>>"main()" <dn****@gmail.c omwrites:
[...]
>>>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*****@macroe xpressions.comw rites:
Keith Thompson wrote:
>Ark <ak*****@macroe xpressions.comw rites:
>>Keith Thompson wrote:
"main()" <dn****@gmail.c omwrites:
[...]
>>>>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_Keit h) 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*****@macroe xpressions.comw rites:
>Keith Thompson wrote:
>>Ark <ak*****@macroe xpressions.comw rites:
Keith Thompson wrote:
"main()" <dn****@gmail.c omwrites:
[...]
>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
2054
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 search php.net and have tried a dozen things I can't get to work
32
2531
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 Fortran and trying to do it in Python. I got it to work, but now I'm trying to modularize it. My fortran program uses a subroutine and I was trying to do the same thing in Python. But I'm still new so I'm having trouble understanding what I'm...
20
3503
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 "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
4
3626
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
5
2100
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
6
1771
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 the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
5
1897
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 automatic variables are retained somewhere? so that when the function is completed at a later point ( I hope I am expressing this vaguely correctly) those automatic variables that were retained are again used. Is my understanding correct? 2) In...
58
4684
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 the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
7
3365
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 based on the segments of the URI it will fire up some controller class, for instance, say I have an inbox in which end-users can view messages they got from other users, they'ld start at:
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.