Changing the value of a char array in another function

I'm having some problems with pointers. Help would be much appreciated.

I'm trying to change the value of a character array inside a function. My code is split into three files. It compiles but doesn't do what it is supposed to do. I'm kind of new to pointers but I'm pretty sure that they are causing the problem.

Here is my code:

extras.h:

1
2
3
4
5
# include <iostream>
# include <sstream>
# include <string>

void nameWithPaddedNumber(char _name[], int number, int digits, char* destination);


extras.cpp:

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
# include "extras.h"
using namespace std;

void nameWithPaddedNumber(char _name[], int number, int digits, char* destination) {
	std::cout << "destination = " << destination << std::endl;
	std::cout << "*destination = " << *destination << std::endl;
	std::cout << "&destination = " << &destination << std::endl;
	std::cout << "_name = " << _name << std::endl;
	string name(_name);
	std::cout << "name = " << name << std::endl;
	string padded;
	stringstream convert;
	convert << number;
	padded = convert.str();
	std::cout << "padded = " << padded << std::endl;
	int n = padded.length();
	std::cout << "n = " << n << std::endl;
	for (int i=n+1;i<=digits;i++) {
		padded = "0" + padded;
	}
	std::cout << "padded = " << padded << std::endl;
	padded = name + padded;
	std::cout << "padded = " << padded << std::endl;
	char* dest;
	dest = new char[padded.length()+1];
	dest[padded.length()]=0;
	memcpy(dest,padded.c_str(),padded.length());
	destination = dest;
	std::cout << "dest = " << dest << std::endl;
	std::cout << "*dest = " << *dest << std::endl;
	std::cout << "&dest = " << &dest << std::endl;
	std::cout << "destination = " << destination << std::endl;
	std::cout << "*destination = " << *destination << std::endl;
	std::cout << "&destination = " << &destination << std::endl;
}


main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
# include "extras.h"

int main(int argc, char* argv[])
{
	char padd [40];
	char * ptrToPadd = padd;
	std::cout << "ptrToPadd = " << (void*)ptrToPadd << std::endl;
	char name[] = "daria_walk_";
	std::cout << "padd = " << padd << std::endl;
	std::cout << "*padd = " << *padd << std::endl;
	std::cout << "&padd = " << &padd << std::endl;
	nameWithPaddedNumber(name, 14, 4, padd);
	std::cout << "padd = " << padd << std::endl;
	std::cout << "*padd = " << *padd << std::endl;
	std::cout << "&padd = " << &padd << std::endl;
	int i = 1;
	while (true) {
		i++;
	}
	return 0;
}


The output is:


ptrToPadd = 0025F9B4
padd = ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ç☺˨4·%
*padd = ╠
&padd = 0025F9B4
destination = ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ç☺˨4·%
*destination = ╠
&destination = 0025F8B4
_name = daria_walk_
name = daria_walk_
padded = 14
n = 2
padded = 0014
padded = daria_walk_0014
dest = daria_walk_0014
*dest = d
&dest = 0025F790
destination = daria_walk_0014
*destination = d
&destination = 0025F8B4
padd = ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ç☺˨4·%
*padd = ╠
&padd = 0025F9B4
You're assigning dest to the local variable destination, so that change will never leave the function.
You need a pointer to a pointer (char**) or far better yet, a reference or pointer to a real string (string&).
I can immediately see that padd was never initialized. Therefore the first time you send padd to cout the system will start printing whatever characters happen to be in memory until a 0 is found. Therefore I would expect to see a bunch of garbage being printed. What exactly are you having trouble with? What did you expect to happen?
kempofighter, I want padd to start off as garbage but then change inside the function. So I pass in a reference to padd. But after the function is called padd remains garbage instead of being changed to daria_walk_0014.

Athar, you may be right but I don't know what is supposed to be a pointer to a pointer or reference to a string.
1
2
3
4
dest = new char[padded.length()+1];
	dest[padded.length()]=0;
	memcpy(dest,padded.c_str(),padded.length());
	destination = dest;


Here is your other problem. You don't need to allocate memory for this new thing "dest". You should be copying directly into destination and you'd be fine. The problem is that destination was a copy of padd which started out with the same address as padd. Then you overwrite it with a pointer to the new memory. After returning from the function padd in main has its original value and still points to the same garbage. You don't need to do char**. You just need to write to the memory pointed to by destination in the first place. You've also created a memory leak by dynamically allocating memory without ever deleting it.

Also if you are going to use the char array use strcpy not memcpy. strcpy will copy the NULL for you and is specifically designed to deal with character arrays that are NULL terminated. Alternatively use std::string and pass the object by reference or by iterators to the start and end.
Thankyou very much. That's really helpful.
Topic archived. No new replies allowed.