Jump to content
malikms

Newbie: Array As Argument Is Function

Recommended Posts

Can someone provide an example of a prototype using an array as an argument in a function, and an example function call?

 

I cannot come up with the right syntax for my file to compile. I have tried using a pointer syntax as an argument, the array variable name, and a reference to the array. I am obviously missing something.

 

In my reading I have seen that the compiler should translate the array argument into a pointer (if so defined). Is this the case with Boost?

 

Thank you in advance,

 

Mark

Share this post


Link to post
Share on other sites
Can someone provide an example of a prototype using an array as an argument in a function, and an example function call?

 

I cannot come up with the right syntax for my file to compile. I have tried using a pointer syntax as an argument, the array variable name, and a reference to the array. I am obviously missing something.

 

In my reading I have seen that the compiler should translate the array argument into a pointer (if so defined). Is this the case with Boost?

 

Thank you in advance,

 

Mark

 

bit adc_sign_digit;

signed long adc_deci (unsigned char n[])
{
signed long m;
m = 0;
m += n[0];
m += n[1] * 10;
m += n[2] * 100;
m += n[3] * 1000;
m += n[4] * 10000;
if(adc_sign_digit==0)  
{
 m = m*(-1);
} 
return m;  
}	  

void main()
{
unsigned char adc_bcd[5];
while(1)
 {
 if (portd.6 == 0)	 //if tare.
 {
tare_value = adc_deci(adc_bcd);
 } 

  }
}

 

Regards

Raghunathan

Edited by ra68gi

Share this post


Link to post
Share on other sites

Look at %ProgramFiles%\SourceBoost\Samples\C\BoostC\lcd.c

Lines 117-146 define LcdReadBack() and StrEqu(), used all over Main() at 173 onwards on two 21 element char arrays.

 

Edit: I see Raghunathan has already given you an example, but its always nice to have another one, especially one that comes with the compiler :-)

Edited by IanM

Share this post


Link to post
Share on other sites

Hi Mark,

 

Remember you are not passing the array but a pointer to it. So if your function modifies the array it is the original array that is being modified.

 

Your function could have also have been declared using a pointer argument.

signed long adc_decip (unsigned char *n)
{
signed long m;
m = 0;
m += *n++;
m += *n++ * 10;
m += *n++ * 100;
m += *n++ * 1000;
m += *n++ * 10000;
if(adc_sign_digit==0)  
{
 m = m*(-1);
} 
return m;  
}

 

BoostC does not let you specify the size of the array though in the function argument e.g. signed long adc_deci (unsigned char n[5]).

 

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Hi Mark,

 

Remember you are not passing the array but a pointer to it. So if your function modifies the array it is the original array that is being modified.

 

Your function could have also have been declared using a pointer argument.

signed long adc_decip (unsigned char *n)
{
signed long m;
m = 0;
m += *n++;
m += *n++ * 10;
m += *n++ * 100;
m += *n++ * 1000;
m += *n++ * 10000;
if(adc_sign_digit==0)  
{
 m = m*(-1);
} 
return m;  
}

 

BoostC does not let you specify the size of the array though in the function argument e.g. signed long adc_deci (unsigned char n[5]).

 

 

Cheers

 

Reynard

Hi,

There are some issues when u have your argument as pointer.

declare the pointer as char *n

as in the above case..

signed long adc_deci ( char *n)
{
signed long m;
m = 0;
m += *n++;
m += *n++ * 10;
m += *n++ * 100;
m += *n++ * 1000;
m += *n++ * 10000;
if(adc_sign_digit==0)  
{
 m = m*(-1);
} 
return m;  
}

I have a doubt here???

I think its ok to pass a string in the calling function & u need to check the null character as the terminator. Like the simple putc function

void putc(unsigned char tx_char)
{
while(SSR0_TDRE == 0);
TDR0 = tx_char;
} 

void puts(char *source)
{
while (*source != 0)   
	putc(*source++);
}
void main()
{
 while(1)
{
  puts("raghu");
 }

}

 

But if u have a pointer as the argument in a function & u have a array to be passed in the call function, I think u need to write the call function this way

  adc_deci(&adc_bcd);   // in the first example
 puts(&adc_bcd);		 //2nd example

 

Reynard, correct me if I am wrong.

Regards

Raghunathan.

Edited by ra68gi

Share this post


Link to post
Share on other sites

Passing arrays (vectors) do have their exceptions. As you cannot pass an array by value a reference (pointer) to the first element is therefore passed. You don't have to tell the compiler it is a reference as you have no other choice.

 

An argument of type T[] will be converted to a T* when passed as an argument. It's all in the manual somewhere folks.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Passing arrays (vectors) do have their exceptions.

Isn't a vector a very different animal from an array? An array is fixed in size when it's declared. A vector can grow and shrink dynamically and uses lots of overhead to do so. No?

 

Best,

Henry

Share this post


Link to post
Share on other sites

Hi Henry,

 

In 'C' I would consider array and vector to be the same thing.

 

In 'C++' you can have a vector class which can have a fixed size specified with the constructor and also be changable using a finctions of the vector class (set-size, add and delete).

 

As the posting was in the BoostC section of the forum I assumed the poster to be using 'C'.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...