Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can make a program typing with c++?
For example with (char) can type a grapheme but i want type word.
char x;
x=='c';
But i want type for example 'equation'.
Someone know how can i do this?
Use a std::string. You can also use char* but i recommend you the first one. Remember that string goes within "" instead of ''.
string x; //You have to define what is 'x'.
x=="equation";
If you use char*, it behaves as an array of chars.
#include <string> // paste this line on top of the file
std::string name = "Bob"; // Use double quotes here.
if(name == "Bob")
{
std::cout << name << std::endl; // works as you expected
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This may be a trivial question for most but I am new to c++. My question is, how would I pass a pointer which is deference to a function to operate on the pointed value?
char first_name[] = "hello";
int myFunc(const char *source){
innerFunc(char *source){/*append world*/}
}
This doesnt seem to work.
One example:
char first_name[] = "hello";
int inner_func(const char* source) { /* do something, read-only */ }
int my_func(const char* source) {
inner_func(source);
}
So, you merely need to pass the name, that's all.
However, note that you have passed the pointer as const, which means that you cannot change it. Appending world does not work in that instance. In fact, if you would like to operate on your char string in a changing manner, you would need to create a second char* dynamically with the extended size. You cannot change source.
Also, inner functions like that cannot be defined in C++. Just define it outside of myFunc. You can create inner functions with lambdas, but this would be another answer.
Luckily, in C++, manipulating strings is far easier and deeply to recommend:
#include <string>
std::string first_name = "hello";
int inner_func(std::string& source) {
source += " world";
}
int my_func(std::string& source) {
inner_func(source);
}
Now, when you pass a string like first_name to my_func, it will be passed to inner_func where some string is appended.
Note, though, that hello world is quite a strange name, especially as a first name. It might not be what you want.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
While releasing memory of a const data, why do I need to const_cast it. What will be the result If I ignore it.
You don't. This is completely valid, standard conform, code:
int const * const a = new int(42);
delete a;
It sounds like you are using std::free from <cstdlib> to do this instead, which signature is
void free(void *);
In this case the implicit conversion from int const * const to void * fails because you can only implicit const-cast, not cast const away. You want the first version in C++ anyway.