473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a string or int to a character array

5 New Member
Hi,

I have a problem that's (probably) very easy to solve, only I'am more a Java programmer and I couldn't find anything like it on the internet.
My problem is that I have to add a variable (int, string, double) to a character array. I know that it is very easy with Java but I din't find any thing for C/C++.
Everything I found created errors.

Example:
I've added the first 4 lines of the methode "setOffset( int arg1)". "msg" is the char array created at the beginning of the methode it's importend that the size of this array is equal to the size of the hardcoded string + the length of arg1, because it is used in the "viWrite(.....) ".
The char array won't be changed after it's made.

Expand|Select|Wrap|Line Numbers
  1. bool Tektronix::setOffset(int arg1){
  2.     char msg[] = "source1:Voltage:Offset " + arg1; //this is the Java way
  3.     status = viWrite(vi, (ViBuf) msg , sizeof(msg), &retCnt);
  4.     if (status < VI_SUCCESS) return false;
  5.         .............
  6.  
ERROR: cannot convert from 'System::String ^' to 'char []'
ERROR: 'char []': illegal sizeof operand

Second problem is that it says illegal sizeof opperand, but I expect that error will disapear if I've solved the first.

Thanks for all the support

GreetZ

Spikey
Oct 2 '07 #1
6 5408
sicarie
4,677 Recognized Expert Moderator Specialist
I believe you will need string streams. That should help you convert to a string.
Oct 2 '07 #2
Spikey
5 New Member
Hi,

I guess it's not that easy... the strcpy I have to use (to convert the string to a char array) is creating a Warning. I can live with that but it's not pretty.
The real problem is that the sstream is creating an error in combination with my errorhandler.

ERROR: error C2362: initialization of 's2' is skipped by 'goto errorHandler'

GreetZ.
Oct 2 '07 #3
Spikey
5 New Member
Hi,

I have a few things working now, but I really need to have an array with the exact size of the string.
I was hoping the following code would do the trick, but it ain't

example:
Expand|Select|Wrap|Line Numbers
  1. string s1 = "output1?";  // s1.size = 8
  2. char ch1[s1.size()]; // this ain't gonne work, I know!
  3. strcpy (ch1 , s1.c_str());
  4.  
it's essential that afterwards sizeof(ch1) is equal to the number of characters inside, otherwise I can't use the viWrite methode (created by National Instuments).

every possible help is welcome :D

Greetz

Spikey
Oct 2 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
string s1 = "output1?"; // s1.size = 8
char ch1[s1.size()]; // this ain't gonne work, I know!
strcpy (ch1 , s1.c_str());
Did you read what Sicarie said in Post #2?? He gave you the answer.
This is C++ and not C of 1972. That is, you don't use the C-string library anymore.

Expand|Select|Wrap|Line Numbers
  1. //Java:
  2. char msg[] = "source1:Voltage:Offset " + arg1; //this is the Java way
  3. //C++:
  4. stringstream ss;
  5. ss << "source1:Voltage:Offset " << arg1; 
  6. string msg;
  7. ss >> msg;
  8.  
Oct 2 '07 #5
Spikey
5 New Member
Sorry to bother you, but I really NEED a char array like "char msg[]" instead of a string. The only reason I used a string was to add data to a text line, afterwards I really need to convert it to a char array.
The methode viWrite is a methode writen by National Instruments and I only can convert a char array to a ViBuf (see also code of post#1). ViBuf is defined as a char.

in the visatype.h (the source of National Instruments) is the following defined about ViBuf
Expand|Select|Wrap|Line Numbers
  1. typedef unsigned char       ViByte;
  2. typedef ViByte      _VI_PTR ViPByte;
  3. typedef ViPByte             ViBuf;
  4.  
So please if some body understand what I am trying to say (what i need) and know a way to solve the problem. please help, but read Post#1 and Post#4 really carefully before you reply and also come with a solution ending with a string!

GreetZ.
Oct 3 '07 #6
Spikey
5 New Member
I have found the solution. Sorry for every one I just pissed off.
I only had to use the c_str() methode of a string and I'am sure I did try it yesterday multiple times, but I gues I forgot the "()".

greetz

Spikey
Oct 3 '07 #7

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

Similar topics

0
2727
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful tool for breaking ties when a phonic call returns a bunch of possibilities. Also, I'm looking for someone that has a zip code file with alternate...
16
17379
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf(...
7
7063
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
14
12902
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
14
4048
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
6
2343
by: Paulers | last post by:
Hello, I have a string that I am trying to add each char to a datatable row. for example if I have a string that looks like "abcdefg", I would like to break it up into an array of characters so I can do this: myDataTable.Rows.Add(array()) instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
9
1730
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined behavior. char *returnValueFromIniFile(char *iniFilePath,char *keyIntheFile)
10
18613
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The...
19
5316
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given,...
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
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 we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.