Jump to content
  • Advertisement
Sign in to follow this  
prophete

how do i concatenate a string with an integer?

This topic is 4244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do i concatenate a string with an integer in C? i tried str += i; it didnt work, i also tried str = " " + i; didnt work either

Share this post


Link to post
Share on other sites
Advertisement
One possible way, there are much better and safer ways :)

Look at sprintf and snprintf


sprintf(buffer, "%s%d", myString, myInt);


So if myString is "Hello" and myInt is 5, buffer will now be "Hello5". Dangers of this inluding not having large enough space in buffer variable but you can calculate max size.

Share this post


Link to post
Share on other sites
In c, you can sprintf() the integer to the string, like so ( forgive me if this isnt perfect, I dont usually use c )



char *buffer = /*some c string */;
sprintf(buffer + strlen(buffer),"%d",i);



The strlen(buffer) is to make sure it adds it to the end of the string.

Share this post


Link to post
Share on other sites
i tried

char *toReturn[4];
sprintf(toReturn[3] + strlen(toReturn[3]), "%d", result);

it's giving me a runtime error..

Share this post


Link to post
Share on other sites
Quote:
Original post by prophete
i tried

char *toReturn[4];
sprintf(toReturn[3] + strlen(toReturn[3]), "%d", result);

it's giving me a runtime error..
You haven't allocated the buffer, so you'll get an access violation.

Try:

char toReturn[4][256];
strcpy(toReturn[3], "Some string");
sprintf(toReturn[3] + strlen(toReturn[3]), "%d", result);




Or, better, switch to C++ and use std::string and std::stringstream.


EDIT: Woooah, I hope you're not trying to return these pointers from a function, are you? If so, you'll need to use malloc() to allocate memory for them, since if they're on the stack they'll be destroyed when the function exits.

Share this post


Link to post
Share on other sites
And I hope "toReturn" doesnt mean that you are going to return that variable from a function, because returning arrays/c-strings won't work either.

Share this post


Link to post
Share on other sites
Quote:
Original post by prophete
yeah i wanted to return the array toReturn ..
thanks steve that worked, but what was the 256 for?


Evil Steves code creates an array of 4 256 character strings. Did you just want a 4 character long string?

If you were going to return an array, you should probably re-write it to something like this:


void appendInt( char *str, int val )
{
sprintf(str+ strlen(str),"%d",val);
}


and call it like so:


char str[100] = "The value of i is ";
appendInt(str,i);

Share this post


Link to post
Share on other sites
C strings are pointers to storage. They are not garbage collected or reference counted.

This means you have to allocate and deal with storage for every character you deal with.

As an exception, "static" strings in quotes are allocated by the compiler.

But any other string you build, you have to manage the lifetime of the memory, the amount of memory, and what character goes into what part of memory.

rip-off's posts will result in memory corruption.

....

I would advise against dealing with raw C strings. Try the following:


typedef struct Buffer {
char* start;
char* end;
} Buffer;

Buffer allocate_buffer( size_t n ) {
char* tmp = calloc( n, sizeof(char) );
Buffer result = {tmp, tmp+n};
return result;
}

void dispose_buffer( Buffer b ) {
free( b.begin );
}


The Buffer struct stores both the start pointer to a block of memory, and the size of the block of memory.

Using it, and some care, you can avoid corrupting memory. Because you actually know how large your memory allocations are. :)

And then you can write buffer-overrun-safe string modification functions:

// returns non-zero on success, 0 on error.
int append_string( Buffer b, char* str ) {
size_t n = strlen(str);
size_t b_n = strlen(b.begin);
if (b_n + n + 1 > (b.end-b.begin)) return 0; // error!
sprintf( b.begin+b_n, "%s", str );
};

int append_int(Buffer b, int i ) {
char tmp[100];
sprintf(tmp, "%d", i);
return append_string( b, tmp );
}


C is a very low level language. You are directly dealing with the stack, heap, and the processor. You should think of C as a fancy assembler rather than the kinds of languages you have delt with before.

Now, note that in C++, I would do it very differently.

Share this post


Link to post
Share on other sites
Quote:

rip-off's posts will result in memory corruption.


I'm sorry, but how? [smile]

Granted the function requires that the buffer has enough space to handle the extra characters being appended (which I should have checked [embarrass]), but once that has been met how would there be memory corruption?

I know if the string is not NUL terminated it could cause problems, but its a c-string, if they dont aren't NUL terminated you don't have much hope anyway.

I do agree with you BTW, but I really feel if you are willing to go to those lengths you would be much better off jumping to c++ anyway, unless there is some real restriction thats keeping you using c.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!