What is the difference between adding these strings? - c++

Program 1:
#include <iostream>
using namespace std;
int main() {
string str;
char temp = 'a';
str += temp + "bc";
cout << str;
return 0;
}
Output:
Unknown characters
Program 2:
#include <iostream>
using namespace std;
int main() {
string str;
char temp = 'a';
str += temp;
str += "bc";
cout << str;
return 0;
}
Output:
abc
Why are both the outputs different? Shouldn't both outputs be the same?

This statement
str += temp + "bc";
can be represented like
str = str + ( temp + "bc" );
In the sub-expression temp + "bc" the string literal "bc" is implicitly converted to pointer to its first character and has the type const char *. The value of the variable temp is converted to the type integer due to the integer promotions that for example in ASCII table has the value 97.
So in the sub-expression there is used the pointer arithmetic. The expression temp + "bc" points to a memory that outside the string literal. So the result of the expression is undefined.
If you would write for example
char temp = 1;
then the expression temp + "bc" points to the second character of the string literal. As a result str would have the value 'b'.
Or to get the same result as in the second program you could write
str += temp + std::string( "bc" );
As for the second program then in this statement
str += temp;
str += "bc";
there are used overloaded operators += for the class std::string and objects of type char and char *. So these statements are well-defined.
Pat attention to that you should include explicitly the header <string>.
#include <string>

Program 1 in this line
str += temp + "bc";
first arguments of addition are evaluated
on right there is array of char: const char[3]
on left there is char which can be auto converted to int
const char[3] degrades to const char *
adding int (97) and const char * is doable, but results points beyond buffer range.
then you are using basic_string::operator+=( const CharT* s );, but argument is invalid. You have got buffer overflow.
Program 2.
Doesn't have this undefined behavior and operators form std::string are used.
Another reasonable version is:
str = str + temp + "bc";
Now each addition will create a std::string as a result.

std::string class has + and += operators overloaded, allowing you to use those to concatenate std::strings with each other, with individual characters and arrays of characters.
But just like in C, "bc" is not a std::string but a const char [3] (a constant array of 3 characters). Arrays are often automatically converted (decay) to pointers to their first elements. It happens here too.
str += "foobar"; appends the characters pointed by the pointer one by one, until it hits the null byte that terminates the string.
You can add integers to pointers: str += "foobar" + 3; will append "bar" to the string.
In C++, chars are simply small integers. So 'a' + "bc" actually means 97 + "bs" (assuming your compiler uses ASCII, all common ones do).
This forms a pointer that's out of bounds of the array and causes undefined behavior.
The random characters you're seeing are the contents of memory located 97 bytes after the "bc" array, terminated by a random null byte that was in that memory.

Related

How to create a char pointer in C++ that points to a concatenation of integers and commas?

Sorry if the terms are not right, just starting in C++ here... I have the following variables whose values can be 1 or 0 depending on another script result:
int number1, number2;
I need to format a variable as "X,Y" ending with a new line, so I did:
char c_number1 = '0' + number1; //going from int to char
char c_number2 = '0' + number2;
char comma = ',';
char new_line = '\n';
char concat_result[100] = {c_number1 + comma + c_number2 + new_line};
Here I don't get the expected result "X,Y" but a letter.
After that, I would need a pointer to that char variable, so I wrote:
const char *pointer_result = concat_result;
Not sure this is correct either.
Any guidance by any chance here?
Many different ways to handle this:
#include <string>
std::stream result = std::to_string(number1) + "," + std::to_string(number2) + "\n";
const char *pointer_result = result.c_str();
#include <sstream>
#include <string>
std::ostringstream oss;
oss << number1 << ',' << number2 << '\n';
std::string result = oss.str();
const char *pointer_result = result.c_str();
#include <cstdio>
char result[5];
std::sprintf(result, "%d,%d\n", number1, number2);
const char *pointer_result = result;
It seems you want to store each characters to separate elements of the array.
To archive that, use , instead of + to separate the elements.
char concat_result[100] = {c_number1, comma, c_number2, new_line};
This will initialize the first 4 elements with the specified characters and the other elements with zero.
Then you can use
const char *pointer_result = concat_result;
because most of arrays in expressions are converted to a pointer to the first element and assigning pointers without const to pointers having compatible types with const is allowed.

Arithmetic operation on mixed data type in C++ [closed]

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
I want to understand how arithmetic operation happen on different data type,
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
void newprintf(int, int, long, char *);
int main()
{
int i = 22;
newprintf(14, 30, (long) i, "9,999");
getch();
return 0;
}
void newprintf(int r, int c, long val, char *format)
{
char *p, str[20];
int len, i, j, lstr;
len = strlen(format);
_itoa(val, str, 10);
lstr = strlen(str);
p = str;
c += len;
p = str + lstr -1;
format = format + len - 1;
cout << "The value in p : " << p << endl;
cout << "The format is : " << format << endl;
}
Basically this program doesn't have a specific functionality, i just wanted to understand how the value of 'p' and 'format' are calculated, This program is compiled on Visual Studio.
Can anyone explain me how the value are calculated in detail ?
On running i get,
p = 2 and format = 9
Thanks in advance
char *p is a pointer to a char in memory. char str[20] is an array large enough to hold 20 characters. str[0] is a pointer to the first element in the array str. The line p = str is setting the character pointer p to point to the beginning of the array str.
strlen returns the number of characters in a string, so lstr = strlen(str) is setting lstr to the number of characters in the str array.
When adding or subtracting pointers, you are shifting the pointer according to the type. See the below example.
char *p, str[20];
p = str; //p now points to the beginning of str (a.k.a str[0])
p++; // We shifted p over by one character
// now (*p) == str[1]
p += 3; // We shifted p over 3 more characters
// now (*p) == str[1+3]
Back to your example, p = str + lstr - 1 is setting p to point to the base of str plus the number of characters in str (which is 20) minus 1. The minus one is important because if we were to do str + lstr and access str[20] we would be going out of bounds.
Essentially p is being set such that it points to the last character in the str array.
Basically, you have so-called operators. You can also write your own operators. There is many resources on net where you can check in detail how some operator works in detail. This probably will help you and answer that all things: http://en.cppreference.com/w/cpp/language/operator_arithmetic

c++ - char const * const * - <error reading characters of string>

I have a variable that is a pointer to a constant pointer to a constant char.
char const * const * words;
I then add the word "dog" to that variable.
words = (char const * const *)"dog";
However, when when I debug the code, it states this about words:
{0x616d7251 Error reading characters of string.}
My question is, how would I properly access the characters of that variable to the point where I can record each individual character of the string.
Here is some example code below:
char const * const *words;
words = (char const * const *)"dog";
for (int i = 0; i < 5; ++i)
{
char c = (char)words[i]; // Gives me, -52'i symbol', 'd', and then '\0'
// How do I access the 'o' and 'g'?
}
Thanks for the help in advance.
maybe you mean this
char const * const words = "dog";
for (int i = 0; i < strlen(words); ++i)
{
char c = words[i];
}
now of course in c++ code you should realy be using std::string
Short answer:
Your program has undefined behavior. To remove the undefined behavior use:
char const * word = "dog";
for (int i = 0; i < std::strlen(word); ++i)
{
char c = word[i];
}
or
char const * word = dog;
char const * const *words = &word;
for (int i = 0; i < std::strlen(*words); ++i)
{
char c = (*words)[i];
}
Long answer:
You are forcing a cast from char const* to char const* const* and treating the location of memory that was holding chars as though it is holding char const*s. Not only that, you are accessing memory using an out of bounds index.
Let's say the string "dog" is held in some memory location as (it takes four bytes that includes the null character) and give it an address.
a1
|
v
+---+---+---+----+
| d | o | g | \0 |
+---+---+---+----+
You can treat the address a1 as the value of a pointer of type char const*. That won't be a problem at all. However, by using:
words = (char const * const *)"dog";
You are treating a1 as though it is holding objects of type char const*.
Let's assume for a moment that you have a machine that uses 4 bytes for a pointer and uses little endian for pointers.
words[0] evaluates to a pointer. Its value will be:
'd' in decimal +
256 * 'o' in decimal +
256*256 * 'g' in decimal +
256*256*256 * '\0' in decimal.
After that, you truncate that value to char, which will give you back the character d, which is not too bad. The fun part (undefined behavior) begins when you access words[1].
words[1] is same as *(words+1). words+1 evaluates to a pointer whose value is the address a2.
a2
|
v
+---+---+---+----+
| d | o | g | \0 |
+---+---+---+----+
As you can see, it points to memory that is beyond what the compiler allocated for you. Dereferencing that pointer is cause for undefined behavior.
You are consistently missing the second *.
Ignoring the const stuff, you are declaring a char** word, which is a pointer to a pointer to a single char. You won't get a word or many words into that, and casting just hides the problem.
To get "dog" accessible through such a pointer, you need to take an extra step; make a variable that contains "dog", and put its address into your word.

How can I change Char digits to integer value

Since I am a newbie in this c++ programming. I kind a confuse with this question. If there is "const char str[]", How can I return this with integer value ??
for(int i=0; i<size; i++)
{
n = rand () % 98 + 1;
str [i] = n + '0';
}
Do I have to do str[i] = n -'0' ?
How can I return this character digit with integer value ??
First of all, if str is declared as a const char[] it means that the content of str is constant. Therefore you cannot modify it.
You could try to const_cast str, but it is usually a bad idea.
Secondly you can simply convert a char to an int by assigning your char variable to an intvariable.
char c = 'a';
int c_value = c;
You can find more inforation about const-correcteness here, and about type-casting here.

C++ pointer to char arithmetic

If I add 1 to a pointer, the actual value added will be the size of the type that the pointer points to right? For example:
int* num[5];
cout << *num << ", " << *(num + 2) << endl;
This will print the value stored at num[1] and at num[2],
so num + 2 is actually num + 2*sizeof(int) if I'm not wrong.
Now, if I initialize an array of pointers to char to string literals, like this one:
char* ch[5] =
{
"Hi",
"There",
"I,m a string literal"
};
This can be done because a string literal like "hi" represents the address of its first character, in this case 'h'. Now my question is how can I write something like:
cout << *(ch + 2);
and get "I,m a string literal" as the output?
Since the pointer points to char, shouldn't adding 2 to the pointer actually be (ch + 2*sizeof(char)) ? giving me the output 'There' ?
Does it have something to do with cout? Does cout search the memory of the pointed to values to see if it finds '\0's recognizing the contents of the pointed to values as strings and then modifying pointer arithmetic? But then adding 1 to a pointer to char pointing to strings would mean adding different number of bytes (instead of the size of a char) everytime, since a string can be any size. Or am I totally wrong? I'm sorry I am new to C++, and programming in gerenal.
The array isn't storing chars, it's storing char *s. Hence saying ch + 2 will be equivalent to ch + 2*sizeof(char *). You then dereference that, which is pointing to "I'm a string literal".
Your initial example shows the confusion:
int* num[5];
cout << *num << ", " << *(num + 2) << endl;
This is an array of pointers-to-int. Hence, *(num + 2) will be *(num + 2*sizeof(int *)), not 2*sizeof(int). Let's demonstrate this with a small program:
#include <iostream>
int main()
{
int *num[3];
int x, y, z;
x = 1;
y = 2;
z = 3;
num[0] = &x;
num[1] = &y;
num[2] = &z;
std::cout << *(num + 2) << "\n";
}
This will print out a memory address (like 0x22ff28) because it is holding pointers, not values.
In C and C++, arrays and pointers are very similar (many books claim they are exactly the same. This is not -quite- true, but it is true in a lot of situations).
Your first example should be int num[5]. Then *(num + 2) (which is equivalent to num[2]) will be equivalent to *(num + 2*sizeof(int). Hopefully this clears up your confusion somewhat.
"If I add 1 to a pointer, the actual value added will be the size of the type that the pointer points to right?"
There's no guarantee in the C++ Standard that a pointer is the number of the byte of some memory where the pointer points to. If you add an integer n to a pointer, the result is a pointer to the nth next element in that array:
int iarr[10];
int* pi = iarr; // pi points to iarr[0]
int* pi2 = pi+2; // pi2 points to iarr[2]
What you get when you look at, e.g. int repr = (int)pi; is not defined by the C++ Standard.
What will happen on the most popular platforms/implementations, is that
(int)pi2 == ((int)pi) + 2*sizeof(int)
When you have arrays of pointers, the exact same thing happens:
int* piarr[10];
int** ppi = piarr; // ppi points to iarr[0]
int** ppi2 = piarr+2; // ppi2 points to iarr[2]
Note that the type of piarr is array of 10 pointer to int, therefore the elements of that array have the type pointer to int. A pointer to an element of that array consequently has the type pointer to pointer to int.
char* ch[5] is an array of 5 pointers to char.
"Hello" etc. are (narrow) string literals. A (narrow) string literal is an array of n const char, where n is the length of the string plus 1 (for the terminating \0 character). Arrays can be implicitly converted to pointers to the first element of the array, this is what happens here:
char* ch[5] =
{
"Hi",
"There",
"I,m a string literal"
};
The array ch contains three pointer to char. As those have been obtained by converting arrays to pointers, each of them points to the first element of an array of char: The pointer ch[0] (the first element of the array ch) points to the first element of the array "Hi", ch[1] points to the first element of "There" and so on.
Note there's also a conversion involved from const char to char, which is deprecated and should be avoided. The better form would be:
char const* ch[5] =
{
"Hi",
"There",
"I,m a string literal"
};
The expression *(ch + 2) is interpreted as follows:
ch names that array (see above)
ch + 2 implicitly converts ch from array of 3 pointers to char to pointer to pointer to char, a pointer pointing to the first element of the array ch. The type of this expression therefore is pointer to pointer to char.
ch + 2 makes the pointer from the last step now point to the second next element; it pointed to the first element of ch, so it now points to the third element of the array ch.
*(ch + 2) finally, the * dereferences the pointer and "fetches" the object pointed to. The pointer created by ch + 2 points to the 3rd element of the array ch, therefore, this expression resolves into the third element of the array ch. The type of the expression now is pointer to char.
The result of the expression is passed to std::cout::operator<<. As the type of the expression is pointer to char, cout will print that string: the third element of the array ch.
In C, a character is represented by the datatype char. It can hold any ASCII character and it ranges from 0 to 255. Moreover it uses a single byte of size.
A string, however, is represented by char*, which technically is an array of chars. There's a difference. A char is not the same as a char*. The former stores a single character, and the latter stores a memory direction which corresponds to the offset of the string.
Now, in your example, ch is not a char* but a char**. This is, it is an array of an array of chars, or better said, an array of strings. If we dereference ch once, as in *ch, we will get the first string: Hi. If we dereference it twice, as in **ch, we will get the first character of the first string: H. So, we can start working with pointer arithmetics!
cout << *(ch + 2) will output I,m a string literal
cout << **(ch + 1) will output T (first character of second string)
cout << *(*ch + 1) will output i (second character of first string)
Keep on working with these examples to understand better how characters and strings are output! It's all about pointer arithmetics!
Does it have something to do with cout?
No:
const char* cstrings[5] =
{
"Hi",
"There",
"I,m a string literal"
};
const char** x = cstrings + 2;
cout << *x << endl;
.
.
But what can be confusing is that the << operator works differently when given a pointer to a cstring--instead of outputting the address, it outputs the string. Here is an example:
int x = 10;
int* pint = &x;
const char* pstr = "hello";
cout << pint << endl << pstr << endl;
--output:--
0x7fff5fbff85c //hexidecimal string representation of an integer
hello
Since the pointer points to char,
1) The literal strings are stored in your array as pointers. That's why the type of the array is pointer.
2) Pointers are adresses in memory, which are just integers.
3) So your array of pointers is really an array of integers.