0

I am studying c++ to get a good understanding of memory.

char *name = "example"; 

*(&name+1) = (char*)'A';

std::cout << name << std::endl;

is returning example not eAample.

My idea is to get the memory address of 'x' and changed it.

Is not a const so I should be able to change the value of the address right?

obs: I know how do it in other ways, I am curious to make this way.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 6
    I'd opt for _undefined behavior_. You're trying to access memory, you're not allowed to access. – πάντα ῥεῖ Jun 18 '22 at 10:23
  • 2
    You cannot change the elements of a `string-literal`. The code you have is undefined behavior. Also, what C++ compiler are you using that didn't warn you that `char *name="example";` is either deprecated, or not valid C++? – PaulMcKenzie Jun 18 '22 at 10:23
  • 3
    For studying C++, I recommend getting [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Learning by trial and error is not going to work. – Yksisarvinen Jun 18 '22 at 10:23
  • Besides *at least* you're supposed to cast to `char` instead of `char*` (although not casting also works) – user202729 Jun 18 '22 at 10:24
  • `char *name = "example";` => `const char *name = "example";`, `(char*)'A'` => `(char)'A'`, although it'll still won't work. You have to use `char name[] = "example"` to make it work – phuclv Jun 18 '22 at 10:27
  • *I know how do it in other ways, I am curious to make this way.* -- Change that to a writeable char array, not a string-literal. `char name[] = "example";` – PaulMcKenzie Jun 18 '22 at 10:28
  • [Here is an example of the warning](https://godbolt.org/z/5W1ndG1rv). Did you ignore it? And even if you ignore it, [this is an example of attempting to change a character in that string-literal](https://godbolt.org/z/nKPhMPK79). – PaulMcKenzie Jun 18 '22 at 10:31
  • Same question in C [c - Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s\[\]"? - Stack Overflow](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a), or in C++ https://stackoverflow.com/questions/5007369/program-crashes-when-trying-to-set-a-character-of-a-char-array – user202729 Jun 18 '22 at 10:32
  • If you are trying to learn C++ then start again. Look up `std::string`. If you are using `char*` or `char[]` in your first year then you are doing something wrong. Or you have a book that actually teaches you C disguised as c++. – Goswin von Brederlow Jun 18 '22 at 10:37
  • @OP *I know how do it in other ways, I am curious to make this way.* - This is the consequence of trying to learn C++ in an adhoc way, rather than following a good C++ book or set of good C++ books. You will end up wasting a lot of time writing invalid code, or writing valid code that is not idiomatic C++. – PaulMcKenzie Jun 18 '22 at 10:42

1 Answers1

5

This declaration

char *name = "example";

is invalid.

In C++ string literals have types of constant character arrays. It means that you have to write

const char *name = "example";

and you may not change the string literal.

Even in C where string literals have types of non-constant character arrays you may not change a string literal. Any attempt to change a string literal results in undefined behavior.

This expression

&name+1

points outside the pointer name. So dereferencing the pointer expression *(&name+1) invokes undefined behavior.

Also this expression (char*)'A' does not make a sense. It denotes that you are trying to use the value of the code of the character 'A' (that for example in ASCII is equal to 65) as an address of memory.

What you could do is the following

char name[] = "example"; 

*(name+1) = 'A';

std::cout << name << std::endl;

Or if you want to deal with pointer expressions then instead of

*(name+1) = 'A';

you could write

*(name+1) = *"A";

Here is a demonstration program that is based on your idea. The first range-based for loop will be compiled by a compiler that supports C++ 20. Otherwise you can rewrite it like

const char **p = name; 
for ( const auto &letter : "example")

Here you are.

#include <iostream>

int main()
{
    const char *name[sizeof( "example" )]; 

    for ( const char **p = name; const auto &letter : "example")
    {
        *p++ = &letter;
    }

    *( name + 1 ) = &"A"[0];

    for ( const char **p = name; **p; ++p )
    {
        std::cout << **p;
    }
    std::cout << '\n';
} 

The program output is

eAample
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335