G
grishin-mailing-lists
Hi guys,
Writing a program I found that if a function returns char* it's
necessary to have its declaration within the compilation unit.
A simple example
/*----------------------------------------------*/
/* filename: casting_test.c */
#include <stdio.h>
char *Foo_int(const char *c)
{
return (char *)c;
}
int main(int argc, char *argv[])
{
char s[] = "Hi. It's a test string.";
char *n = NULL;
char *m = NULL;
n = Foo_ext(s); /* line #15 */
m = Foo_int(s);
puts(n);
puts(m);
return 0;
}
/*----------------------------------------------*/
Foo_ext is in separate Foo.c file.
Compiling this
gcc casting_test.c Foo.c
casting_test.c: In function `main':
casting_test.c:15: warning: assignment makes pointer from integer
without a cast
This message a misleading one because
if I add
char *Foo_ext(const char *c);
to casting_test.c (above main(...))
then no warnings appear.
It works well in both cases.
Writing a program I found that if a function returns char* it's
necessary to have its declaration within the compilation unit.
A simple example
/*----------------------------------------------*/
/* filename: casting_test.c */
#include <stdio.h>
char *Foo_int(const char *c)
{
return (char *)c;
}
int main(int argc, char *argv[])
{
char s[] = "Hi. It's a test string.";
char *n = NULL;
char *m = NULL;
n = Foo_ext(s); /* line #15 */
m = Foo_int(s);
puts(n);
puts(m);
return 0;
}
/*----------------------------------------------*/
Foo_ext is in separate Foo.c file.
Compiling this
gcc casting_test.c Foo.c
casting_test.c: In function `main':
casting_test.c:15: warning: assignment makes pointer from integer
without a cast
This message a misleading one because
if I add
char *Foo_ext(const char *c);
to casting_test.c (above main(...))
then no warnings appear.
It works well in both cases.