Hello people,
here I was writing some thing which confused me abit. Here it is:
I have two pieces of code, let it be foo.c and bar.c, in first one i have
#define FOO "...."
and then
char lolo[]=FOO;
which is supposed to make an array of chars containing "...." and point
lolo to FOO, right?
here is foo.c
gizmo:~/coding/fun$ cat foo.c
#include <stdio.h>
#define FOO "...."
char lolo[]=FOO;
void show_extern(void);
void main (void) {
printf("lolo is %s\n and hex is %X\n",lolo,lolo);
show_extern();
}
-------
now bar :
I just want to use the same lolo here, so I declare it as extern :
gizmo:~/coding/fun$ cat bar.c
#include <stdio.h>
extern char *lolo;
void show_extern() {
printf("extern lolo is %X\n",lolo);
}
------------
now I compile them :
gizmo:~/coding/fun$ make clean && make
rm -f foo.o bar.o foo
gcc -Wall -ggdb -c foo.c -o foo.o
foo.c: In function `main':
foo.c:13: warning: unsigned int format, pointer arg (arg 3)
gcc -Wall -ggdb -c bar.c -o bar.o
bar.c: In function `show_extern':
bar.c:9: warning: unsigned int format, pointer arg (arg 2)
gcc -o foo foo.o bar.o -Wall -ggdb
warnings are ok, b/c I want to print the value of a pointer.
gizmo:~/coding/fun$ ./foo
lolo is ....
and hex is 8049540
extern lolo is 2E2E2E2E
anyone could explain this?:) I am really puzzled.
(probably compiler gets confused about #define things, but I am wondering
how could I make it think 'properly' and still use defines).
I've attached both pieces, in case if anyone would want to play..:)
#include <stdio.h>
#define FOO "...."
char lolo[]=FOO;
void show_extern(void);
void main (void) {
printf("lolo is %s\n and hex is %X\n",lolo,lolo);
show_extern();
}
#include <stdio.h>
extern char *lolo;
void show_extern() {
printf("extern lolo is %X\n",lolo);
}
CC=gcc
#CFLAGS= -DDEBUG -DPONG
CFLAGS= -Wall -ggdb
OBJS=foo.o bar.o
LDFLAGS=
all:$(OBJS)
$(CC) -o foo $(OBJS) $(CFLAGS) $(LDFLAGS)
clean:
rm -f $(OBJS) foo