Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const vs #define

0 views
Skip to first unread message

Jose Luis

unread,
Jan 27, 2009, 11:33:05 AM1/27/09
to
Hi,

Given the code below:


<<<snip begin>>>

m3vmsa3.rraaupma /tmp > more p1.c p2.c defs.h

#include "defs.h"

int main()
{}

...skipping...

#include "defs.h"
...skipping...
#ifndef DEFS_H
#define DEFS_H

const char* STR1="Hello World";
const int i1=0;

#endif

<<<snip end>>>


If compiled:

m3vmsa3.rraaupma /tmp > aCC p1.c p2.c
p1.c:
p2.c:
/usr/ccs/bin/ld: Duplicate symbol "STR1" in files p1.o and p2.o
/usr/ccs/bin/ld: Found 1 duplicate symbol(s)


What is the best option for STR1: "static const" or "#define"?

Why is "i1" not a duplicate symbol?

Thanks in advance,
Jose Luis.


Victor Bazarov

unread,
Jan 27, 2009, 11:49:00 AM1/27/09
to

i1 is a const (by itself). STR1 is not a const, it's a regular pointer
to a const char. Make it

const char* const STR1 = ...

and it will be const.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

James Kanze

unread,
Jan 28, 2009, 5:17:58 AM1/28/09
to
On Jan 27, 5:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

> > #include "defs.h"

> > int main()
> > {}
>
> > ...skipping...

> > #endif

> > <<<snip end>>>

> > If compiled:

Alternatively, he could just skip the pointer:

char const STR1[] = "Hello World" ;

Most of the time, this is probably closer to what is actually
wanted.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages