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