strcat syntax with int and char

2

2005

Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.
 
G

Gianni Mariani

2005 said:
Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.


This is what std::string is good at.

The construct:
char String[] = "Time";

says to the compiler - "Allocate a char array just big enough to fit the
characters { 'T', 'i', 'm' , 'e', '\0' }". In fact, the construct below:

char String[] = { 'T', 'i', 'm' , 'e', '\0' };

means exactly the same thing as your construct above.

Which also means this:

char String[5] = { 'T', 'i', 'm' , 'e', '\0' };

because the compiler automatically fills in the size by counting the
number of initializers.

You can't add more storage to statically defined arrays, you need to use
dynamically defined arrays (like std::string and std::vector).

Also "char temp[]" does not do what you think it does, nor does
temp = (char) slot;


The easiest way to do this is:


#include <string>
#include <sstream>

// conversion to string helper
template <typename w_type>
inline std::string ToString(
const w_type & i_value
) {

std::eek:stringstream l_stream;

l_stream << i_value;

return l_stream.str();
}

const std::string String( "Time" );

int slot = 1;

std::string Time1( String + ToString( slot ) );

Time1 should contain "Time1".
 
B

BobR

2005 wrote in message
Hi
I have

char String[] = "Time";
char temp[];
int slot;
slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.

{
char String[] = "Time";
char temp[ std::numeric_limits<unsigned>::max() ];
int slot(1);
std::string name( String );
std::eek:stringstream sos;
sos << slot;
name += sos.str();

strncpy( temp, name.c_str(), name.size() );
temp[ name.size() ] = '\0';

std::cout<< temp << std::endl;
}

// output: Time1
 
D

Daniel T.

2005 said:
Hi

I have

char String[] = "Time";
char temp[];
int slot;

slot = 1;

What I need is:= String[] = "Time1"

temp = (char)slot
strcat(String, temp);

Something is a miss - could someone fix it for me pls.

In C++ you would do it this way:

int slot = 1;
stringstream ss;
ss << "Time" << slot;
string s = ss.str();
 
F

Frederick Gotham

BobR:
char temp[ std::numeric_limits<unsigned>::max() ];


Variable Length Arrays are non-standard, so the array length must be a
compile-time constant.

Anywho, the handiest way to get the max value for an unsigned integer type is
to convert -1 to it:

char temp[(unsigned)-1];
 
R

Ron Natalie

Frederick said:
BobR:
char temp[ std::numeric_limits<unsigned>::max() ];


Variable Length Arrays are non-standard, so the array length must be a
compile-time constant.

Anywho, the handiest way to get the max value for an unsigned integer type is
to convert -1 to it:

char temp[(unsigned)-1];

Of course, you're unlikely to be able to actually allocate an array that
size anyway.
 
B

BobR

Frederick Gotham wrote in message ...
BobR:
char temp[ std::numeric_limits<unsigned>::max() ];

Variable Length Arrays are non-standard, so the array length must be a
compile-time constant.

Anywho, the handiest way to get the max value for an unsigned integer
type is to convert -1 to it:

char temp[(unsigned)-1];

It was a joke, son! Yer sposed ta come back wif a:

ROFLMAO

( ...but your advice is, as always, good. )


// ....did compile for me on MinGW(GCC 3.3.1)
char temp[ std::numeric_limits<unsigned>::max() ]; // <int> crashes
std::cout<<" sizeof temp ="<< sizeof temp << std::endl;
// out: sizeof temp =4294967295

// --- bigger than a string!!! <G> ---
std::string name( String );
std::cout<<" string name.max_size() ="<< name.max_size() << std::endl;
// out: string name.max_size() =1073741820

Weird!
char temp[ 2147483647 ]; // == std::numeric_limits<int>::max()
..... kicks out with kernel32.dll error! (win98)
// char temp[ 2097151 ]; // bomb 0x1FFFFF
char temp[ 2031615 ]; // OK 0x1EFFFF

Must be some strange 'C' casting in that old kernel!
[.. or, a bug in MinGW. I'm not going hunting, I'll just upgrade if it
bothers me. ]
 
M

Marcus Kwok

BobR said:
Frederick Gotham wrote in message ...
BobR:
char temp[ std::numeric_limits<unsigned>::max() ];

Variable Length Arrays are non-standard, so the array length must be a
compile-time constant.

// ....did compile for me on MinGW(GCC 3.3.1)
char temp[ std::numeric_limits<unsigned>::max() ]; // <int> crashes
std::cout<<" sizeof temp ="<< sizeof temp << std::endl;
// out: sizeof temp =4294967295

IIRC VLAs are accepted as an extension in g++. I think if you compile
with options specifying standards-compliant mode then it should give you
a diagnostic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top