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 to struct

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



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

Pointer to struct
PostPosted: Mon Sep 12, 2011 9:48 am     Reply with quote

I have the following function to write to an external SPI EEPROM.

Code:
void FillBufferEE(long StartAddress, char *DataBuffer, char ByteCount)
{
    #warning "Esta funcion no controla desbordamientos de buffer; el buffer debe tener suficiente espacio para la cantidad de datos solicitados"
    char x;
    while(EE_Busy())
        {
            delay_us(10);
        }
        output_low(CS);
        spi_write(Read);
        spi_write(make8(StartAddress,1));
        spi_write(make8(StartAddress,0));
       
        for(x=0;x<ByteCount;x++)
        {
            DataBuffer[x]=spi_read(0);
        }   
        output_high(CS);
}   


And I think maybe I should declare it

Code:
void FillBufferEE(long StartAddress, char *DataBuffer[], char ByteCount)


I'm calling the function in many program points and is working OK but when I call the function and pass as a destination buffer a 32bit UInteger inside an struct did'nt work, It seems like doing nothing.

Code:
FillBufferEE(Timer1EE,Timer1.Setting,4);

_________________
Electric Blue
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: Pointer to struct
PostPosted: Mon Sep 12, 2011 10:16 am     Reply with quote

E_Blue wrote:
I have the following function to write to an external SPI EEPROM.

Code:
void FillBufferEE(long StartAddress, char *DataBuffer, char ByteCount)


Code:
FillBufferEE(Timer1EE,Timer1.Setting,4);


You need to pass the *address* of Timer1.Setting. You are passing its value, which will be interpreted as an pointer, with wrong results. You need the & operator:

Code:
FillBufferEE(Timer1EE,&Timer1.Setting,4);


If you wanted to fill an array you could write

Code:
FillBufferEE(Timer1EE,My_Array,4);


or


Code:
FillBufferEE(Timer1EE,&My_Array[0],4);


both should produce the same result. An array name without [] *is* a pointer in C. To pass a pointer to a specfic array element you need the & just as for a simple variable whether its in a struct or not.

RF Developer
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

Pointer to struct
PostPosted: Mon Sep 12, 2011 12:04 pm     Reply with quote

Thanks!, that works pretty well. Wink
_________________
Electric Blue
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