2

I am working on 2 different programs, but have the same problem for both. I have a char array (that doesn't hold meaningful text) that I use for drawing things on the screen. The first one is for a race line that shows progress, and the other one for the map of an area that can be modified.
What I want to do is overwrite a section of the array, not the entire array, without changing the array's size. For example, if array1 looks like |..........| and I wanted to add a fallen tree at position 3 and a rock at position 10 it would change to |..-->>>..@|. What I meant in my title for 'specific' is that the amount of characters to be replaced is given by me or determined during execution, instead of calling a function that automatically knows when to stop.
I thought of doing it with a for loop, like:

char tree[5] = "-->>>";
char map[12] = "|..........|";
int offset = 3; //position from which to start replacing characters
object_size = sizeof(tree) / sizeof(char); //to get the length of the object to be added
for (int i = 0; i < object_size; i++) {
    map[offset + i] = tree[i];
}  

Is there a cleaner way to do it? I'm sure there must already be a function that does it, but most of what I find is in other languages. The idea is to then be able to use a 2-dimensional array to store the map and modify bigger areas.

3
  • 1
    If your map string indeed is a regular C string, you can use a straight strncpy. This will copy the string in position, without the terminating zero. (Note that ../sizeof(char) is redundant, as sizeof(char) == 1 per the C specifications.) Commented Jan 5, 2017 at 17:16
  • 1
    char tree[5] = "-->>>"; oups, where is the place for the terminating null??? It should be char tree[6] = "-->>>"; or char tree[] = "-->>>"; to let the compiler count for you. Commented Jan 5, 2017 at 17:19
  • Can code assume inputs like array1, tree,map, offset are all well behaved together, or should code be resilient to troublesome cases like offset > strlen(map)? Without a stated model of error handling - if any, "a cleaner way to do it" is moot. Commented Jan 5, 2017 at 18:02

2 Answers 2

2

If I have correctly understood, you do not want to copy the terminating null. The standard library function to use is void *memcpy(void *dest, void *src, size_t size) that copies exactly size bytes from src to dest.

But you have to ensure that there is enough room at dest to accept the size bytes

Sign up to request clarification or add additional context in comments.

4 Comments

Detail: With OP's char tree[5] = "-->>>";, there is no null character to copy or not - nor is one truly needed here. Agree memcpy(map +offset, tree, sizeof tree) should meet OP's goal - which seems to not be concerned with error checking.
Thanks. I initialized the arrays incorrectly. Is there any difference between a character array with no '\0' at the end (because it was initialized like array[] = {'-', '-', '>', '>', '>'} and a character array initialized with a string, besides the terminating NULL character at the end? I guess if I store it like a string I can output the lines of the map using printf() instead of a for loop, but other than that they have the same qualities and are stored the same, right?
@FranciscoAyrolo: I'm unsure I have met your point. A C string is an array of characters terminated with a null character. And all C string functions knows about that. For me, the the only difference is that you cannot use a character array not terminated by a null as a C string, for example in printf, or any strxx function. Said differently any C string is a character array, but not all character arrays are C strings
Thanks for the follow up. I was unsure of whether I should store the maps and objects as strings or arrays, but now I see the clear advantage.
0

You can take a look at

https://www.tutorialspoint.com/c_standard_library/string_h.htm

And for you,

char *strcpy(char *dest, const char *src)

char *strncpy(char *dest, const char *src, size_t n)

They can make your code cleaner, but i think that deep inside, they do what you're doing manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.