www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/26/12:03:29

From: "Adrian Smith" <Adrian AT atpelectronics DOT connectfree DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
References: <39533dab$1 AT news DOT telinco DOT net> <nvm8lsgqmcgdur1to6vbgnlit7jn2mdalk AT 4ax DOT com> <pb4blsg62j72iaajejp50a766p5bl0sb4s AT 4ax DOT com>
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 <cmears AT bigpond DOT com> 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"
> ><Adrian AT atpelectronics DOT connectfree DOT co DOT uk> 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 :<filename.cpp> :  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 <stdio.h>
> #include "a.h"
> void foo(void)
> {
>     /* prints "I am a foo bird." */
>     puts(message);
> }
>
> --
> Chris Mears
> ICQ: 36697123


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019