Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the function:
C++
#include <string.h>
#include <iostream>

#define cstr std::string
#define cout std::cout

char* data = "hello";

cstr CArrayToCStr(char* a) { //I HAVE TESTED THIS AND IT WORKS!
  cstr str(data);
  return str;
}

char* CStrToArray(cstr str) { //ALSO TESTED AND WORKS!
  char* arr = &str[0];
  return arr;
}

char* Insert(int pos, char* str) {
  char* ret;
  cstr _ret = CArrayToCStr(data);
  cstr insertion = CArrayToCStr(str);
  _ret.insert(pos, insertion);
  ret = CStrToArray(_ret);
  return ret;
}

int main(void) {
  cout << Insert(1, "eee");

What i obviously want to output is "heeeello", right? But does it output that? NOOOO it really doesn't. It gives me an uppercase P, an exponent 2, a lowercase a, it looks like a \n (newline), then maybe a \t or multiple spaces, then lowercase x and again an exponent 2. So basically:
P2a
x2
(with the numbers being small and upper, like an exponent or suberscript)

What I have tried:

I have tried running through a loop and inserting one letter at a time. Did not work either.
Posted
Updated 4-Dec-21 7:38am

You should avoid mixing C-types with C++ classes. There is no need for the conversion functions you have, just use a std::string from the beginning.
See string::insert - C++ Reference[^].

And the first header file should be
C++
#include <string> // not string.h
 
Share this answer
 
v4
What goes wrong in your program is your function Insert:
- You create a local variable of type std::string and in it compose a text string from the global char array and an insertion string.
- You determine a pointer to the internal character array used by the std::string object
- The function ends, which destroys all local variables, including the std::string object
- The function returns a pointer that now points to invalid memory
- your main program tries to print something that is declared to be a char array, but points to invalid memory. This causes undefined behaviour

You are fortunate your program didn't crash. Instead you get some random output.

All of this wouldn't be a problem if you would simply use std::string everywhere rather than needlessly converting back and forth to char array, which is a mistake anyway.

Other than that:
1. use #include <string>, not #include <string.h>
2. do not use #define. Instead use const definitions or using statements. In this case, the following would do the intended job just fine, and be much safer:
C++
#using cstr = std::string; // declares an alias
#using std::cout; // lets you use cout without having to specify std::

3. do not use global variables. Instead pass the constants or variables to the functions that use them (see below)
4. do not use char arrays or char pointers. E. g. your function Insert should be declared and then called like this:
C++
void Insert(std::string& targetString, size_t position, const std::string& insertion)
...
std::string text("hello");
Insert(text, 1, "eee");
cout << text;

Note how you can pass a literal string to the Insert function even though it expects a std::string. This conversion is done implicitely and you don't need a function for that!

On a sidenote, in the above example, you can replace the call to Insert simply by this one line:
C++
text.insert(1, "eee");
This would be much easier to read. There's no need to make it any more complex than that.
 
Share this answer
 
Comments
Member 14769677 4-Dec-21 14:52pm    
Yeah i'm converting the program to use the string instead of char array as data, and writing a new CStrToCArray function. Why the hell can't I do "const int _siz = str.size()" and "char* arr[_siz];" it nags me about using constant integers, well what the hell? I am. This worked on another C++ program I wrote last week.
Stefan_Lang 6-Dec-21 13:14pm    
If you have trouble understanding a specific concept ask or look it up e. g. on cppreference.com . You can find a lot of help and example code there.

As for statements like "this worked on another C++ program": that doesn't prove anything, except that, apparently, it's not the same program any more!
When you use the debugger you would see, what is going on in your code. Not only the mixing is bad style, but the use of the global var data is nonsense and is corrupting the memory and using non initialized memory. This is normal in C/C++ when doing such "!?'"§$%" stuff.

At least it should be a string not a char array, because of memory managment. I would write a class with a data member which has your functions.

Please visit some Learn C++ tutorial instead of guessing and waisting your time ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900