Hey Guys. Im having a hard time figuring this out. I realize there are a bunch of topics out there discussing this problem but I couldn't find anything that was helpful to this particular situation.

#include <iostream>
#include <string.h>

using namespace std;

typedef char charType[4];

int main()
	  {
		char a[2] = {'a','b'};
		charType MyChar;
		
		for(int i=0;i<3;i++)
		   {
			MyChar = a[i];//i want to set the value of MyChar to whatever a[i] has at that moment in the loop.
			strcpy(MyChar,a[i]); //this doesnt work either.
                      /*MyFunction(MyChar)<--- this function takes in MyChar suppose..*/
		   }
		

		return 0;
	  }

Its giving me the errors:
prog.cpp: In function ‘int main()’:
prog.cpp:15: error: incompatible types in assignment of ‘char’ to ‘char [4]’
prog.cpp:16: error: invalid conversion from ‘char’ to ‘const char*’
prog.cpp:16: error: initializing argument 2 of ‘char* strcpy(char*, const char*)’

My goal is to somehow copy the elements of the array to MyChar to use it later. Any suggestions would be much appreciated. Thanks.

MyChar is an array of char. a is a single char. The two types are completely incompatible. MyChar[i] = a[i] would work. At least until i gets incremented past 1, then you're accessing a out of bounds.

Nice that was quite helpful. Although the function that i mentioned above MyFunction(MyChar) takes in a char value. I cannot pass it a MyChar value because it complains for invalid conversion from char to *char. So i did MyFunction(&MyChar) and it compiles but it inputs malicious data...this is what it inputs:
a�y�ab
by�ab
�ab
....im not sure how to fix that. I need to somehow provide that MyFunction() with data thats in the a

Dude, your code is severely broken in several ways. I can only assume that MyFunction is equally crappy. Start by normalizing your expected/actual types, and fix your overflow errors. I'd help you more, but I have no idea what the hell you're trying to accomplish with this cluster of a program.

hahaha...that was funny...well actually this is related to a Btree program i am writing and this is one of the things i need to do. all my other functions work quite well. Is there no way that I can possibly give that MyFunction() function the data from a[]?....because that would pretty much solve all my problems.

Member Avatar for stevee1984
#include <iostream>
#include <string.h>

using namespace std;

typedef char charType; <-Changed

int main()
{
	char a[] = {'a','b'};
	
	for (int i=0;i<2;i++) <-Changed
	{
		charType MyChar = a[i];
	}
		

	return 0;
}

Try this dude.

I cannot change that because then I will have to change a lot of other stuff which is a big headache....any other solution?

Member Avatar for stevee1984
#include <iostream>
#include <string.h>

using namespace std;

typedef char charType[4];

int main()
{
	char a[] = {'a','b'};
	
	charType MyChar;
	for (int i=0;i<2;i++)
	{
		memcpy(MyChar,&a[i], 1);
	}
		
	return 0;
}

Use memcpy instead.

if you do a cout on the MyChar this is what it outputs:
��ab
��ab
...malicious data

Member Avatar for stevee1984

Thats because when MyChar is defined its not filled with nulls.

Try this then

#include <iostream>
#include <string.h>

using namespace std;

typedef char charType[4];

int main()
{
	char a[] = {'a','b'};
	
	charType MyChar;
	memset(MyChar, 0, 4); // Add this
	for (int i=0;i<2;i++)
	{
		memcpy(MyChar,&a[i], 1);
		cout << MyChar << "\n\r";
	}
		
	return 0;
}

Of course you could replace the whole for loop with:

memcpy(MyChar,&a,2);

nice you solved it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.