Re: [ROOT] Problem compiling character array

From: cstrato (cstrato@EUnet.at)
Date: Wed May 29 2002 - 20:18:33 MEST


Dear Jiri

Thank you for your suggestion. However, I had already tried this before
sending this mail and it did not work either.

It seems that I made a trivial mistake, since Victor Perevoztchikov 
suggested
to me to replace str = "A" with str = 'A', and this solved my problem.
I am not sure if there could be a memory problem with this solution. At the
moment it works perfectly fine.

Thank you both once again.

Best regards
Christian

Jiri Masik wrote:

>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