Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 19, 2024, 07:55:23 19:55


Login with username, password and session length


Pages: [1]
Print
Author Topic: using arrays in both main.c file and other.c files for same project  (Read 2999 times)
0 Members and 1 Guest are viewing this topic.
bugmike25
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 15
-Receive: 13


« on: May 16, 2008, 01:54:42 13:54 »

Hi all. I'm having a problem with compiling my program because i want to use the same array inboth my main c file and my other c file.

Example: main.c

#include <absacc.h>
#include <stdio.h>
#include <SPI.h>
#include <DS1305.h>
#include <SDcard.h>

unsigned char rdat[12]; //To be used in main.c and SDcard.c

However if i declare it in both i get an error: Multiple public definitions
and if i declare it in only 1 of the two the compiler thinks that the array is undefined in one of them.

Please help me out. Can i solve this problem by defining the array in the SDcard.h file instead or any other solutions??/
Logged
zorx
Active Member
***
Offline Offline

Posts: 120

Thank You
-Given: 48
-Receive: 163


« Reply #1 on: May 16, 2008, 02:58:17 14:58 »

You have to use extern , like this:

main.c
-------

#include <absacc.h>
#include <stdio.h>
#include <SPI.h>
#include <DS1305.h>
#include <SDcard.h>

unsigned char rdat[12];
.
.
----------------------

SDcard.c
-----------
.
.
extern unsigned char rdat[12]
.
.
.
---------------------------

In SDcard.c this means that rdat[12] is externaly defined (in this case in main.c) and can be used across files.

Hope that this works.

Regards

Logged
bugmike25
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 15
-Receive: 13


« Reply #2 on: May 16, 2008, 06:11:18 18:11 »

Thanks it worked. Didn't know it was that simple.
Logged
YellowTiger
Guest
« Reply #3 on: May 19, 2008, 05:58:37 17:58 »

You have to use extern , like this:

extern unsigned char rdat[12]

There's the way to declare all the related vars just once:

"MySDLib.h"
-------------------------
// here follows block of SD-related variables declarations:
#ifndef  _SD_Card_Lib_
  #define  _SD_DefArea_ extern
#endif
_SD_DefArea_   int   iVar1;      // all SD-related variables are declared just once, all in one place - this will ease the control over the code and variables
_SD_DefArea_   char   cVar2;      // ...
_SD_DefArea_   unsigned char rdat[12];   //
...
#undef  _SD_DefArea_
...



"SDLib.c"
-------------------------
#define  _SD_Card_Lib_   // this will turn extern OFF
#include <MySDLib.h>   // all SD-vars will be declared as usual

// SDLib functions follows...
...



"main.c"
-------------------------
// #define  _SD_Card_Lib_   // omitting this #define will turn extern ON

#include <MySDLib.h>   // all SD-vars will be declared as external
...
Logged
LabBoy
Guest
« Reply #4 on: May 23, 2008, 12:43:28 12:43 »

Hi

yor can share global variable(s) in other files by using "extern"

for example

//-------[file A.C]--------
char arr[10];           

.....
.....
.....

//-------[file B.C]--------
// if you want to access arr in file A.C, you can  declar in the top of file B.C

extern char arr[10];

.....
.....
.....




Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC