7 Replies - 436 Views - Last Post: 27 May 2019 - 06:19 PM Rate Topic: -----

#1 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-February 19

int concanating with string C++

Posted 23 May 2019 - 01:11 PM

Hi guys!
I'm wondering why if I want to concatenate integer with string as I should do like this in C++:
6+'0'+"hello" => "6hello"
why should i add '0' and not using 6+"hello" immediately? what does +'0' mean?

thanks alot
Is This A Good Question/Topic? 0
  • +

Replies To: int concanating with string C++

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6917
  • View blog
  • Posts: 23,511
  • Joined: 05-May 12

Re: int concanating with string C++

Posted 23 May 2019 - 02:11 PM

Actually, you are not concatenating. You are getting back a pointer to a memory that is 54 bytes past the beginning of the string "hello", and then invoking undefined behavior by trying to access that memory that you don't own.

I think this will be more illustrative of what is happening:
#include <string>
#include <iostream>

using namespace std;

int main()
{
	const char * helloAddr = "hello";
	cout << static_cast<const void *>(helloAddr) << endl;

	const char * s = 6 + '0' + helloAddr;
	cout << static_cast<const void *>(s) << endl;

	int ascii = '0';
	cout << hex << ascii << endl;

	return 0;
}


Output from my run:
0135CB30
0135CB66
30



'0' has a hex value of 0x30 or otherwise 48 in decimal.
48 + 6 == 54 or 0x36.

So if "hello" lives at memory location 0x0135CB30, then:
0x0135CB30 + 0x36 == 0x0135CB66
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5732
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: int concanating with string C++

Posted 23 May 2019 - 02:11 PM

Quote

I'm wondering why if I want to concatenate integer with string as I should do like this in C++:

That is a good question, what makes you think you should do it that way? There are several different ways to accomplish this feat. Have you considered using stringstreams instead?


Quote

why should i add '0' and not using 6+"hello" immediately?

Have you ever consulted an ASCII chart?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-February 19

Re: int concanating with string C++

Posted 23 May 2019 - 03:05 PM

Yeah, but I didn't understand how is that going.
I understand that adding '0' , will let the PC go to asci chart and increase from 0 offset to the number that we add (in my case it's 6) but after increasing to number 6 from offset zero in asci chart, what's going? asci chart have value of integers corresponded to chars .. so how is that connected to my case?! I mean I go to number 6 in asci chart, so how he's getting me string "6" ? asci chart is something corresponded to integer as chars .. so it should print the char of number 6 and not "6" ..but actually it prints out "6" and not its char as found in asci chart

This post has been edited by Skydiver: 23 May 2019 - 03:25 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5732
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: int concanating with string C++

Posted 23 May 2019 - 03:14 PM

You don't seem to realize that your snippet is badly formed.

You really need to show an actual program that illustrates your question. And be careful about implicit conversions.

Quote

why should i add '0' and not using 6+"hello" immediately?

Well first off you can't add an integer to a string.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2346
  • View blog
  • Posts: 4,461
  • Joined: 30-May 10

Re: int concanating with string C++

Posted 24 May 2019 - 02:32 AM

Same serial helplessness going on here.
https://cboard.cprog...ger-string.html
Was This Post Helpful? 2
  • +
  • -

#7 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-February 19

Re: int concanating with string C++

Posted 27 May 2019 - 03:21 PM

View PostSkydiver, on 23 May 2019 - 09:11 PM, said:

Actually, you are not concatenating. You are getting back a pointer to a memory that is 54 bytes past the beginning of the string "hello", and then invoking undefined behavior by trying to access that memory that you don't own.

I think this will be more illustrative of what is happening:
#include <string>
#include <iostream>

using namespace std;

int main()
{
	const char * helloAddr = "hello";
	cout << static_cast<const void *>(helloAddr) << endl;

	const char * s = 6 + '0' + helloAddr;
	cout << static_cast<const void *>(s) << endl;

	int ascii = '0';
	cout << hex << ascii << endl;

	return 0;
}


Output from my run:
0135CB30
0135CB66
30



'0' has a hex value of 0x30 or otherwise 48 in decimal.
48 + 6 == 54 or 0x36.

So if "hello" lives at memory location 0x0135CB30, then:
0x0135CB30 + 0x36 == 0x0135CB66


Hi Skydiver, thanks for your answer ! so from your explanation I could conclude that ASCII TABLE'S ADDRESSES already reserved in memory by OS on the computer? I mean ASCII TABLE values is already loaded in specific memory on the PC ..and then we can use/manipulate over it? for your example you're telling that '0' having a specific address in ascii table, this means ascii table's values are already loaded in memory in specific places? thanks alot
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15066
  • View blog
  • Posts: 60,160
  • Joined: 12-June 08

Re: int concanating with string C++

Posted 27 May 2019 - 06:19 PM

ASCII tables are not stored anywhere. The decode _is_.

Quote

and then we can use/manipulate over it?

No, not really.
Follow up - what are you angling for?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1