This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
C++ forbids converting a `string` constant to `char*` - Alphabets to Morse converting program [duplicate]
(2 answers)
Closed 2 months ago.
The below is just a brief part of my code. And when I run it, it gives me the warning that ISO C++ forbids converting string to char* for visual studio code and exits with code 1. Is there a way I can ignore the warning to run the code?
Class Airport
{
public:
Airport(char *code, char *name);
}
Airport::Airport(char *code, char *name)
{
airport_code = new char[20];
strcpy(airport_code, code);
airport_name = new char[20];
strcpy(airport_name, name);
}
int main() {
Airport *lax = new Airport("LAX", "LA");
}
I saw on other stackoverflow posts that changing char to const char will solve the problem. But because the values will need to be changed using other predefined functions, it won't be the answer that I need.
Related
This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
Closed 4 months ago.
I am trying to run the following code which takes string parameter and returns the length of the string in characters in C language using Visual Studio Code, but I am getting:
Error message:
ISO C++ forbids converting a string constant to 'char*'
[-Wwrite-strings]
Here is my code:
int str_length(char *mystring){
int i=0;
for(i=0; mystring[i]!='\0'; i++);
return i;
}
void alpha(){
printf("%d\n", str_length("-h=123"));
printf("%d\n", str_length(""));
}
I am stuck with this task, maybe you could provide a possible solution or some parts of the code that I have to change?
It means that you are compiling your program as a C++ program.
In C++ opposite to C string literals have types of constant character arrays.
From the C++ 17 Standard (5.13.5 String literals)
8 Ordinary string literals and UTF-8 string literals are also referred
to as narrow string literals. A narrow string literal has type
“array of n const char”, where n is the size of the string as
defined below, and has static storage duration (6.7).
But even in C the function parameter shall have the qualifier const because the function does not change the passed string. The function can look the following way
size_t str_length( const char *mystring )
{
size_t i = 0;
while ( mystring[i] != '\0' ) i++;
return i;
}
So within the function alpha you need to write
printf("%zu\n", str_length("-h=123"));
printf("%zu\n", str_length(""));
changing the conversion specifier from %d to %zu. The signed type int can be not large enough to store the length of an arbitrary string.
This question already has answers here:
initializing char pointer as string vs other type pointers as arrays
(5 answers)
Initializing a char pointer C++ [duplicate]
(3 answers)
What is the type of string literals in C and C++?
(4 answers)
Closed 5 months ago.
so ive just begun learning about pointer basics and ive come across something im stuck on.
as the title says, should the value of the pointer must always be an address?
because i saw a line of code, which says otherwise:
char *text = "text";
this here is being used for the creation of a string, the other method is:
char text[] = "text";
which is pretty understandable.
could you guys explain to me what this line does exactly?
char *text = "text";
a pointer is being used but what does it do and point to? how can you use it to then access
the string created.
thanks.
"text" is a string literal. It is stored somewhere in memory and its address is used to initialise the pointer. You access the string as you would with any other pointer.
And as stated above
char *text = "text";
is not legal C++ (it is legal C) the correct C++ is
const char *text = "text";
This question already has answers here:
What is the type of string literals in C and C++?
(4 answers)
Closed 6 months ago.
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define nline "\n"
int main(){
//const char *p="hello world";
// court<<p;
char *p="hello world";
cout<<p;
}
C:\Users\Dell\AppData\Roaming\Sublime Text\Packages\User\cses2.cpp: In function 'int main()':
C:\Users\Dell\AppData\Roaming\Sublime Text\Packages\User\cses2.cpp:7:10: warning: ISO C++ forbids converting a string constant to 'char' [-Wwrite-strings]*
char *p="hello world";
^~~~~~~~~~~~~
"hello world" is a const char[12]. A c-array of 12 constant (!) characters (note that one is for the null terminator).
In early C++ standards it was allowed to get a char * to a string literal due to compatibility with C. Though, it was nevertheless forbidden to actually modify the characters of the string literal via the char *.
This changed in C++11 and since then string literals have proper const correctness.
If you do want a string that can be modified, then use a std::string. Note that the pointer you obtain from the literal is not a string, its just a pointer to a (non-modifiable) c-string. If you want a string rather than a pointer to one, you need to use a string.
This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
Deprecated conversion from string literal to 'char*' [duplicate]
(3 answers)
Closed 4 months ago.
I'm trying to run the following code, taken from "Object Oriented Programming with C++" by Balagurusamy (8th edition):
#include <iostream>
#include <cstring>
using namespace std;
class String
{
char *name;
int length;
public:
String()
{
length = 0;
name = new char[length+1];
}
String(char *s)
{
length = strlen(s);
name = new char[length+1];
strcpy(name,s);
}
void display(void)
{cout<<name<<"\n";}
void join(String &a, String &b);
};
void String :: join(String &a, String &b)
{
length = a.length + b.length;
delete name;
name = new char[length+1];
strcpy(name, a.name);
strcat(name, b.name);
};
int main()
{
char *first = "Joseph ";
String name1(first), name2("Louis"), name3("Lagrange"), s1,s2;
s1.join(name1, name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
return 0;
}
When I compile with g++, I run into the following log:
g++ -Wall -Werror -Wextra constructors_with_new.cpp -o constructors_with_new.o
constructors_with_new.cpp: In function ‘int main()’:
constructors_with_new.cpp:45:15: error: ISO C++ forbids converting a string constant to ‘char*’ [-Werror=write-strings]
45 | char *first = "Joseph ";
| ^~~~~~~~~
constructors_with_new.cpp:47:28: error: ISO C++ forbids converting a string constant to ‘char*’ [-Werror=write-strings]
47 | String name1(first), name2("Louis"), name3("Lagrange"), s1,s2;
| ^~~~~~~
constructors_with_new.cpp:47:44: error: ISO C++ forbids converting a string constant to ‘char*’ [-Werror=write-strings]
47 | String name1(first), name2("Louis"), name3("Lagrange"), s1,s2;
| ^~~~~~~~~~
cc1plus: all warnings being treated as errors
Then I found the following answer
Why is conversion from string constant to 'char*' valid in C but invalid in C++
and to make it work, I modified the above code
to receive a pointer to const char inside the 2nd constructor (String(char const *s))
inside main(), by changing the initialization of the first name "Joseph", from char * first to char const * first, as suggested by Jeremy Coffin in the answer to the provided link
In this way, it compiles without problems with the following output
Joseph
Louis
Lagrange
Joseph Louis
Joseph Louis Lagrange
What I wonder is whether this is the best way to fix this problem, or if you recommend a different way (maybe another that doesn't need to enter a pointer to a const of type char).
Best,
Stefano
As the comments indicate, you need to use const char*, not char*. It looks like the book is badly out of date.
In C++, a string literal is of type const char[N], where N is the number of characters in the literal plus one for the terminating '\0'.
I was able to make your code compile and run by making the following changed:
Change the constructor for String from
String(char *s)
to
String(const char *s)
Change the declaration of first from
char *first = "Joseph ";
to
const char *first = "Joseph ";
Remove the extraneous semicolon at the end of String::join.
Also, the code would be a lot more legible with proper indentation.
A string literal in C++ is by default a const char*. That's why you are seeing these warnings where you are trying to assigning a const char* to a char*.
You can simply remove these warnings by casting away the constness of the char*. You can achieve this by using const_cast<char*>
Eg:
char *first = const_cast<char*>("Joseph ");
String name1(first), name2(const_cast<char*>("Louis")), name3(const_cast<char*>("Lagrange")), s1,s2;
This should remove your warning.
Edit: This is not the best advice. My answer is only meant to explain how you can cast away const-ness of the string literal and in no way promotes it. The constness of string literal exists for a reason.
This question already has answers here:
Program crashes when trying to set a character of a char array
(5 answers)
Closed 5 years ago.
I am new to C++.
I have a program:
#include <iostream>
int main()
{
char* str = "Test";
*str = 'S';
}
The question is, why *str = 'S' crashes the program?
As far as I know str must be pointing to the first character of the string (well, char array), so in theory I should be able to modify it.
Is it because the memory is read-only for defined constant values?
I am using gcc 5.3.0.
why *str = 'S' crashes the program?
Because you're not allowed to modify string literals. The C++ standard allows them to be stored in read-only memory.
In fact, if you enable compiler warnings, you get:
prog.cc:5:16: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char* str = "Test";
^~~~~~
Always use const char* when pointing to string literals: