Re: [ROOT] Problem compiling character array

From: Jiri Masik (masik@fzu.cz)
Date: Tue May 28 2002 - 12:06:58 MEST


cstrato <cstrato@EUnet.at> writes:

> --------------020908090703030104030905
> Content-Type: text/plain; charset=us-ascii; format=flowed
> Content-Transfer-Encoding: 7bit
> 
> Dear Rooters
> 
> The following macro CharArray() below runs perfectly fine as macro,
> but I am not able to compile it. I get the error:
> "assignment to `char' from `const char *' lacks a cast"
> However, casting "str" to "char(str)" causes a different error.
> 
> Since I have to store letters read from an input file as character
> array "arrStr" in my code, my question is:
> What is wrong in my code example?
> How do I have to rewrite the code?
> Since I have to use "sscanf()", how do I access "str"?
> 
> Thank you in advance for your help.
> (MacOSX running root 3.03/05)
> 
> 
> Best regards
> Christian
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> C.h.r.i.s.t.i.a.n. .S.t.r.a.t.o.w.a
> V.i.e.n.n.a.         .A.u.s.t.r.i.a
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 
> 
> //-----------CharArray---------------
> void CharArray()
> {
>    char str = "A";
>    char *arrStr = new char[20];
> 
> // initialize array
>    Int_t i;
>    for (i=0; i<20; i++) {
>       arrStr[i]  = str;
> //      arrStr[i]  = str + i;
>    }
> 
>    for (i=0; i<20; i++) {
>       cout << arrStr[i];
>    }
>    cout << endl;
> 
> // read letters from input
>    char nextline[128];
>    ifstream input;
>    input.open("ABC.txt", ios::in);
>    for (i=0; i<20; i++) {
>       input.getline(nextline, 128);
>       sscanf(nextline, "%c \n", &str);
>       arrStr[i] = str;
>    }
>    input.close();
> 
>    for (i=0; i<20; i++) {
>       cout << arrStr[i];
>    }
>    cout << endl;
> 
>    delete [] arrStr;
> }//CharArray
> 
> 

Hi Christian,

this is because the string constant "A" is allocated as const char*. 
You can make work the first part  by changing char str to
char *str and modify the rest accordingly. 

Please note that for the second part where you try to modify contents
of str you need to allocate memory yourself. The place where "A"
resides is not writable.

cheers
        Jiri



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:54 MET