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

Pointer in functions problem...

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



Joined: 10 Sep 2003
Posts: 13

View user's profile Send private message

Pointer in functions problem...
PostPosted: Wed Oct 06, 2004 3:04 am     Reply with quote

Hi!,

I have some problems with using pointer in functions.
I have written a simple code snipped that use pointers but it does not work.

#include <18f458.h>
#include <string.h>
//#device *=16
#fuses H4,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT,NOLVP
#use delay(clock=40000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)



byte WriteIntoBuff(int16 Length, unsigned char *BuffAddr)
{

int16 count;

for(count=0;count<Length;count++)
{
Buff[PT_WrIndex]=*BuffAddr;
BuffAddr++;
}
}


void main(void)
{

int16 tx_count;
unsigned char s[100];
int16 adres;
adres=&s[0];
strcpy(s,"This is a sample text string for PIC18F458 UART0 Tx test");

txcount=strlen(s);

WriteIntoPumpTxBuff(tx_count,s);

.
.
.
.

}
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Wed Oct 06, 2004 3:37 am     Reply with quote

What exactly does not work?
Have you declare the function prototype as
byte WriteIntoBuff(int16 Length, unsigned char *BuffAddr) ;
Where do you actually call this function?
_________________
Alex
Guest








PostPosted: Wed Oct 06, 2004 4:16 am     Reply with quote

I have made a mistake in my question. Correct code is below

#include <18f458.h>
#include <string.h>
//#device *=16
#fuses H4,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT,NOLVP
#use delay(clock=40000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)



byte WriteIntoBuff(int16 Length, unsigned char *BuffAddr)
{

int16 count;

for(count=0;count<Length;count++)
{
Buff[PT_WrIndex]=*BuffAddr;
BuffAddr++;
}
}


void main(void)
{

int16 tx_count;
unsigned char s[100];
int16 adres;
adres=&s[0];
strcpy(s,"This is a sample text string for PIC18F458 UART0 Tx test");

txcount=strlen(s);

WriteIntoBuff(tx_count,&s[0]);

.
.
.
.

}

In the code, WriteIntoBuff does not work. It should be take "s" array and put it in to "Buff" array but it does not. What is wrong with it..?
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Wed Oct 06, 2004 4:19 am     Reply with quote

Try to cal the function this way:
WriteIntoBuff(tx_count,s[0]);
in other words no '&', as you already pass the pointer.
_________________
Alex
Guest








PostPosted: Wed Oct 06, 2004 4:38 am     Reply with quote

I have tried but it does not work again. Actually, a pointer variable requieres an adsress as a value. So it seems , & operator is required but it does not work. I am confused.
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Wed Oct 06, 2004 4:43 am     Reply with quote

you definitely do not need the '&'.
Try this:

WriteIntoBuff(tx_count,s);

byte WriteIntoBuff(int16 Length, unsigned char *BuffAddr)
{

int16 count;

for(count=0;count<Length;count++)
{
Buff[PT_WrIndex]=*BuffAddr[0];
}
}

If that does not work, declare this array as global and do not pass it.
_________________
Alex
Mark



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

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

PostPosted: Wed Oct 06, 2004 7:47 am     Reply with quote

Code:

Buff[PT_WrIndex]=*BuffAddr;


Umm... how about this
Code:

Buff[count]=*BuffAddr;


Quote:

WriteIntoBuff(tx_count,s[0]);
in other words no '&', as you already pass the pointer.


Please help only if you are sure of yourself. s[0] is the value of the array! You DEFINITELY need to pass the address.
Code:

WriteIntoBuff(tx_count,&s[0]);
WriteIntoBuff(tx_count,s);
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 06, 2004 8:45 am     Reply with quote

I always thought an array is the same as a pointer...

referance K&R second edition page 98 - 99.

I think passing the address of "&n[2]"
should work the same as n
when n is an array...n[5]


(if you don't know what K&R is...it shows I'm getting old)


Last edited by treitmey on Wed Oct 06, 2004 9:42 am; edited 2 times in total
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Wed Oct 06, 2004 8:47 am     Reply with quote

Sorry, I meant no '&' when passing just the name of the array
_________________
Alex
JPA
Guest







Pointers and array
PostPosted: Wed Oct 06, 2004 9:25 am     Reply with quote

treitmey wrote:
I always thought an array is the same as a pointer...

referance K&R second edition page 98 - 99.

I think passing the address of "&n"
should work the same as n[5]
when n is an array...n[5]


(if you don't know what K&R is...it shows I'm getting old)


When I was a student, in 1984, I have been told that (I think this have not change since then):

if n is an array,
n[5] is the same as *(n+5) so it is definitevely not an address.
n is the same as &n[0] and is the starting address of the array
&n[5] is the address of the 5th element, and is the same as n+5

Also note that:
if n is declared to be an array of int*16, n+5 is still the address of the 5th element because the compiler takes care of the type of n. So if n is an float (4 byte length), n+5 is still pointing to the 5th element.

Hope it will help you.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 06, 2004 9:37 am     Reply with quote

I think we are saying the same thing.
Yes the name is an address to ellement 0.
If you have a funtion that requires an address,... for example an address to an int8.

int8 tmp[3];

Then you can pass in the array name.

function(tmp)
or
function(&tmp[2])

This is how I use it.


Code:

int8 RX_BUF[260];
pkt.calcCS=chksum(&RX_BUF[rx_indx_o],pktsz-1);

int8 chksum(char *data,int8 size)
{
  int8 x;
  int8 chksum=0;
  for (x=0;x<size;x++,data++)
  {
    chksum=chksum+*data;
    }
  return(chksum);
}



Last edited by treitmey on Wed Oct 06, 2004 9:44 am; edited 1 time in total
Guest








PostPosted: Wed Oct 06, 2004 9:41 am     Reply with quote

treitmey wrote:
I think we are saying the same thing.
Yes the name is an address to ellement 0.
If you have a funtion that requires an address,... for example an address to an int8.

int8 tmp[3];

Then you can pass in the array name.

function(tmp)
or
function(&tmp[3])

This is how I use it.


Code:

int8 RX_BUF[260];
pkt.calcCS=chksum(&RX_BUF[rx_indx_o],pktsz-1);

int8 chksum(char *data,int8 size)
{
  int8 x;
  int8 chksum=0;
  for (x=0;x<size;x++,data++)
  {
    chksum=chksum+*data;
    }
  return(chksum);
}



I think that calling function(&tmp[3]) will pass a pointer to the 4th element of the tmp array; as this is a 3 element array (subscripts from 0 to 2) the function will receive a pointer to a "undefined" portion of memory.

Calling funcion (&tmp[0]) or function (tmp) is effectivly exactely the same thing.

Bye.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 06, 2004 9:44 am     Reply with quote

yup,.. bad array example. (fixed)
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