Solved: Array passed to function

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to pass an array to a function, where the array is modified in place.  I'm using AS7.  I keep getting the compiler error "called object 'Voltages' is not a function or function pointer", with the error cursor pointing to each 'Voltage(i)'

 

Here's my function and its caller:

void CalAnalog(){
OCR0A = 192;
	while(UDR0 != 'x'){
		ReadVoltages(Voltages);
		printf("Ch1=%2.2f,\tCh2=%2.2f,\tCh3=%2.2f\n\n", Voltages(0), Voltages(1), Voltages(2));
	_delay_ms(1000);
	}
}

float ReadVoltages(float *Voltages){
	Voltages(0) = RDiv*ReadADC(0, 16);
	Voltages(1) = RDiv*ReadADC(1, 16);
	Voltages(2) = RDiv*ReadADC(2, 16);
}

I declared earlier:

float Voltages[3];
float ReadVoltages(float Voltages);

Any idea what I'm doing wrong? thanks, paul

 

Last Edited: Mon. Mar 20, 2017 - 02:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which line does the error statement point to?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wrong type of brackets!
Array: Voltages[0]
Function: Voltages(0)

Solution - use square brackets for array.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The error points to the printf statement and all 3 Voltages(i) = 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

duh! Knew it had to be something stupid! thanks much, Kartman! paul

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Funnily enough, the same thing came up only the other day:

 

http://www.avrfreaks.net/comment...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

paulofelora wrote:
I declared earlier:

float ReadVoltages(float Voltages);

but then you defined it as:

paulofelora wrote:

float ReadVoltages(float *Voltages){

(which is the correct interface). Surely the compiler complained about this??

 

(that is the lack of '*' in the declaration).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that

float ReadVoltages(float *Voltages){

 

is equivalent to

float ReadVoltages( float Voltages[] ){

 

but the latter emphasises that the parameter is intended to be array of floats - rather than a pointer to a single float.

 

 

EDIT:

 

Just to clarify, as following replies have pointed out, that I am not saying that an array declaration is universally equivalent to a pointer declaration.

 

I am just talking about the specific case of a function formal parameter.

 

http://c-faq.com/aryptr/aryptrpa...

 

Last Edited: Mon. Mar 20, 2017 - 02:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
is equivalent to
Someone is going to be along in a minute to remind us that this is technically not true.

 

(though I know what you mean!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think it is true in this particular context (ie, as a function parameter) ?

 

http://c-faq.com/aryptr/aryptrpa...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

awneil wrote:
is equivalent to
Someone is going to be along in a minute to remind us that this is technically not true.

 

(though I know what you mean!)

An oldie but goodie.  Indeed, when learning, the tutor will say "an array is a pointer and a pointer is an array".  As one learns, "...except when it is not" is added.

 

For most vanilla work -- e.g. a character string array of char -- the axiom works out pretty well.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I didn't say that the array is equivalent to the pointer - I said that the two function signatures are equivalent.

 

I'll update my original post to be clear - just in case someone takes it out of context, and doesn't read the replies ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
I didn't say that the array is equivalent to the pointer

But I [almost] did.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.