Re: Newbie question
Jens.Toerring_at_physik.fu-berlin.de
Date: 01/02/05
- Next message: Barry Schwarz: "Re: Newbie question"
- Previous message: Keith Thompson: "Re: vesa mode 105h or 117h"
- In reply to: vadud3: "Re: Newbie question"
- Next in thread: Chris Torek: "Re: Newbie question"
- Reply: Chris Torek: "Re: Newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 2 Jan 2005 07:54:36 GMT
vadud3 <vadud3@gmail.com> wrote:
> That was how it was exactly in the book. I wonder if there might be
> something very simple that came from the book as typo.
Then it might a good idea to throw away that book and get a better one.
It can't be a single typo and there are also several other problems with
the program.
> #include <stdio.h>
> #include <string.h>
>
> typedef struct employee {
> int id;
> char name[10];
> float salary;
> } e;
>
> void processEmp(e); //supply function prototype with structure alias
> name
At least for posts in newsgroups better use the traditional "/*" and
"*/" comment delimiters - it makes it less error prone for others that
try to copy and paste the code when lines are too long and get broken
by the newsreader. Moreover, comments starting with "//" became only
legal with the newer C99 standard and they might get you into problems
with older compilers.
> main()
main() is a function that returns an int. Relying on that being the
default isn't an option anymore if your program is to be compiled
with a C99 compliant compiler. And it's also clearer to specify the
arguments main() is supposed to accept, i.e. none. So make that line
int main( void )
> {
> e empl = {0,'\0',0}; //Initialize members
The second member of the employee structure is a char array, not a
char, so the initialization must be either
e empl = { 0, "", 0 };
or
e empl = { 0, { '\0' }, 0};
> processEmp (empl); //pass structure by value
And this means that the function receives a copy of the structure.
Everything done by that function gets done to the copy it received
but not to the structure you have in the caller of the function,
which remains unchanged. As Martin Ambuhl already pointed out you
can correct that by passing a pointer to the structure to the func-
tion and operate on the structure belonging to the caller via this
pointer. Another option would be to have the function return the
modified structure and to assign this return value to 'empl'.
> printf("\nID: %d\n", empl.id);
> printf("Name: %s\n", empl.name);
> printf("Salary : $%.2f\n", empl.salary);
> } // end main
Since main() returns an int you're missing a return statement here.
Use either
return EXIT_SUCCESS;
(but then you need to include <stdlib.h>) or
return 0;
on successful exit from main().
Some of the problems the compiler can tell you about if you invoke it
with the appropriate options. In the case of gcc I would recommend to
use at least "-W -Wall" - it then will warn you about code that is
problematic.
Regards, Jens
-- \ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de \__________________________ http://www.toerring.de
- Next message: Barry Schwarz: "Re: Newbie question"
- Previous message: Keith Thompson: "Re: vesa mode 105h or 117h"
- In reply to: vadud3: "Re: Newbie question"
- Next in thread: Chris Torek: "Re: Newbie question"
- Reply: Chris Torek: "Re: Newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|