473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem assignin an int to a char * to a string

8 New Member
hello all i have a problem assigning an int to a char pointer and then to a string is there a way to do that i'm trying this:

string message;
int Af = 12;
char * field2 = new char [2];
field2 = (char *) Af;
message += field2;

When building it doesnt show an error but on debug it gives me:
First-chance exception at 0x00404150 in DataConverter.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x00404150 in DataConverter.exe: 0xC0000005: Access violation reading location 0x00000000.
The program '[5840] DataConverter.exe: Native' has exited with code 0 (0x0).

if i can directly assign the integer to the string it would be better.
Nov 1 '07 #1
2 1414
scruggsy
147 New Member
string message;
int Af = 12;
char * field2 = new char [2];
field2 = (char *) Af;
This code isn't doing what you think. It is trying to make field2 point to memory location 12.
You need to convert each of the digits of Af into their ASCII representations and store them in field2.
You could use itoa() to do that, although it's deprecated in C++. Writing your own function to do it isn't difficult.
Nov 1 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
hello all i have a problem assigning an int to a char pointer and then to a string is there a way to do that i'm trying this:
The larger question is why do you wnat to do this? These are incompatible data types. Is it your intent to write code that crashes?
Nov 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
10613
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
1
3041
by: Voronkov Konstantin | last post by:
Hello, All! I have following problem with std::string class. I exproted class from dll which has function which returns std::string. I compiled dll in *release* configuration (using...
18
6138
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
8
2535
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
2
2336
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the...
2
2510
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
2
4428
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
22
2488
by: Wilson | last post by:
i am learning to program using c++ and was set a task of making a simple encryption algorithim. I choose to start with one where simply each letter is replaced with its equivilent in the alphabet...
6
3512
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.