using char[] within class through function..

I'm sorry, I'm really slow..
lets say I have a class "DAD" and I want to use the pointer to the DAD class, then I want to send another char array to the function to assign the word to a char array within the class.. How can I do that? I kind of simplified my code, to get to the meat of the thing I am trying to do.. does it make sense? I know I may have other syntax wrong as well, could anyone help me?

I want to be able to send a char array to the function, then assign that word to the char array that is in the class..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class DAD
{
public:
	char wordInClass[80];
	void assignWord(char[]);
private:
};

int main()
{
	DAD *words;
        words new DAD();

        char wordInMain[80] = "My name is John";
        words->assignWord(char wordInMain[]);
}

void getWord(char[])
{
        strcpy(wordInClass, wordInMain);
}
closed account (D80DSL3A)
Try this!

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
#include <iostream>// for use of cin, cout
using namespace std;

class DAD
{
public:
	char wordInClass[80];
	void assignWord(char* pch);
};

void DAD::assignWord(char* pch)
{
        strcpy_s(wordInClass, pch);
		return;
}

int main()
{		
	DAD *words;
        words = new DAD;

        char wordInMain[80] = "My name is John";
        words->assignWord( wordInMain );

	// test output
	cout << wordInMain << endl;
	cout << words->wordInClass << endl;

	delete words;// don't forget this

	system("PAUSE");
	return 0;
}


Or, do it using a constructor. If I add:
1
2
3
4
DAD::DAD(char* pch)
{
     strcpy_s(wordInClass, pch);		
}

Then I could replace lines 19 - 23 above with:
1
2
char wordInMain[80] = "My name is John";
DAD *words = new DAD(wordInMain);

This gets the string copied when the new DAD is allocated.
Last edited on
closed account (D80DSL3A)
Am I getting carried away? Sorry.
This solution allocates array space for the wordInClass dynamically on creation of a DAD object through a constructor, or through the function assignWord(). The dynamically allocated character array in the class is deleted with the DAD object using a custom destructor.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

class DAD
{
public:
	char* pwic;// pointer to word in class

	void assignWord(char* pch);
	DAD(char* pch = NULL);// overloaded. Pass a char* now or call copyWord() later
	~DAD();// destructor
};

void DAD::assignWord(char* pch)
{
	if( pwic != NULL )// delete previously allocated array
	{
		delete [] pwic;
		pwic = NULL;
	}
	// allocate for new string
	int n = 1 + strlen(pch);
	pwic = new char[ n ];
	strcpy_s(pwic, n*sizeof(char), pch);

	return;
}
// constructor - allocates char array and copies to it
DAD::DAD(char* pch)
{	
	if( pch != NULL )
	{
		int n = 1 + strlen(pch);
		pwic = new char[ n ];
	       strcpy_s(pwic, n*sizeof(char), pch);
	}
}
// destructor - deletes char array
DAD::~DAD()
{
	cout << "Bye-Bye!" << endl;
	if( pwic != NULL )
	        delete [] pwic;
}


int main()
{		
		
        char wordInMain[80] = "My name is John";
	DAD *words = new DAD(wordInMain);
        
	// test output
	cout << wordInMain << endl;
	cout << words->pwic << endl;// dynamic array

	// change the string in the class
	words->assignWord("New name is Joe");// a string literal will do as argument
	cout << words->pwic << endl;// to newly allocated array
	
	delete words;// destructor will delete pwic char string

	system("PAUSE");
	return 0;
}
Dude, fun2code! You Rock! I forgot about adding the '::' scope solution operator? thing.. anyways.. will be trying to finish my assignment later today.. (unlikely to finish today..) but I appreciate your help so much.!

Hi Fun2Code, So far, having a progress with your help! thanks so much!

Now that I've made some progress, looking to make it more efficient..
I initially was trying to pass the argument (agrv[1]) to the class, but had failed, so i just created a new char, strcpy 'ed it then sent it to the class.. anyway I can send the **argv ? how can i do that? a sample is below of what I have..

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char **argv)
{
	
	cout << "Received " << argc << " arguments...\n";
	for (int i=0; i < argc; i++)
	cout << "argument " << i << ": " << argv[i] << endl;

        char word[80];
	strcpy (word, argv[1]);
	cout << word << endl;
	words->assignWord (word);
}
Topic archived. No new replies allowed.