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

Pointer use in function arguments

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








Pointer use in function arguments
PostPosted: Sun Jul 29, 2007 3:15 pm     Reply with quote

Hello,

Assume that I have the following type description :

Code:
typedef struct _NETWORK_DATA {
   int8 IP[4];
   int8 MAC[6];
   int8 GATEWAY[4];
   int8 MASK[4];   
} NETWORK_DATA;


And that I use a pointer to this type as an argument for a function. How should I use the pointer to modify the content of the received data ? In the following example, only the first assignation works. Is it the normal C behavior ? I thought that these two notations should be equivalent.

Code:
void NovramGetNetworkData ( NETWORK_DATA * DATA ) {
   DATA->MAC[0] = 101;
   (*DATA).MAC[1] = 102; 
}


Note.:
System : WinXP
PCWH V4.047

For info, here is the generated assembler :
Code:
....................    DATA->MAC[0] = 101;
*
00398:  MOVLW  04
0039A:  ADDWF  34,W
0039C:  MOVWF  FE9
0039E:  MOVLW  00
003A0:  ADDWFC 35,W
003A2:  MOVWF  FEA
003A4:  MOVLW  65
003A6:  MOVWF  FEF
....................    (*DATA).MAC[1] = 102;
003A8:  MOVLW  05
003AA:  MOVWF  FE9
003AC:  CLRF   FEA
003AE:  MOVLW  66
003B0:  MOVWF  FEF



Dimitri
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 3:59 pm     Reply with quote

I installed PCH vs. 4.047, compiled the test program shown below,
and ran it in MPLAB simulator (vs. 7.41). It worked fine.
Here's the output of the program.
Quote:
101, 102


Here's the ASM code for the two lines. It looks the same.
Code:

....  DATA->MAC[0] = 101; 
0004:  MOVLW  04
0006:  ADDWF  18,W
0008:  MOVWF  FE9
000A:  MOVLW  00
000C:  ADDWFC 19,W
000E:  MOVWF  FEA
0010:  MOVLW  65
0012:  MOVWF  FEF
....  (*DATA).MAC[1] = 102;   
0014:  MOVLW  05
0016:  ADDWF  18,W
0018:  MOVWF  FE9
001A:  MOVLW  00
001C:  ADDWFC 19,W
001E:  MOVWF  FEA
0020:  MOVLW  66
0022:  MOVWF  FEF
.................... 


Here's the test program. I used your code and just added
the necessary things to make a test program.
Code:

#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

typedef struct _NETWORK_DATA {
   int8 IP[4];
   int8 MAC[6];
   int8 GATEWAY[4];
   int8 MASK[4];   
} NETWORK_DATA;


void NovramGetNetworkData ( NETWORK_DATA * DATA )
{
 DATA->MAC[0] = 101;
 (*DATA).MAC[1] = 102; 

}

//=======================
void main()
{
NETWORK_DATA DATA;

NovramGetNetworkData(&DATA); 

printf("%u, %u \n\r", DATA.MAC[0], DATA.MAC[1]);

while(1);
}
Guest








PostPosted: Sun Jul 29, 2007 5:07 pm     Reply with quote

Thank you for the answer.

I made some more tests ( reducing my program until the code is correct ) and I found that the error comes from the fact that I used the magical option 'multiple compilation units' together with the 'link separately'

If I force all the sources into one file by including the '.c' files instead of the '.h' files and make a project without the multiple compilation units, the code is correctly generated.

I think I will report a bug on that point.

This is the test program to compile with the magical option :
Main file :
Code:
#include "global.h"
typedef struct _NETWORK_DATA {                               
   int8 IP[4];
   int8 MAC[6];
   int8 GATEWAY[4];
   int8 MASK[4];   
} NETWORK_DATA;
 
void NovramGetNetworkData ( NETWORK_DATA * DATA ) {
   (*DATA).MAC[0] = 105;
   DATA->MAC[0] = 105;


void main(void) {     
   NETWORK_DATA NET_DATA;
   NovramGetNetworkData ( &NET_DATA );
   while(TRUE) {
   }
}


Separate file (.h):
Code:
#ifndef GLOBAL__H
#define GLOBAL__H
   #include <18F4620.h>                                 
   #device *=16
   #fuses H4, NOWDT, NOLVP, NODEBUG
   #use delay(clock=40000000)       
   #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
     
   void InitUserIO ( void );
#endif 


Separate file (.c):
Code:
#include "global.h"
void InitUserIO ( void ) {
   *0xF92 = 0xDF;                      // PortA : A5 output, A4..A0 input   
   *0xF93 = *0xF93 & 0xCF;             // PortB : B5..B4 output     
   *0xF94 = *0xF94 & 0xF8;             // PortC : C2..C0 output
   *0xF95 = *0xF95 & 0xFB;             // PortD : D2 output
}
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