-2

I wrote a simple program about calculate the covariance. The data is read from a txt file and coverted to a matrix. and then the covariance is calculated by function calcov().

The warning deprecated conversion from string constant to char [-Wwrite-strings] is occured for the line printmatrix("cov.txt",cov);. I checked similar question here, but I still can not find where I go wrong, so I post it. I hope I can figure it out.

void calCov(gsl_matrix* data,gsl_matrix* cov,int p)
{
  for(int i=0;i<p;i++){
    for(int j=0;j<p;j++){
      gsl_vector_view a = gsl_matrix_column (data, i);
      gsl_vector_view b = gsl_matrix_column (data, j);
      gsl_matrix_set(cov,i,j,gsl_stats_covariance(a.vector.data, a.vector.stride,b.vector.data, b.vector.stride, a.vector.size));
    }
  }
}
int main()
{
  int n = 158; //sample size
  int p = 51; //number of variables
  int i;
  char datafilename[] = "erdata.txt";
  //allocate the data matrix
  gsl_matrix* data = gsl_matrix_alloc(n, p);
  //read the data
  FILE* datafile = fopen(datafilename, "r");
  fclose(datafile);

  gsl_matrix* cov = gsl_matrix_alloc(p, p);
  calCov(data,cov,p);
  printmatrix("cov.txt",cov);}
Benjamin Lindley
  • 95,516
  • 8
  • 172
  • 256
  • Similar question: http://stackoverflow.com/questions/59670/how-to-get-rid-of-deprecated-conversion-from-string-constant-to-char-warnin – excray Jun 03 '14 at 23:42
  • Don't pass pointers to constant data into functions that take pointers to non-constant data. – chris Jun 03 '14 at 23:43
  • @excray Thanks, but I already read it before I answer this question, and still do not know where I go wrong... – user3689282 Jun 03 '14 at 23:50
  • @quantdev Thanks, but I already read it before I answer this question, and still do not know where I go wrong... – user3689282 Jun 03 '14 at 23:50
  • What is the definition of `printmatrix`? What does it take as its first argument? If it takes a `char*`, then you cannot pass a string literal to that, since a string literal is an array of **const** chars. Change the parameter type to `char const*`. – Benjamin Lindley Jun 03 '14 at 23:52
  • Why did you change your post title? Your question is not about how to calculate co-variance. – Benjamin Lindley Jun 03 '14 at 23:57
  • @BenjaminLindley thanks a lots. I changed it and it worked. The things I still do not understand is I use `printmatrix` lots time in the same way,and it does not throw any warning. – user3689282 Jun 04 '14 at 00:42

1 Answers1

1

declare the first argument of printmatrix as a std::string or a pointer to char const.

by the way, when the problem is with a function called printmatrix, why not include that in the sample code.


background info: the type of the string literal "blah" is an array of 5 char const values (the fifth is the terminating zero). in C narrow string literals can be implicitly converted to char*, and this was supported in the first C++ standard, C++98, but was deprecated already then. the conversion was removed in C++11.

Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304
  • thanks a lots. I changed it and it worked. The things I still do not understand is I use `printmatrix` lots time in the same way,and it does not throw any warning. – user3689282 Jun 04 '14 at 01:13