Re: [Rd] "warning: assignment discards qualifiers from pointer target type"

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed, 08 Jun 2011 12:22:10 +0100 (BST)

On Tue, 7 Jun 2011, Duncan Murdoch wrote:

> On 07/06/2011 9:08 AM, oliver wrote:

>> Hello,
>>
>> following an advice here from the list I looked into sources of other
>> packages (xts) and found the TYPEOF() macro/function, which really is
>> helpful.

It is documented, of course, but actually better alternatives are described in 'Writing R Extensions'.

We would urge you to study the R sources rather than copy bad habits from randomly chosen package sources.

>> I iused the follwong code snippet:
>>
>>
>> switch( TYPEOF( filename_sexp ) )
>> {
>> case STRSXP: filename = CHAR( STRING_ELT(filename_sexp, 0) );
>> break;
>>
>> default: error("filename argument must be a string");
>> break;
>> }
>>
>>
>> Here, filename is of type char*
>> and one function opens a file with that name.
>> So it is purely intended to just grab out the char* from the
>> String-Expression.
>>
>> Am I doing something wrong here, or is it ok, but I have somehow
>> to say the extracting macros/functions, that it is really intended
>> to throw away information and that a warning is not necessary?

>
> The result of calling CHAR should be a "const char *".  You are not allowed 
> to touch the string it points to.

Note too that isString() exists for this purpose, and there is no check in that code that LENGTH(filename_sexp) > 0 (or == 1). In the R sources you will often see things like

     if(!isString(sfile) || LENGTH(sfile) < 1)
         error("invalid '%s' argument", "description");

Then, reading on,

     file = translateChar(STRING_ELT(sfile, 0));

for you cannot (in general) assume that the character vector passed is in the native encoding.

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 08 Jun 2011 - 11:26:25 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 08 Jun 2011 - 17:10:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive