From: "Adrian Smith" Newsgroups: comp.os.msdos.djgpp References: <39533dab$1 AT news DOT telinco DOT net> Subject: Re: Pointers and Arrays (Newbie.) Date: Mon, 26 Jun 2000 16:31:28 +0100 Lines: 91 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2014.211 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 X-Original-NNTP-Posting-Host: ppp-1-184.cvx3.telinco.net Message-ID: <3957789c@news.telinco.net> NNTP-Posting-Host: news2.cluster1.telinco.net X-Trace: 26 Jun 2000 16:32:47 GMT, news2.cluster1.telinco.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Cheers Chris, Damian Yerrick had me worried for a moment. I understand where I went wrong now. As Simon Sulser pointed out too - If I leave the decleration in the header file and definition in the .cpp file - all is well . Ta. Chris Mears wrote in message news:pb4blsg62j72iaajejp50a766p5bl0sb4s AT 4ax DOT com... > On Sat, 24 Jun 2000 07:39:47 GMT, that hoopy frood Damian Yerrick > scribbled the following: > > >On Fri, 23 Jun 2000 11:29:15 +0100, "Adrian Smith" > > wrote: > > > >>My program has a header file in which I've got :- > >> > >>const char * message = "This is an error message \n" > >> > >>In the main .cpp file I've got :- > >> > >>cout << message << endl; > >> > >>When i try to compile, it comes up with:- > >> > >>error : : multiple defenition of 'message' > >> > >>If i use an array - ie, const char message[30] = "This is an error message > >>\n" > >>this works perfectly. Could somebody please tell me where Iam going wrong > >>with the pointer method. > > > >You're trying to store a string inside a pointer. > > No, he's not trying to do anything of the sort. He's trying to have a > pointer point to an array of char, which is perfectly normal. > > > This is just wrong. > > No, it's just right. > > >Strings are supposed to go into character arrays. > > A string literal is a character array. > > >If you want an > >array that is just big enough to hold what you put into it, do this: > > > >const char message[] = "hello world\n"; > > That's not what you do if you want a pointer to a string literal, > though. > > To the OP: > You were getting a multiple definition error. I can't be certain of > the problem from what you've posted, but my guess is that you are > including the header from two different modules and linking them > together, which creates two definitions of the one variable (hence the > "multiple definition"). > > Change the definition in the header to a declaration by removing the > initialisation and in exactly one of the source files, add an > initialiser. > > == a.h == > > char *message; > > == b.c == > > char *message = "I am a foo bird."; > > == c.c == > > #include > #include "a.h" > void foo(void) > { > /* prints "I am a foo bird." */ > puts(message); > } > > -- > Chris Mears > ICQ: 36697123