For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > How to share a declared struc for other *.c use?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to share a declared struc for other *.c use?
boki

2005-06-09, 9:00 am

Hi All,

I have a Ball struct in main.c, and I have a myBall from Ball
//////////////main.c////////////
typedef struct {
int A;
int B;

} Ball;

Ball myBall;

main()
{
myBall.A=123;
myBall.B=456;
}
//////////////anotherC.c////////////


I want to use myBall (the same data content as it in main.c), what shoud I
do?


Thank you very much for your help.

Best regards,
Boki.


adebaene@club-internet.fr

2005-06-09, 9:00 am



boki a =E9crit :
> Hi All,
>
> I have a Ball struct in main.c, and I have a myBall from Ball
> //////////////main.c////////////
> typedef struct {
> int A;
> int B;
>
> } Ball;
>
> Ball myBall;

First of all, it's basically a very bad idea to have global variables,
even in C.

> main()
> {
> myBall.A=3D123;
> myBall.B=3D456;
> }
> //////////////anotherC.c////////////
>
>
> I want to use myBall (the same data content as it in main.c), what shoud I
> do?


First of all, the declaration of Ball type should be in an header,
included by both anotherC.c and main.c.

Then, to pass the instance to anotherC.c, the best approach it to pass
a pointer to it in the function call :

//in anotherC.c
void DoSomeStuff(Ball* pBall)
{
//...
}

//in main.c
int main()
{
//blabla
DoSomeStuff(&myBall); =20
}

Arnaud
MVP - VC

boki

2005-06-10, 4:04 am

Thanks a lot, before you tell me this method I did below yesterday.

int Access_Ball_A()
{
return myBall.A;
}

Best regards,
Boki.

<adebaene@club-internet.fr>
???????:1118311083.947124.273640@f14g2000cwb.googlegroups.com...


boki a écrit :
> Hi All,
>
> I have a Ball struct in main.c, and I have a myBall from Ball
> //////////////main.c////////////
> typedef struct {
> int A;
> int B;
>
> } Ball;
>
> Ball myBall;

First of all, it's basically a very bad idea to have global variables,
even in C.

> main()
> {
> myBall.A=123;
> myBall.B=456;
> }
> //////////////anotherC.c////////////
>
>
> I want to use myBall (the same data content as it in main.c), what shoud I
> do?


First of all, the declaration of Ball type should be in an header,
included by both anotherC.c and main.c.

Then, to pass the instance to anotherC.c, the best approach it to pass
a pointer to it in the function call :

//in anotherC.c
void DoSomeStuff(Ball* pBall)
{
//...
}

//in main.c
int main()
{
//blabla
DoSomeStuff(&myBall);
}

Arnaud
MVP - VC


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com