Error in Function that use and return Array

N

nick048

Hi to all

I have in my program 2 arrays DIM 2:
firstArray[5] and secondArray[5]
fisrtArray is for example 'ABCD' plus terminator '\0'.

I need to create a function in order to perform this in my program:

secondArray = createArray(firstArray);

I have. with my poor experience, create a function like this:

char *createArray(char *originalArray)
{
char *tmpArray;
// Allocation
tmpArray = new char[ADIM]; // ADIM is definied as 5
//for(int i=0; i<ADIM; i++)
tmpArray = new char[ADIM];
// original must be the fistArray
tmpArray[0] = originalArray[2];
tmpArray[1] = originalArray[0];
tmpArray[2] = originalArray[3];
tmpArray[3] = originalArray[1];
tmpArray[4] = '\0';
return tmpArray;
}

But this function don't run and I have fron VS C++ the errors:
error C2440: '=': impossible to converte from 'char' to 'char *'

Can someone help me ? I am a newby in C++.

Thank You
Nino
 
D

Daniel T.

"nick048 said:
Hi to all

I have in my program 2 arrays DIM 2:
firstArray[5] and secondArray[5]
fisrtArray is for example 'ABCD' plus terminator '\0'.

I need to create a function in order to perform this in my program:

secondArray = createArray(firstArray);

I have. with my poor experience, create a function like this:

char *createArray(char *originalArray)
{
char *tmpArray;
// Allocation
tmpArray = new char[ADIM]; // ADIM is definied as 5
//for(int i=0; i<ADIM; i++)
tmpArray = new char[ADIM];

remove the above line, it is causing memory to leak.
// original must be the fistArray
tmpArray[0] = originalArray[2];
tmpArray[1] = originalArray[0];
tmpArray[2] = originalArray[3];
tmpArray[3] = originalArray[1];
tmpArray[4] = '\0';
return tmpArray;
}

But this function don't run and I have fron VS C++ the errors:
error C2440: '=': impossible to converte from 'char' to 'char *'

Can someone help me ? I am a newby in C++.

The above function as written compiles and does what you are asking for.
Based on your comments before the code, your main must look something
like this:

int main()
{
char firstArray[5] = "ABCD";
char secondArray[5];
secondArray = createArray( firstArray ); // error here
}

The reason you are getting an error is that secondArray is already an
array, it doesn't make sense to assign a newly created array to it.

There are two ways you can fix this:

int main()
{
char firstArray[5] = "ABCD";
char* secondArray = 0;
secondArray = createArray( firstArray ); // error here
// do whatever
delete [] secondArray;
}

A better way to fix it would be to change your function to copy an array
rather than creating one:

char* copyArray( char* orig, char* copy )
{
for ( int i = 0; i < ADIM; ++i )
copy = orig;
return copy;
}

Then use it in main like this:

int main()
{
char firstArray[5] = "ABCD";
char secondArray[5];
copyArray( firstArray, secondArray );
}
 
V

Victor Bazarov

nick048 said:
I have in my program 2 arrays DIM 2:
firstArray[5] and secondArray[5]
fisrtArray is for example 'ABCD' plus terminator '\0'.

I need to create a function in order to perform this in my program:

secondArray = createArray(firstArray);

If 'secondArray' is declared as a true array, you can't do that. It
does not matter what your 'createArray' function returns. Even if
it were possible to return an array from a function (it isn't), you
still have a problem that arrays are *not assignable*.
I have. with my poor experience, create a function like this:

char *createArray(char *originalArray)
{
char *tmpArray;
// Allocation
tmpArray = new char[ADIM]; // ADIM is definied as 5
//for(int i=0; i<ADIM; i++)
tmpArray = new char[ADIM];
// original must be the fistArray
tmpArray[0] = originalArray[2];
tmpArray[1] = originalArray[0];
tmpArray[2] = originalArray[3];
tmpArray[3] = originalArray[1];
tmpArray[4] = '\0';
return tmpArray;
}

But this function don't run and I have fron VS C++ the errors:
error C2440: '=': impossible to converte from 'char' to 'char *'

Can someone help me ? I am a newby in C++.

No, we can't help you. You cannot assign to arrays. The requirement
you have stated is *unsatisfiable*. You cannot write a function that
would allow you to do

secondArray = createArray(firstArray);

if 'secondArray' *is* in fact an array.

V
 
H

Howard

Victor Bazarov said:
nick048 said:
I have in my program 2 arrays DIM 2:
firstArray[5] and secondArray[5]
fisrtArray is for example 'ABCD' plus terminator '\0'.

I need to create a function in order to perform this in my program:

secondArray = createArray(firstArray);
Can someone help me ? I am a newby in C++.

No, we can't help you.

Well, we can, with a little more effort. :)
You cannot assign to arrays. The requirement
you have stated is *unsatisfiable*. You cannot write a function that
would allow you to do

secondArray = createArray(firstArray);

if 'secondArray' *is* in fact an array.

Just to follow up on that...

IF secondArray is defined as char* (instead of as an array), then the
assignment IS possible, and does what you want. (But remember: because the
function performed a new[], it's your responsibility to call delete[] on
that pointer once you're done with it.)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top