3
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <conio.h>
   char *preorden="GEAIBMCLDFKJH";//line 5

error in the above line

   char *inorden="IABEGLDCFMKHJ";//line 6

error in this line

   char *postorden;

error in this line

   void post(char *pre, char *in,  char *pos,int n)
   {
   int longIzqda;

   if(n!=0){
   pos[n-1]=pre[0];
   longIzqda=strchr(in,pre[0])-in;
   post (pre+1,in,pos,longIzqda);
   post (pre+1+longIzqda,in+1+longIzqda,pos+longIzqda,n-1-longIzqda);
     }
   }



   int main(int argc,char  *argv[])
   {
   int aux;

   aux=strlen(preorden);//convert to string 
   postorden=(char *)malloc(aux*sizeof(char));//use of malloc function
   if (postorden){
   printf("The preorden is: %s\n",preorden);
   printf("The inorden is: %s\n",inorden);
   post(preorden,inorden,postorden,aux);
   postorden[aux]='\0';
   printf("The  postorden calculated is: %s\n",postorden);
   free(postorden);
   }
   else{
   fprintf(stderr,"Whithout memory\n");
   return 1; // return 1 
   }

   return 0;
   }

the error is in the line 5 and 6 the compiler says: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

jay
  • 31
  • 3
  • Welcome to Stackoverflow. Please [format your code properly](https://stackoverflow.com/editing-help) and don't dump it. If you expect help from us, we expect some effort from you. – hellow Oct 29 '18 at 13:43
  • 5
    Use `const char *` – stark Oct 29 '18 at 13:46
  • 2
    `#ifdef __cplusplus` / `#error wrong compiler` / `#endif` – pmg Oct 29 '18 at 13:51
  • 1
    The 'deprecated conversion' message usually comes from G++; it isn't generated by GCC when compiling C, AFAICR — which is what's behind @pmg's comment. The problem is there in C, but the error message usually isn't. When compiled with `-Wwrite-strings`, the message _warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] `char *ptr = "abc";`_ is generated for the code line shown in the message. – Jonathan Leffler Oct 29 '18 at 14:01

2 Answers2

2

There are few issues with your code, firstly this

char *preorden="GEAIBMCLDFKJH";//line 5

forces compiler to warn you like below if compiled with -Wwrite-strings flags in C

deprecated conversion from string constant to 'char*' [-Wwrite-strings]

because the string literal GEAIBMCLDFKJH stored in read only section of primary memory i.e pointer where it points, that contents is read only, hence instead of char* use const char*. for e.g

char *preorden = "GEAIBMCLDFKJH";/* preorden is normal pointer but "GEAIBMCLDFKJH" is read only, hence error */

And

const char *preorden = "GEAIBMCLDFKJH"; /* const char *ptr means ptr contents is read only */

Secondly, here

   postorden=(char *)malloc(aux*sizeof(char));//use of malloc function

casting of malloc result is not required as malloc() return type is void* which is automatically and safely promoted to any other pointer type, Read Do I cast the result of malloc?. for e.g

postorden = malloc(aux * sizeof(*postorden));//use of malloc function

Also here(this point is about wrong comment on below line, please don't mind)

   aux=strlen(preorden);//convert to string 

strlen(preorden) returns the length of string pointed by preorden and gets assigned to aux not as written in comments(convert to string).

And change the post() definition to

void post(const char *pre, const char *in,  char *pos,int n) {
   /* some code*/
}
Achal
  • 11,821
  • 2
  • 15
  • 37
  • This answer is wrong. As [Jonathan Leffler](https://stackoverflow.com/users/15168/jonathan-leffler) points out, the error is likely due to compiling as C++, not C as tagged. C++ has stricter rules about conversions. C explicitly allows these conversions (C 2018 6.3.2. 6). The warning is due to a violation of a C++ rule. Whether the string is stored in read-only memory is an implementation detail and is not the reason for the warning. – Eric Postpischil Oct 29 '18 at 15:26
  • Note that it would be valid for the compiler to warn the user that the code has a problem in that it does or may write to a string literal, which is not permitted by the C standard. This is noted in Leffler’s comment and in this answer, but the warning message in that case says that the initialization discards qualifiers—is warning about something the code does that is problematic. But the messages the OP complains about says a **rule** is violated, that the conversion is deprecated. It is not deprecated in C, so we know this message comes from a C++ compilation, not a C compilation. – Eric Postpischil Oct 29 '18 at 15:30
  • Further, the message tells us why it is being issued. It is being issued because the conversion is deprecated. So it is not being issued because the string literal is in read-only memory. The message means what it says. – Eric Postpischil Oct 29 '18 at 15:31
  • Correction to my citation above: It should be C 2018 6.3.2.3 7. – Eric Postpischil Oct 29 '18 at 15:32
1

The message “deprecated conversion from string constant to 'char*' [-Wwrite-strings]” arises because the code was compiled as C++ code, which has different rules about string literals and pointer conversions from C.

This can be fixed by compiling the code as C code or worked around by inserting an explicit cast to char *.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312