Why in the Code "456"+1, Output Is "56"

Why in C++ when i concat a string with '+' like it don't concat but remove first char of the string

This is how pointer arithmetics work in C/C++.

Basically, "Hello world!" is just a const char* and its + operator works on it like a pointer. Which means that, for example, if you add 3 to it, then instead of pointing to the beginning of "Hello world" it will point to 3 characters after the beginning. If you print that, you get llo world.

So you're not doing a string concatenation there.

If you just want to print the stuff, you can use operator << and do something like this instead:

std::cout << "Hello world!" << id << std::endl;

If you need a string that actually works like a string, consider using std::string from the standard library or if you use something like Qt, you can use QString.

With a modern compiler (that supports C++11), you can use std::to_string to create a string from an int, and then you can do it like this:

std::string s = "Hello world!";
int i;
std::cin >> i;
s += std::to_string(i);
std::cout << s;

If you consider getting seriously into C++ app development, consider using a library like Qt or boost, which make your work just that much easier, although knowing the standard library well is also a good thing. :)

In C++: If we add some integer to string, why does it removes that number of characters from the beginning? (string + int)

"Hello World" is not std::string, it's a string literal so it's type is const char[]. When adding an integer like you're doing with i here you're actually creating a temporary const char* that first points to the first element of the const char[] which is 'H', then you move it 2 spots (because i is 2) so it points to 'l', then you pass that pointer to cout and thus cout starts printing from 'l'. Doing binary operations on such types is called Pointer Arithmetic.

To get a better understanding using an example char array, your code under the hood is similar to:

const char myHello[] = "Hello World";
const char* pointer = myHello; // make pointer point to the start of `myHello`
pointer += i; // move the pointer i spots
cout<< pointer <<endl; // let cout print out the "string" starting at the character pointed to by the new pointer.

Note that if you try to "move" the pointer too much so that it's pointing to something out of the string and then try to access this pointer you get Undefined Behaviour. Same as how accessing an array out of bounds is UB. Just make sure index < size.

Concatenating string and int in c++

A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.

Why does std::string return the tail of the string when adding an integer

You need to know your types. Firs of all, you are not adding 3 to the std::string. Addition happens before std::string is created. Instead you are adding 3 to the char[12], which is defined, since char array decays to char*, and adding 3 to it advances the pointer by 3 elements. This is exactly what you see.

Than std::string is constructed from the result, and you end up with a tail.

char concat to string returns wrong length


1 + "test" = "est"  // 1 offset from test

So you are getting the correct answer.

+---+---+---+---+---+
| t | e | s | t | \0|
+---+---+---+---+---+
+0 +1 +2 +3 +4

What you want is possibly:

std::string test;
test += x;
test += "test";

Result cout Hello + 1 endl; (C++)

Your example is roughly equivalent to this:

// `p` points to the first character 'H' in an array of 6 characters
// {'H', 'e', 'l', 'l', 'o', '\0'} forming the string literal.
const char* p = "Hello";
// `q` holds the result of advancing `p` by one element.
// That is, it points to character 'e' in the same array.
const char* q = p + 1;
// The sequence of characters, starting with that pointed by `q`
// and ending with NUL character, is sent to the standard output.
// That would be "ello"
std::cout << q << std::endl;

Insert numbers to array

I have formatted function add and added some comments to explain it, I hope they clarify what's happening.
Note that arr[i] == *(arr + i) and *arr == arr[0]

void add(int *arr, int n)
{
// p points to last number in arr
// Equivalent to &arr[n-1]
int *p = arr + n - 1;

// y points to where last number will be after spreading
// Equivalent to &arr[2*n - 2]
int *y = arr + n * 2 - 2;

// for n = 4 arr looks like this
// [A, B, C, D, _, _, _, _]
// ^ ^
// p y

// Spread numbers of arr
// [A, B, C, D, _, _, _, _] -> [A, _, B, _, C, _, D, _]
while (p > arr)
{
// Copy D to last position
*y = *p;
// Zero original D location, unnecessary
*p = 0;
// Move p backwards by one
p--;
// Move y backwards by two
y -= 2;

// arr now looks like this
// [A, B, C, _, _, _, D, _]
// ^ ^
// p y

// After next iteration
// [A, B, _, _, C, _, D, _]
// ^ ^
// p y

// After next iteration
// [A, _, B, _, C, _, D, _]
}

// Iterate over every other value
// and assign arr[i+1] to sum_of_digits[i]
p = arr;
while (p < arr + n * 2)
{
*(p + 1) = sum_of_digits(*p);
p += 2;
}
// Equivalent to
// for (int i = 0; i < n * 2; i += 2) {
// arr[i + 1] = sum_of_digits(arr[i]);
// }

// [A, sum_of_digits(A), B, sum_of_digits(B), C, sum_of_digits(C),
// D, sum_of_digits(D)]

// Print array
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}


Related Topics



Leave a reply



Submit