Worried about A message that I get when compiling my program

Write your question here.
Here is the message:
C:\Dev-Cpp\Pointers\toCommunity.cpp In function 'int main()':
8 17 C:\Dev-Cpp\Pointers\toCommunity.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
THE PROGRAM COMPILES OKAY & RUNS FINE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  Put the code you need help with here.
//Shows string initialization
// uses pointers
#include<iostream>
using namespace std;

int main()
{
	char *salute = "I love you Greetings," ;

	cout << salute << endl ;

		salute = '\0';   //Assigning the null character to *salute
		
	cout << salute;
	
}


Worried about this message because in my next exercise when I now try to use a FUNCTION I run into bigger problems.
Here is my next exercise
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include <cstdio>
#include<string>
using namespace std;

int main(){
	
	  char *Array =  "I love you Greetings";
			
			cout << Array;

	changeToNull(Array);
	
	cout << Array;
}

  changeToNull(ptr)
		
	char *ptr ;
{
		*ptr = '\0';    //Assigning the null character to Array
	//cout<< Array;
}

Now this one does not compile at all
There are a number of problems in the first example.

As the compiler message indicates,
deprecated conversion from string constant to 'char*'
there is a problem with the declaration here:
 
    char *salute = "I love you Greetings," ;

Basically the literal "I love you Greetings," is read-only. That means you cannot change the contents of the string later. To properly write that line, the pointer should be declared as constant:
 
   const char *salute = "I love you Greetings," ;


So far, so good. But now what about this line,
 
    salute = '\0';   // Assigning the null character to *salute 

Well, the comment says you want to assign a null character in the location pointed to by salute. But that isn't what the code does, Instead, it alters the pointer itself and sets it to zero. Effectively that is now a null pointer, it doesn't point to anything at all.

But, when the code is corrected,
 
    *salute = '\0';   // Assigning the null character to *salute 
the compiler now gives the error message,
[Error] assignment of read-only location '* salute'



So where to go from here? Well since you want to modify the contents of the string, don't declare a pointer to a read-only literal. Instead, declare a character array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    char salute[] = "I love you Greetings," ;
    
    cout << salute << endl ;
    
    *salute = '\0';   // Assigning the null character to *salute
    
    cout << salute;
	
}


Notice at line 10, even though salute is an array, it is interpreted here as a pointer to the start of the array.
As for the second example, perhaps you need to review how to declare and define a function, the tutorial pages might help.
http://www.cplusplus.com/doc/tutorial/functions/

Note when passing an array to a function you may declare the function parameter as either an array char ptr[] or a pointer char * ptr, they will work in the same way.
Thank Chevril.
Here is the solution from the book that I am using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//zerostr.c
// tests function which puts null character in string
#include<iostream>
using namespace std;

int main() {
	char *phrase = "Don't test the river with both feet";
	cout<< "Phrase = %s." << endl <<phrase;
	zero(phrase);
	cout<< "Phrase = %s." << endl << phrase;
}

//Zero()
//Function to put null character at start of string
 zero(str)
	char *str;
	{
		*str = '\0';
	}

It gives there errors:

C:\Dev-Cpp\Pointers\zerostr.cpp In function 'int main()':

7 17 C:\Dev-Cpp\Pointers\zerostr.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]

9 13 C:\Dev-Cpp\Pointers\zerostr.cpp [Error] 'zero' was not declared in this scope

C:\Dev-Cpp\Pointers\zerostr.cpp At global scope:
15 6 C:\Dev-Cpp\Pointers\zerostr.cpp [Error] expected constructor, destructor, or type conversion before '(' token

17 2 C:\Dev-Cpp\Pointers\zerostr.cpp [Error] expected unqualified-id before '{' token

TO MY SURPRISE, The solution from the book does not compile at all!!!
Hmm, there are indeed problems with that code. If it is an example from a textbook, then perhaps you should throw it away and get a new textbook.

Also, there are a couple of things, such as the comment at the top, //zerostr.c and the use of %s in the cout statement which makes it look like something hastily converted from C to C++, without any testing.

Out of interest, what is the name/author of the book?

Though I think you would be well advised to stop using the book, here is an example of a possible solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

// Declare prototype of function
void zero(char * str);

int main() 
{
    char phrase[] = "Don't test the river with both feet";
    cout << "Phrase = " << phrase << endl;
    zero(phrase);
    cout << "Phrase = " << phrase << endl;
}

// Actually define the function
// Function to put null character at start of string
void zero(char * str)
{
    *str = '\0';
}
Last edited on
Yaaa!!! Maybe the book is too old Chevril it was written by: Robert Lafore in 1987.

I am in South Africa at Johannesburg , Soweto. I will go to the nearest library
and get a newer book. thanks
1987?

That would make it about thirty years old. Definitely only of interest as a piece of history.
Topic archived. No new replies allowed.