Thread: ISO C++ forbids converting a string constant to ‘char*’

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    ISO C++ forbids converting a string constant to ‘char*’

    still trying to figure out how to get a char * pass back out of a function( prams )
    while this works perfectly in C it does not in C++, but I need to do this very same thing in C++.

    Code:
    #include <stdio.h>
    
    void watchThis ( char ** point) {
        *point = "passing a char *  results back \n"
        "through the prams\n";
    }
    
    int main (void)
    {
     char * point;
     watchThis(&point);
     printf("%s\n", point);
    return 0;
    }
    now what is the best next thing?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,969
    Yeah, make it const.
    Code:
    void watchThis ( const char ** point) {
        *point = "passing a char *  results back \n"
        "through the prams\n";
    }
     
    int main (void)
    {
     const char * point;
     watchThis(&point);
     printf("%s\n", point);
    return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Yeah, make it const.
    Code:
    void watchThis ( const char ** point) {
        *point = "passing a char *  results back \n"
        "through the prams\n";
    }
     
    int main (void)
    {
     const char * point;
     watchThis(&point);
     printf("%s\n", point);
    return 0;
    }
    that is just too simple, let me give that a try.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    that was all I needed, thanks

    uhg pointers and passing thru funcion prams.

    I put it in here too because my functions now work in c and c++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deprecated conversion from string constant to char*
    By baxy in forum C++ Programming
    Replies: 7
    Last Post: 08-07-2013, 05:52 AM
  2. Replies: 8
    Last Post: 07-24-2011, 11:34 PM
  3. warning: deprecated conversion from string constant to char*
    By Albinoswordfish in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2008, 10:24 AM
  4. Replies: 7
    Last Post: 09-24-2008, 03:48 AM
  5. Replies: 5
    Last Post: 10-09-2002, 12:37 PM

Tags for this Thread