By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

What's wrong with this function

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
neo88 <so****@truevine.net> spoke thus:
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];
neo88


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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2

P: n/a
neo88 wrote:
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.

Jul 22 '05 #3

P: n/a
"neo88" <so****@truevine.net> wrote in message
news:6a*************************@posting.google.co m...

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[i]=ar[i]+br[i];

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

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


Ioannis Vranos

Jul 22 '05 #4

P: n/a
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];
}


On 14 Apr 2004 06:57:19 -0700, so****@truevine.net (neo88) wrote:
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


Jul 22 '05 #5

P: n/a
so****@truevine.net (neo88) wrote in message news:<6a*************************@posting.google.c om>...
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
Jul 22 '05 #6

P: n/a

"neo88" <so****@truevine.net> wrote in message
news:6a**************************@posting.google.c om...
so****@truevine.net (neo88) wrote in message

news:<6a*************************@posting.google.c om>...
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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.