|
From: Ioannis V. <iv...@at...> - 2003-04-11 19:46:32
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Andr? Macario Barros
> Sent: Friday, April 11, 2003 10:09 PM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] structs
>=20
>=20
> Please consider the following code:
>=20
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -
> 4 -struct car
> 5 -{
> 6 - int size;
> 7 - char name[10];
> 8 -};
> 9 -
> 10-int elevator(struct car);
> 11-
> 12-int main()
> 13-{
> 14- int floor;
> 15- struct car auto;
> 16-
> 17- printf("Enter car name-->");
> 18- gets(auto.name);
> 19- printf("Enter car size-->");
> 20- scanf("%d", &auto.size);
> 21- floor =3D elevator(auto);
> 22- printf("Please park your %s in the %d. floor\n",=20
> auto.name, floor);
> 23- system("PAUSE");
> 24- return 0;
> 25-}
> 26-
> 27-int elevator(struct car car_arg)
> 28-{
> 29- if(car_arg.size =3D=3D 1) return 1;
> 30- if(car_arg.size =3D=3D 2) return 2;
> 31- if(car_arg.size =3D=3D 3) return 3;
> 32-}
>=20
> 1-I know that I don=B4t need to pass all the struct. Only the=20
> size is enough;
> :-)))
> 2-I know I don=B4t need the int floor variable. I could=20
> directly call the
> elevator function as
> argument in line 22 printf; :-)))
> 3-I am calling the lines 4 until 8, as the "struct definition";
> 4-I am calling the line 15 and the argument function in line=20
> 27, as the
> "struct variable
> declarations";
>=20
> My Questions:
> A) My statement in 3 is correct?(y/n, comment)
Yes it is the definition of the struct type.
> B) My statement in 4 is correct?(y/n, comment)
These two are both definitions and declarations. The same way that a
function definition is also a declaration (for example you could have
defined the function at the place where you have the declaration
int elevator(struct car);
> C) Is there any way to rewrite this code to avoid a global "struct
> definition"? That is, how
> could I eliminate the lines 4 to 8, put them inside main and inside
> elevator functions?
You can't. However keep in mind that a type definition does not occupy
space. Its instances do. That is that only the "auto" object occupies =
space,
no the definition of struct car.
However the word "auto" is a reserved C keyword (it is the opposite of
keyword "static" for variables), so I do not think that the above would
compile.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|