What's wrong with this function

N

neo88

hi guys
Can anyone please tell me what is wrong with this function:

inline int strength(void) {
int a;
// tests to see what strength value the agent just attacked\defended
with
for (strength[a]; a < 5; a++)
int strength[a] = int power[a] + int effect[a]; // array of
strength
// values added
return a; // this returns the value to the calling function
(getStrength)
}

The gcc gives me these errors:

pointer to a function used in arithmetic
parse error before '[' token

So what is going on here? What I am trying to do is add the values of
two arrays and put them in another on that can be accesed by a calling
function.

May the Source be with you.
neo88
 
C

Christopher Benson-Manica

neo88 said:
inline int strength(void) {
int a;
// tests to see what strength value the agent just attacked\defended
with
for (strength[a]; a < 5; a++)
int strength[a] = int power[a] + int effect[a]; // array of
^^^^^^^^
This identifier is already used - as the name of the function. Pick a
different name. Additionally, your syntax is wrong; the keyword "int"
is used only to declare variables. Try

strength[a] = power[a] + effect[a];

Apparently you're simulating "The Matrix". Please come with us,
Mister Anderson...
 
I

Ioannis Vranos

neo88 said:
So what is going on here? What I am trying to do is add the values of
two arrays and put them in another on that can be accesed by a calling
function.



Let's suppose you have two int arrays of size 10 ints.


// Somewhere
int ar[10]={1,2,3,4,5,6,7,8,9, 10}, br[10]={1,2,3,4,5,6,7,8,9, 10};


// ...


// Somewhere else

int result[10];

or

int *result=new int[10];

// ...

for(unsigned i=0; i<10; ++i)
result=ar+br;



// If you used operator new
delete[] result;



For the rest details you have to check your C++ manual.






Ioannis Vranos
 
R

Rolf Magnus

neo88 said:
hi guys
Can anyone please tell me what is wrong with this function:

inline int strength(void) {
int a;
// tests to see what strength value the agent just attacked\defended
with
for (strength[a]; a < 5; a++)

What is the above strength[a] supposed to mean?
int strength[a] = int power[a] + int effect[a]; // array of
strength

Local arrays must be defined with a constant size. Further, you cannot
assign to arrays. And where does 'power' and 'effect' come from?
// values added
return a; // this returns the value to the calling function
(getStrength)
}

The gcc gives me these errors:

pointer to a function used in arithmetic

That's because in your 'for' statement, 'strength' is only known as the
name of the function it's in. If you use this name without the function
calling syntax, you get a pointer to the function. strength[a] would be
the same as *(strength + a), but such pointer arithmetic isn't allowed
for function pointers.
parse error before '[' token

So what is going on here? What I am trying to do is add the values of
two arrays and put them in another on that can be accesed by a calling
function.

Where do you get those two arrays from, and how big are they? If their
sizes are not fixed (what's the 5 in your code for then?), you'd have
to dynamically allocate the array and later free it. You have to copy
the values in a loop from the two arrays to your target array.
You should consider using vectors instead of arrays to make your life
easier.
 
C

cedric buerfent

hi!

oups, a few things to check:

a) name of the function and name of your array is the same
b) you use your variable strengs[a] _before_ you have declared it
c) your "for" - condition checks the upper bound of "a" , but
"a" is not even initialized, and even incrementing "a" is
strange, because you initialise your loop with strength[a] which
is another variable. (like for(i ; a < 5; a++) )

try something like this (its only pseudocodic)

int strength_func()
{
int a = 0;

int strength[5];

for(a=0;a<5;a++)
{
strength[a] = power[a] + int effect[a]; // your power and
effect arrays are global? not so nice... better would be to pass them
through the function
}
return strength[a];
}
 
N

neo88

hi guys
Can anyone please tell me what is wrong with this function:

inline int strength(void) {
int a;
// tests to see what strength value the agent just attacked\defended
with
for (strength[a]; a < 5; a++)
int strength[a] = int power[a] + int effect[a]; // array of
strength
// values added
return a; // this returns the value to the calling function
(getStrength)
}

The gcc gives me these errors:

pointer to a function used in arithmetic
parse error before '[' token

So what is going on here? What I am trying to do is add the values of
two arrays and put them in another on that can be accesed by a calling
function.

May the Source be with you.
neo88

ok thanks, I'll try those out and see what happens. I have thought
about vectors, but since I have arrys I want to use them for this. Do
I have to explictly delete[] the arrays?
Thanks

May the Source be with you.
neo88
 
J

John Harrison

neo88 said:
(e-mail address removed) (neo88) wrote in message
hi guys
Can anyone please tell me what is wrong with this function:

inline int strength(void) {
int a;
// tests to see what strength value the agent just attacked\defended
with
for (strength[a]; a < 5; a++)
int strength[a] = int power[a] + int effect[a]; // array of
strength
// values added
return a; // this returns the value to the calling function
(getStrength)
}

The gcc gives me these errors:

pointer to a function used in arithmetic
parse error before '[' token

So what is going on here? What I am trying to do is add the values of
two arrays and put them in another on that can be accesed by a calling
function.

May the Source be with you.
neo88

ok thanks, I'll try those out and see what happens. I have thought
about vectors, but since I have arrys I want to use them for this. Do
I have to explictly delete[] the arrays?
Thanks

It's a simple rule. If you new[] them, then you have to delete[] them. If
you don't new[] then you don't have to delete[].

I don't understand your original code well enough to work out which
situation applies to you.

john
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top