Reputation: 61
#include <iostream>
using namespace std;
void mystrcat(char destination[], const char source[]){
int counter = 0;
while(source[counter] != '/0'){
destination += source[counter];
counter++;
}
destination += '/0';
}
int main(){
}
For my code, when I try to concatenate with the function mystrcat, my school's test bed says that there was a segmentation error. The purpose of my function is to concatenate while removing the NULL from the end of destination and adding it to the end of source. Is there a segmentation error because I am not removing NULL? If so, how do I access the last element of the array? The number of elements is unknown so I don't know if I can use pop_back. Thank you.
Edit:
#include <iostream>
using namespace std;
void mystrcat(char destination[], const char source[]){
int counter = 0;
int counter2 = 0;
while(destination[counter2] != '/0'){
counter2++;
}
while(source[counter] != '/0'){
destination[counter2 - 1] = source[counter];
counter++;
}
destination[counter] = '/0';
}
int main(){
}
is my edited code but now the testbed says that it is taking too long and crashes.
Upvotes: 0
Views: 109
Reputation:
Arrays in C++ have an interesting property where they can easily decay to pointers.
destination += source[counter];
does not append source[counter]
to the end of destination.
Instead, destination
has decayed to a pointer and this operation is doing pointer arithmatic on destination
.
Instead, you want to do destination[destinationLocation] = source[counter];
to actually set the character in destination
.
Don't forget to set destination[end] = '\0';
at the end to null terminate the destination
array.
One final thing to watch out for is that C++ will not make sure that your arrays are properly sized. If destination
is not of the proper size, the code will fail at run time with a segmentation fault.
For future reference, you might want to look into using C++'s std::vector class. std::vector is a variable sized array-like container which automatically keeps track of its size and memory usage. Using pure arrays in C++ is sometimes difficult and error prone (as you have just seen), so std::vector can make things easier.
Upvotes: 1
Reputation: 5766
Static arrays are a fixed sized in C++. That means you can't extend or shrink them at all under any circumstance.
There are three main alternatives to achieve what you're trying to do. From your code, it looks like the best one would be to use the std::string
class (since you're using chars). It contains all the code to manage and concatenate the data very easily.
If you absolutely need an array, then std::vector
would be the next best choice. It allows push and pop operations etc., and will automatically resize the underlying dynamic array as needed.
Finally, you could use a dynamic array directly. You would create and destroy it using new []
and delete []
. When you need to extend the array, you need to create a new one with the new size, copy the old data over, and destroy the old one. It's a lot of unnecessary extra work though, so the other two options are much better.
Upvotes: 0