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 CCS Technical Support

passing dat[4] to a subroutine

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



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

passing dat[4] to a subroutine
PostPosted: Tue Jul 12, 2005 9:37 am     Reply with quote

I have the following subroutine:
Code:

int Validate(int a, int b, int c, int d) {
  if (a == b) && (c == d)
     return 1;
  else
     return 0;
}

void main(void) {
  char dat[4];
  dat[0] = '1';
  dat[1] = '1';
  dat[2] = '2';
  dat[3] = '3';

  if (Validate(dat[0], dat[1], dat[2], dat[3]))
    output_high (PIN_A0);
}



How do I pass the pointer to the dat[4] to the function and use it inside the function rather than sending one char at a time as parameter to the function?

Is there a better way of doing this?

Thankx
valemike
Guest







PostPosted: Tue Jul 12, 2005 10:15 am     Reply with quote

Look how i pass in the pointer to the array. You simply call the array by name: Validate(dat)
And you can index into an array with a pointer to a char -- pChar.

pChar[2] is the same thing as writing *(pChar + 2)

Code:

#include <18F448.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#ignore_warnings 202
#case

int Validate(char *pChar);

int Validate(int *pChar)
{
  int x;

  for (x=0; x<4; x++)
  {
      printf ("%d: %c\r\n", x, pChar[x]);
  }
  return 0;
}

void main(void)
{
  char dat[4];
  dat[0] = '1';
  dat[1] = '1';
  dat[2] = '2';
  dat[3] = '3';

  Validate(dat);
  while (1);
}


The output of this program is:
0: 1
1: 2
2: 2
3: 3
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 12, 2005 10:19 am     Reply with quote

Try
Code:
int Validate(const char* dat) {
  if (dat[0] == dat[1]) && (dat[2] == dat[3])
     return 1;
  else
     return 0;
}

void main(void) {
  char dat[4];
  dat[0] = '1';
  dat[1] = '1';
  dat[2] = '2';
  dat[3] = '3';

  if (Validate(dat))
    output_high (PIN_A0);
}


I recommend to check the resulting size of this function as compared to your original version. The CCS compiler is very efficient when it knows the exact address of every variable. As soon as you start using pointers it is impossible for the compiler to calculate the addresses of the variables and it will generate more universal but larger code. You might be surprised to find out that your original version is larger in the editor than the second version, but smaller in the compiled version.

Edit: It's no fun to find out some other person answered the question while I was typing this message. Crying or Very sad


Last edited by ckielstra on Tue Jul 12, 2005 10:21 am; edited 1 time in total
Guest








PostPosted: Tue Jul 12, 2005 10:19 am     Reply with quote

valemike wrote:
Look how i pass in the pointer to the array. You simply call the array by name:


Sorry, I said it wrong. What I meant to say is that I pass in the ADDRESS of the array, or in other words, the address of the first element of the array.

Since pChar is a pointer to a char, then pChar now points to the first element in the array upon calling the Validate function.
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Tue Jul 12, 2005 10:37 am     Reply with quote

Quote:
It's no fun to find out some other person answered the question while I was typing this message.


I showed a demo program, and you showed exactly how the program was to be corrected. I'm sure both will help.

I can picture us all right now, when we're bored, we sit and keep refreshing the CCS forum on the browser wondering if a new post has popped up the last 3 minutes. (me, ckielstra, Ttelmah, and especially PCMPro & Mark) Laughing
jahan



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 12, 2005 11:55 am     Reply with quote

Is there any difference between:

Code:

char* dat

and
Code:

char *dat


and having the world "CONST" for the parameter.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 12, 2005 12:41 pm     Reply with quote

jahan wrote:
Is there any difference between:

Code:

char* dat

and
Code:

char *dat


no

Quote:

and having the world "CONST" for the parameter.

not allowed
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

valemike
PostPosted: Tue Jul 12, 2005 12:46 pm     Reply with quote

const char* dat

Isn't the above allowed as far as ANSII C goes? I think it declares dat to be a pointer to a char that is read-only, and thus the pointee's contents can't be changed.

Whether or not the CCS compiler would interpret it this way is a different story.
jahan



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 12, 2005 1:18 pm     Reply with quote

ckielstra wrote:
Code:
int Validate(const char* dat) {
  if (dat[0] == dat[1]) && (dat[2] == dat[3])
     return 1;
  else
     return 0;
}

void main(void) {
  char dat[4];
  dat[0] = '1';
  dat[1] = '1';
  dat[2] = '2';
  dat[3] = '3';

  if (Validate(dat))
    output_high (PIN_A0);
}




is above example mistyped with word CONST in parameter part?
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

valemike
PostPosted: Tue Jul 12, 2005 1:47 pm     Reply with quote

With the const, it compiles in Unix. However, it does not compile in CCS.

So delete the const, and you'll be okay.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 12, 2005 3:19 pm     Reply with quote

Sorry about the const keyword, it is indeed not valid for the CCS compiler to use it the way I did. I've been working too much in Delphi lately. C++ allows this construct as well. Embarassed

The const keyword is valid in CCS, but only to declare a constant like for example:
Code:
int8 const A = 3;
char const *B = 4;
int8 const C[3] = {1,2,3};

And yes, I checked these in v3.225. Smile

In C++ there are even more options:
Code:
const int* pX;        // A changeable pointer to a constant int.
int* const pY;        // constant pointer to changeable int
const int* const pZ;  // const pointer to const int

See also http://www.possibility.com/Cpp/const.html for a more in depth description.

Good thing about these constructions is that it allows the compiler to do some extra type checking and create better optimized code.
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