CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

help with array send as argument

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vic481



Joined: 02 Dec 2010
Posts: 2

View user's profile Send private message

help with array send as argument
PostPosted: Thu Dec 02, 2010 2:23 am     Reply with quote

Hi, all! I'm trying make pwm sound:
Code:

void snd(int8 how_many_times,int8 freq_val[5])
{
int8 i; 

for(i=0;i<how_many_times;i++)
  {
   setup_timer_2(T2_DIV_BY_4,freq_val[i],1);
   set_pwm1_duty(512);
   setup_ccp1(CCP_PWM);
   delay_ms(90);
  }

setup_ccp1(CCP_off);
}

If I call this procedure (example for one beep)
Code:

snd(1,(184,0,0,0,0));

only last argument (last 0) is sent to procedure (I see it in .lst file).
What I'm doing wrong?
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Thu Dec 02, 2010 5:01 am     Reply with quote

(184,0,0,0,0), is not an array. It is a mathematical statement, which evaluates each element in turn, and returns the last value in the list. So, '0'. Basic C....

In C, an 'array', is a pointer _to_ a block of values in memory. So you need to pre-declare the array, and hand it's address to the function.

So:

int8 vals[] = {184,0,0,0,0};

snd(1,vals);

Note multiple things:
1) the squirly brackets, not round brackets for the initialisation.
2) You are handing the 'address' of the list to the function, so: &vals[0], which is the address of the first element. However 'C' has a shortcut, which says if you use the 'name' of an array, without a subscript, to automatically assume you want the address. So &vals[0] gives the same address as vals on it's own.

Best Wishes
vic481



Joined: 02 Dec 2010
Posts: 2

View user's profile Send private message

PostPosted: Thu Dec 02, 2010 12:51 pm     Reply with quote

Tnx for reply and explanation.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group