When I pass const char* as an argument in class constructor it changes its value.

I made a function which converts int to const char*. It works like this: https://ibb.co/6PxcP94 . Then, when i try to pass it to the class constructor it changes its value: https://ibb.co/RCH54kF , but when i try to pass it outside of the class functions it does not change its value.Also, i can't use string.h. Could someone please explain why is this happening?
Last edited on
Can we see your tocnstch() function?
i can't use string.h.

That header, <string.h> or <cstring>, is for C string functions, <string> is for C++ std::strings.
https://ibb.co/FbSXy3h . By mathematical calculations it gets every digit of the number passed to the function and in the append_chars() function it adds up the number in const char* data type from the int_cnst_chars array with cnst_ch variable. append_chars function is the implementation of strcat function, i took this implementation from here: https://stackoverflow.com/questions/2488563/strcat-implementation . And in the end i convert cnst_ch to const char*.
Don't just provide a link to somewhere unknown. Post the actual code here.

When posting code use code tags:


[code]
formatted code goes here
[/code]

Oh thanks. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
char* append_chars(char* dest, const char* src)
{
	char* rdest = dest;

	while (*dest)
		dest++;
	while (*dest++ = *src++)
		;
	return rdest;
}
static inline const char* tocnstch(int num_)
{
	const char* int_cnst_chars[10] =
	{
		"0",
		"1",
		"2",
		"3",
		"4",
		"5",
		"6",
		"7",
		"8",
		"9",
	};
	char cnst_ch[20] = "";
	int temp_i = num_;
	int temp_ch = 0;
	for (int i = get_quantity_of_digits(num_, false); i > 0; i--)
	{
		int ch_i = 0;
		if (i == get_quantity_of_digits(num_, false))
		{
			ch_i = int(floor(num_ / pow(10, i - 1)));
			temp_ch = ch_i;
		}
		//5914 -- 91
		else
		{
			float f = temp_i - (temp_ch * pow(10, (get_quantity_of_digits(num_, false) - (get_quantity_of_digits(num_, false) - i))));
			float g = pow(10, get_quantity_of_digits(f, false) - 1);
			ch_i = int(floor(f / g));
			temp_i = f;
			temp_ch = ch_i;
		}
		append_chars(cnst_ch, int_cnst_chars[ch_i]);
	}
	return (const char*)cnst_ch;
}

Explanation of the code is in the previous reply
Last edited on
Just because you return a const char* pointer, there is no guarantee that the memory buffer, which this pointer is pointing to, doesn't get modified elsewhere! So, how exactly was the memory buffer allocated that the const char* pointer, which is returned by your tocnstch() function, is pointing to?

If, for example, you are doing something like this:
1
2
3
4
5
6
const char *tocnstch(int i)
{
    static char buffer[BUFF_SIZE];
    /* ... */
    return buffer;
}

...then subsequent calls to the function are going to overwrite the buffer's contents!

And it would be even worse, if you were doing something like this:
1
2
3
4
5
6
const char *tocnstch(int i)
{
    char buffer[BUFF_SIZE];
    /* ... */
    return buffer;
}

...because you would be returning a "dangling" pointer to the caller!
Last edited on
L48 is a no-no. It's returning a pointer to the local object defined at L26. cnst_ch doesn't exist once the function returns and so the returned pointer is 'dangling' and not valid.

For this to work at all, then cnst_ch needs to be defined as static. However the data will be modified by every call to tocnstch() - so the caller will need to copy this data elsewhere after the call.

PS. Why write your own append_chars() - why not just use strcat() ??

Thanks kigar64551, seeplus and jonnin! I applied your suggestions and now it works perfect!
Last edited on
typically char is single quote: '1'
I don't recall what happens when you use "" which is really a C string / null terminated byte block/ thing as you did, but its probably better to not do that.

Topic archived. No new replies allowed.