|
From: <and...@su...> - 2003-04-11 19:09:32
|
Please consider the following code:
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 = elevator(auto);
22- printf("Please park your %s in the %d. floor\n", auto.name, floor);
23- system("PAUSE");
24- return 0;
25-}
26-
27-int elevator(struct car car_arg)
28-{
29- if(car_arg.size == 1) return 1;
30- if(car_arg.size == 2) return 2;
31- if(car_arg.size == 3) return 3;
32-}
1-I know that I don´t need to pass all the struct. Only the size is enough;
:-)))
2-I know I don´t need the int floor variable. I could 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 27, as the
"struct variable
declarations";
My Questions:
A) My statement in 3 is correct?(y/n, comment)
B) My statement in 4 is correct?(y/n, comment)
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?
Thank you again
Andre
|
|
From: Noorez K. <coo...@ho...> - 2004-01-31 00:16:15
|
what is the difference between these two
typedef struct _Human
{
int height;
}Human;
struct Human
{
int height;
};
and in the first one, what does 'Human' mean at the end? In both structs u
can declare a Human type, even without the Human thing at the end in the
first one
_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca
|
|
From: <or...@vp...> - 2004-01-31 00:53:17
|
Noorez Kassam wrote:
> what is the difference between these two
>=20
> typedef struct _Human
> {
> int height;
> }Human;
>=20
> struct Human
> {
> int height;
> };
>=20
>=20
> and in the first one, what does 'Human' mean at the end? In both struct=
s=20
> u can declare a Human type, even without the Human thing at the end in=20
> the first one
These are the remnants of the C world. The difference is only noticable=20
in C. With the second version in the C world you had to write this to=20
declare a variable (a Human struct):
struct Human MyHuman;
However, with the first one (as Human is a new type in this case), you=20
could write this:
Human MyHuman;
So in the C world, you spared the struct prefix at variable declaration,=20
and saved yourself a lot of typing.
In the C++ world, this is no longer relevant, as struct automatically=20
defines a new type (like class).
--
Greetings,
Bal=E1zs
|
|
From: Maya <Esc...@ne...> - 2004-01-31 12:14:33
|
In the first one you have declared a struct named _Human and
instantiated it right after the declaration with name Human. In your
program you can just say for instance: " Human.height = 1.70 " and it
will work
Here is another example:
typedef struct _Human {
int height;
}Human;
int main(){
std::cout << "The Human Project" << std::endl;
std::cout << "How tall are you " ;
std::cin >> Human.height;
return 0;
}
In the second example you have declared a struct named Human, but has
not yet been instantiated.
Your code would look something like this
struct Human {
int height;
};
int main(){
Human human; // Instantiating the Human struct
std::cout << "The Human Project" << std::endl;
std::cout << "How tall are you " ;
std::cin >> human.height; // <<==
return 0;
}
Which one is better? it depends on the program you are working on, really!
Hava a good one!
coo...@ho... wrote:
> what is the difference between these two
>
> typedef struct _Human
> {
> int height;
> }Human;
>
> struct Human
> {
> int height;
> };
>
>
> and in the first one, what does 'Human' mean at the end? In both
> structs u can declare a Human type, even without the Human thing at
> the end in the first one
>
> _________________________________________________________________
> STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
> http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca
>
>
>
>
> -------------------------------------------------------
> The SF.Net email is sponsored by EclipseCon 2004
> Premiere Conference on Open Tools Development and Integration
> See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> http://www.eclipsecon.org/osdn
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|
|
From: <or...@vp...> - 2004-01-31 12:30:19
|
Maya wrote:
> In the first one you have declared a struct named _Human and=20
> instantiated it right after the declaration with name Human. In your=20
> program you can just say for instance: " Human.height =3D 1.70 " and it=
=20
> will work
> Here is another example:
> typedef struct _Human {
> int height;
> }Human;
>=20
> int main(){
> std::cout << "The Human Project" << std::endl;
> std::cout << "How tall are you " ;
> std::cin >> Human.height;
> return 0;
> }
No, it is not true. In fact this doesn't even work.
--
Greetings,
Bal=C3=A1zs
|
|
From: Per W. <pw...@ia...> - 2004-01-31 13:18:02
|
Maya - try compiling your examples :-)
This is a construct that is affected by language.
In C:
struct Human {
...
};
Require you to always declare your variables as:
struct Human a_human;
In C++ - together with the addition of the class keyword - you can get
away with:
struct Human {
...
};
Human a_human;
or
class Human {
public:
...
};
Human a_human;
So basically:
typedef struct Human {
...
} _Human;
declares a structure named Human, and typedef this struct to the name
_Human.
In C, you can then declare a variable as either of:
struct Human a_human; /* referencing the name of the struct */
_Human a_human; /* referencing type typedef name */
while in C++ it doesn't matter. It is even possible to define the data
type as:
typedef struct Human {
..
} Human;
i.e. same name for the struct declaration and for type typedef.
/Per W
On Sat, 31 Jan 2004, Maya wrote:
> In the first one you have declared a struct named _Human and
> instantiated it right after the declaration with name Human. In your
> program you can just say for instance: " Human.height = 1.70 " and it
> will work
> Here is another example:
> typedef struct _Human {
> int height;
> }Human;
>
> int main(){
> std::cout << "The Human Project" << std::endl;
> std::cout << "How tall are you " ;
> std::cin >> Human.height;
> return 0;
> }
>
> In the second example you have declared a struct named Human, but has
> not yet been instantiated.
> Your code would look something like this
>
> struct Human {
> int height;
> };
> int main(){
> Human human; // Instantiating the Human struct
> std::cout << "The Human Project" << std::endl;
> std::cout << "How tall are you " ;
> std::cin >> human.height; // <<==
> return 0;
> }
> Which one is better? it depends on the program you are working on, really!
>
> Hava a good one!
> coo...@ho... wrote:
>
> > what is the difference between these two
> >
> > typedef struct _Human
> > {
> > int height;
> > }Human;
> >
> > struct Human
> > {
> > int height;
> > };
> >
> >
> > and in the first one, what does 'Human' mean at the end? In both
> > structs u can declare a Human type, even without the Human thing at
> > the end in the first one
> >
> > _________________________________________________________________
> > STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
> > http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca
> >
> >
> >
> >
> > -------------------------------------------------------
> > The SF.Net email is sponsored by EclipseCon 2004
> > Premiere Conference on Open Tools Development and Integration
> > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> > http://www.eclipsecon.org/osdn
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> --
>
> FYI
> http://www.astro.umd.edu/~marshall/abbrev.html
>
> E-Mail Policy
> http://www.vif.com/users/escalante/Email_Policy.html
>
> * You cannot exercise your power to a point of humiliation.
> - Jean Chretien
>
> * The media's the most powerful entity on earth. They have the
> power to make the innocent guilty and to make the guilty
> innocent, and that's power.
> - Malcom X
> * "Innocent until proven guilty", no... not in Canada!!
>
>
>
>
> -------------------------------------------------------
> The SF.Net email is sponsored by EclipseCon 2004
> Premiere Conference on Open Tools Development and Integration
> See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> http://www.eclipsecon.org/osdn
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ed C. <em...@ed...> - 2004-01-31 19:16:55
|
Hi, The former declares a datatype named Human; the latter declares a structure tag Human. To declare a Human named Joe, in the first case you would simply type: Human Joe; In the latter, it would be: struct Human Joe; See also http://www.eskimo.com/~scs/C-faq/q2.1.html -Ed |
|
From: <de...@ci...> - 2004-01-31 21:59:02
|
In the first form you have two different types, and you can use either.
For instance
struct _Human fred;
Human joe;
Note that it is simply to avoid always having to refer to struct _Human
that the typedef is used. This is traditional C usage.
In C++ on the other hand the second form is used, and the type is Human,
not struct Human.
So you can use
Human joe;
C++ compilers will compile the first form too, but the resulting types
will be _Human and Human unless there is an extern "C" block surrounding
the declaration.
In article <BAY...@ho...>,
coo...@ho... (Noorez Kassam) wrote:
> what is the difference between these two
>
> typedef struct _Human
> {
> int height;
> }Human;
>
> struct Human
> {
> int height;
> };
>
>
> and in the first one, what does 'Human' mean at the end? In both
> structs u can declare a Human type, even without the Human thing at the
> end in the first one
|
|
From: <and...@su...> - 2003-04-11 19:18:37
|
Please! Foget the auto name!!!! It´s a keyword. Chage it for some like
hot_auto :-)))))
Andre
-----Mensagem original-----
De: André Macário Barros [mailto:and...@su...]
Enviada em: sexta-feira, 11 de abril de 2003 16:09
Para: Dev-Cpp-Users
Assunto: structs
Please consider the following code:
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 = elevator(auto);
22- printf("Please park your %s in the %d. floor\n", auto.name, floor);
23- system("PAUSE");
24- return 0;
25-}
26-
27-int elevator(struct car car_arg)
28-{
29- if(car_arg.size == 1) return 1;
30- if(car_arg.size == 2) return 2;
31- if(car_arg.size == 3) return 3;
32-}
1-I know that I don´t need to pass all the struct. Only the size is enough;
:-)))
2-I know I don´t need the int floor variable. I could 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 27, as the
"struct variable
declarations";
My Questions:
A) My statement in 3 is correct?(y/n, comment)
B) My statement in 4 is correct?(y/n, comment)
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?
Thank you again
Andre
|
|
From: Ioannis V. <iv...@at...> - 2003-04-11 19:40:56
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Andrι Macαrio Barros
> Sent: Friday, April 11, 2003 10:09 PM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] structs
>
>
> Please consider the following code:
>
> 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 = elevator(auto);
> 22- printf("Please park your %s in the %d. floor\n",
> auto.name, floor);
> 23- system("PAUSE");
> 24- return 0;
> 25-}
> 26-
> 27-int elevator(struct car car_arg)
> 28-{
> 29- if(car_arg.size == 1) return 1;
> 30- if(car_arg.size == 2) return 2;
> 31- if(car_arg.size == 3) return 3;
> 32-}
>
> 1-I know that I don´t need to pass all the struct. Only the
> size is enough;
> :-)))
> 2-I know I don´t need the int floor variable. I could
> 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
> 27, as the
> "struct variable
> declarations";
>
> 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
* 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
|
|
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
|