473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error in Function that use and return Array

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(fir stArray);

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

char *createArray(ch ar *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

Nov 21 '06 #1
3 1725
In article <11************ *********@k70g2 000cwa.googlegr oups.com>,
"nick048" <ni************ *@moonsoft.itwr ote:
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(fir stArray);

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

char *createArray(ch ar *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[i] = orig[i];
return copy;
}

Then use it in main like this:

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

--
To send me email, put "sheltie" in the subject.
Nov 21 '06 #2
nick048 wrote:
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(fir stArray);
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(ch ar *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(fir stArray);

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '06 #3

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ej******** **@news.datemas .de...
nick048 wrote:
>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(fir stArray);
>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(fir stArray);

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

Nov 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3377
by: Kakaiya | last post by:
Error message: An unhandled exception of type 'System.InvalidCastException' occurred in test.dll Additional information: Specified cast is not valid. Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
3
317
by: Kakaiya | last post by:
Error message: An unhandled exception of type 'System.InvalidCastException' occurred in test.dll Additional information: Specified cast is not valid. Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
11
1758
by: Andrew Poelstra | last post by:
I hammered this out this morning to fix inconsistancies with the way my programs handle errors. The code itself is fine, in that it compiles with Richard Heathfield's gcc tags (plus -c because it doesn't have a main). Any comments? /* Start of header */ #ifndef _ERROR_H_ #define _ERROR_H_
4
242
by: MikeB | last post by:
I'm messing around with some sample code that I downloaded from a website (http://odnal.com/prosper-bid-sniper) that uses PHP objects and web services. When I upload it to my server, I get a weird error in a particular class. I'm posting 3 functions from that class. The error is Parse error: syntax error, unexpected '{' in...
2
1982
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on line 142 (line 12 in snippet) the error is caused by this call: empty($inSeite_inp->getValue('PAR_NAME')) method definition see second snippet,...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8190
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5697
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.