View previous topic :: View next topic |
Author |
Message |
micro
Joined: 10 Sep 2003 Posts: 13
|
Pointer in functions problem... |
Posted: Wed Oct 06, 2004 3:04 am |
|
|
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);
.
.
.
.
} |
|
![](templates/subSilver/images/spacer.gif) |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Wed Oct 06, 2004 3:37 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Oct 06, 2004 4:16 am |
|
|
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..? |
|
![](templates/subSilver/images/spacer.gif) |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Wed Oct 06, 2004 4:19 am |
|
|
Try to cal the function this way:
WriteIntoBuff(tx_count,s[0]);
in other words no '&', as you already pass the pointer. _________________ Alex |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Oct 06, 2004 4:38 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Wed Oct 06, 2004 4:43 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Oct 06, 2004 7:47 am |
|
|
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);
|
|
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Oct 06, 2004 8:45 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Wed Oct 06, 2004 8:47 am |
|
|
Sorry, I meant no '&' when passing just the name of the array _________________ Alex |
|
![](templates/subSilver/images/spacer.gif) |
JPA Guest
|
Pointers and array |
Posted: Wed Oct 06, 2004 9:25 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Oct 06, 2004 9:37 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Oct 06, 2004 9:41 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Oct 06, 2004 9:44 am |
|
|
yup,.. bad array example. (fixed) |
|
![](templates/subSilver/images/spacer.gif) |
|