This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Initialising Structures

Hello,

I am using the PK51 package. My program requires a structure of arrays. The following files are used:-
1) declare.c For defining the global variables
2) declare.h Containing the extern definations of all the variables declared in declare.h
This file will be used by all the c source files
3) main.c The main C source file
4) sub.c Containing some of the functions used by main.c

The contents of each of the files are as under:

file: declare.c

struct param {
float value[10];
char mode[10];
};
struct param a;
int i;

file: declare.h

struct param {
float value[10];
char mode[10];
};

extern struct param a;
extern int i;


file: main.c

#include <reg51.h>
#include "declare.h"

main()
{
i = 0;
initstruct();

}

file: sub.c

#include <reg51.h>
#include "declare.h"

void initstruct()
{
for (i=0;i<=9;i++)
{
a.value[i] = 2.5;
a.mode[i] = 1;
}
}

I have used two approaches to build the target files:
1) including the declare.h file in main.c and the sub.c file and compiling the main.c file and the sub.c file independently. Then linking the object files generated thereby.
2) Including the declare.c and sub.c file within main.c file and ignoring the declare.h file and then compiling and linking the main.c file independently.

I am facing two problems with the above approaches:
i) On using the above files and including all the .c files in the target build, the structure 'a' is not initialised properly. It is observed that the address calculations for the a.value and a.mode are not done properly.
If I include the declare.c and the sub.c file in main.c file and do not use the declare.h file, the structure is initialised properly. What am I doing wrong with the first approach?

3) On using the first approach, the UVision2 debugger works alright. On calling the initStruct() routine, the control shifts to the sub.c file. But on using the second approach, I am unable to single step in the sub.c file The control cannot be seen in the sub.c file. One can only see the control in the assembly listing. How can I see it in the sub.c file also?

Regards,

Mohit