Originally Posted by
sahilpatel_13
Code:
#include <stdio.h>
int directory()
{
char a1[3]={'a','n','d'};
...
return p;
}
You declared `directory' to return int but you are actually returning p, which is a char *. Also, as pointed out, the contents of the array will be garbage once you leave the function. Make the array static if you want a quick fix for this problem without using dynamic memory:
Code:
#include <stdio.h>
char *directory()
{
static char a1[4]={'a','n','d','\0'};
char *p;
p=&a1[0];
printf("%p\n", p);
return p;
}
int main(void)
{
char *j;
j=directory();
// ...
printf("contents of j: \"%s\"\n", j);
return 0;
}
Also notice: char arrays typically have '\0' at the end. This is required to print its contents with printf.