The problem here is with
char Tmp[]="";
here, Tmp is not an dynamic array, it's an array with exactly one element (initialized via the initializer). When you use this as destination for strcpy(), you're overrunning the buffer. Basically, your code tries to access out of bound memory, which invokes undefined behavior.
Related, quoting C11, chapter §7.24.2.3, strcpy(), emphasis mine
The
strcpyfunction copies the string pointed to bys2(including the terminating null character) into the array pointed to bys1. [...]
and then, for "String function conventions", §7.24.1,
[....] If an array is accessed beyond the end of an object, the behavior is undefined.
Solution: You need to ensure that Tmp has enough memory to hold the content to be copied into it, including the null terminator.
That said, for a hosted environment, int main() should at least be int main(void) to be conforming to the standard.
The problem here is with
char Tmp[]="";
here, Tmp is not an dynamic array, it's an array with exactly one element (initialized via the initializer). When you use this as destination for strcpy(), you're overrunning the buffer. Basically, your code tries to access out of bound memory, which invokes undefined behavior.
Related, quoting C11, chapter §7.24.2.3, strcpy(), emphasis mine
The
strcpyfunction copies the string pointed to bys2(including the terminating null character) into the array pointed to bys1. [...]
and then, for "String function conventions", §7.24.1,
[....] If an array is accessed beyond the end of an object, the behavior is undefined.
Solution: You need to ensure that Tmp has enough memory to hold the content to be copied into it, including the null terminator.
That said, for a hosted environment, int main() should at least be int main(void) to be conforming to the standard.
Allocate memory to Tmp before copying the string to Tmp.
Tmp is an character array of size 1.
Allocate memory dynamically as shown below. (If you are trying to do dynamic memory allocatio)
char *Tmp = malloc(strlen(sNext1) + 1);
Then try doing strcpy()
c++ - Use Strcpy_s to Copy Dynamically Allocated Char Array - Stack Overflow
c - Strcpy a static char array into a dynamically allocated char array to save memory - Stack Overflow
Using strcpy with a string array in C - Stack Overflow
c++ - strcpy function with dynamic allocated char array - Stack Overflow
Videos
Praetorian hit it on the head. "The important rule you're missing is use std::string". Old C functions like strcpy_s are notoriously incredibly unreliable and that's the whole point of not doing it anymore. So don't do it. Use std::string.
The above code snippet result in "Debug Assertion failed" run time error.
strcpy_s(p, len, temp); //Expression:(L"Buffer is too small" &&0)
So the answer is strcpy_s(p, len+1, temp); will work fine in your case.
That's not a character array; that's an array of character pointers. Remove the * to make it a character array:
char c[20];
Then you can use strcpy:
strcpy(c, "undefined");
If you really did want an array of character arrays, you'll have to do what you said you tried:
// array that holds 20 arrays that can hold up to 70 chars each
char c[20][70];
// copy "undefined" into the third element
strcpy(c[2], "undefined");
The problem could have been you're missing the closing ", I don't know if that was a paste error though. Or, the problem could have been that you're using k without defining it, we can't know without seeing the error message you get.
If you want to set them all to that string, then just loop over them:
char c[20][70];
int i;
for (i = 0; i < 20; ++i)
strcpy(c[i], "undefined");
If what you want is to have 20 strings of 70 chars each then your second option should work:
char c[20][70];
for (int k = 0; k < 20; k++)
strcpy(c[k], "undefined");
The char *c[20] definition is incorrect because you are just defining an array of 20 pointers, and the pointers are not initialized. You could make it work in this way:
char *c[20];
for (int k = 0; k < 20; k++) {
c[k] = malloc(70);
strcpy(c[k], "undefined");
}
// then when you are done with these strings
for (int k = 0; k < 20; k++)
free(c[k]);
You haven't actually allocated memory for the pointer in data[current] leading to undefined behavior (and one of the possible outcomes of UB is that sometimes it seems to work fine).
Either allocate memory and assign it to the pointer, or use std::string instead (which is my recommendation).
In modern C++ there's really little reason to use pointers at all these days, least of all for strings. Using pointers just creates problems like the one you have, and more. And using std::string also means you won't have the risk of a buffer overflow if the user inputs a string longer than 19 characters.
You have allocated array of char pointers. You need to allocate memory for each of your strings, something like this:
char * dynamicTemp = new char [ 20 ];
std::cin >> dynamicTemp;
data[ current++ ] = new char [ 20 ];
strcpy( data[ current ], dynamicTemp );
There are a host of generally innocuous warnings that valgrind will throw from stdlib functions since they "cheat" a little. But this is not the case here:
char* c = new char; // this is bad
only allocates a char - not a buffer of chars, try:
char* c = new char[s->size()+1];
and then change the delete to:
delete [] c;
char* c = new char; The size of c is 1 and to copy even 1 character string you need two characters long buffer (2nd character to hold the null terminator)
There are some problems with your code
First it declares an array of integer pointers (hence the warnings) then those pointers aren't initialized. strncpy isn't the proper function to call (and even if the memory was initialized, it wouldn't null-terminate your strings), you need strdup which does the proper allocation & copy:
char *new_array[2];
for (int i = 0; i < 2; i++){
new_array[i] = strdup(array[i]);
}
(on some old systems, strdup may not be available. If you're in that case, just use new_array[i] = malloc(strlen(array[i]+1)); then strcpy(new_array[i],array[i]);)
Both methods allocate dynamic memory and need free when not used anymore.
But if you just want to store the pointers, for instance because array contains literals and you're not planning to modify your strings, you could just copy the pointer:
new_array[i] = array[i];
The source and destination are not compatible.
char *array[4];
int *new_array[2];
I hope you put int by mistake?