A
arnuld
#include <stdio.h>
void check_arg(char* p);
int main(void)
{
char arr1[25];
char arr2[25] = {0};
check_arg(arr1);
check_arg(arr2);
return 0;
}
void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}
============= OUTPUT ==============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg =
arg =
[arnuld@dune programs]$
First array was not initialized, hence it contained garbage. 2nd array
was initialized with NULs but both of them are same when check_array()
checks them. See the 2nd version of the same program:
#include <stdio.h>
void check_arg(char* p);
int main(void)
{
char arr1[25];
char arr2[25] = {0};
check_arg(arr1);
check_arg(arr2);
return 0;
}
void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}
================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL
[arnuld@dune programs]$
The only difference between these 2 programs is the if condition of
check_array(), which checks for a pointer in 2nd case. *p accesses the
first element which is NUL in 2nd array (but not in first because
first was uninitialized, hence contains contains garbage ) but still
it says both are NUL. Why so ?
void check_arg(char* p);
int main(void)
{
char arr1[25];
char arr2[25] = {0};
check_arg(arr1);
check_arg(arr2);
return 0;
}
void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}
============= OUTPUT ==============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg =
arg =
[arnuld@dune programs]$
First array was not initialized, hence it contained garbage. 2nd array
was initialized with NULs but both of them are same when check_array()
checks them. See the 2nd version of the same program:
#include <stdio.h>
void check_arg(char* p);
int main(void)
{
char arr1[25];
char arr2[25] = {0};
check_arg(arr1);
check_arg(arr2);
return 0;
}
void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}
================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL
[arnuld@dune programs]$
The only difference between these 2 programs is the if condition of
check_array(), which checks for a pointer in 2nd case. *p accesses the
first element which is NUL in 2nd array (but not in first because
first was uninitialized, hence contains contains garbage ) but still
it says both are NUL. Why so ?