Re: Newbie question

Jens.Toerring_at_physik.fu-berlin.de
Date: 01/02/05


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


Relevant Pages

  • Re: forwards declarations!
    ... h, long m, int w, int l); ... compiler obviously doesn't. ... LRESULT callerFunction(HWND, long, WPARAM, LPARAM), HWND ... Not quite the same as straight forwards function pointer usage. ...
    (microsoft.public.vc.language)
  • Re: Common Problems that Compilers Dont Catch
    ... where the compiler offered no help. ... Become more familiar with pointer variables and you won't be making such ... int i = p; ... Any pointer type may be converted to an integer type. ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... Why do you lie to the compiler? ... you define later takes an argument, a pointer to pointer to ... Under both Windows and Linux mainalways returns an int (and ... not know about the return type of malloc(). ...
    (comp.lang.c)
  • Re: Confused with malloc
    ... malloc without prototyppe gets ... interpreted as int malland this gets you code like: ... Not every compiler uses one and the same location with exactly the ... same number of bits for int and pointer to something. ...
    (comp.lang.c)
  • Re: 32-bit programme with x87/sse2-floating-point running under a 64-bit windows os
    ... int x, y; // x, y are not stored on the stack as supposed, they are ESI ... EBX //depends on OS (a compiler can hedge and save/restore in both ... caller save, so the caller has to save them if it wants to keep ... when SSE or x87 regs got invalid on return ... ...
    (alt.lang.asm)