Function crashes if a integer is passed (pointer related).

G

gk245

This is part of a bigger program, but i made it simple (basically the
OT_hours function is supposed to determine if the number entered is
over 40 hours or not and return the appropriate answer):

#include <stdio.h>
#include <stdlib.h>

struct person
{
float overtime;
float hours;
};

float OT_hours ( float hours )
{
struct person *empl;

if ( hours <= 40 )
{
empl->overtime = 0;
return ( empl->overtime );
}
else
{
empl->overtime = hours - 40;
return ( empl->overtime );
}
}

main ()
{
printf("%f", OT_hours(5));
}


And, the program crashes if compiled....i mean, i can't pass a integer
value to OT_hours? Gah, pointers are so confusing....

Thanks.
 
?

=?ISO-8859-15?Q?=22Nils_O=2E_Sel=E5sdal=22?=

gk245 said:
This is part of a bigger program, but i made it simple (basically the
OT_hours function is supposed to determine if the number entered is over
40 hours or not and return the appropriate answer):

#include <stdio.h>
#include <stdlib.h>

struct person
{
float overtime;
float hours;
};

float OT_hours ( float hours )
{
struct person *empl;
Danger.
emp1 doesn't point to a valid object.
You should make it point somewhere valid, so you have an
actual object to access.
 
C

Chris F.A. Johnson

This is part of a bigger program, but i made it simple (basically the
OT_hours function is supposed to determine if the number entered is
over 40 hours or not and return the appropriate answer):

#include <stdio.h>
#include <stdlib.h>

struct person
{
float overtime;
float hours;
};

float OT_hours ( float hours )
{
struct person *empl;

if ( hours <= 40 )
{
empl->overtime = 0;
return ( empl->overtime );
}
else
{
empl->overtime = hours - 40;
return ( empl->overtime );
}
}

main ()

int main(void)
{
printf("%f", OT_hours(5));
}


And, the program crashes if compiled....i mean, i can't pass a integer
value to OT_hours? Gah, pointers are so confusing....

Where did you allocate space for your struct?
 
G

gk245

Chris F.A. Johnson presented the following explanation :
int main(void)


Where did you allocate space for your struct?

Hmm, i think its supposed to be like this:

float OT_hours ( float hours )
struct person *emp; //allocating space for structure.
{
struct person *empl;

if ( hours <= 40 )
{
empl->overtime = 0;
return ( empl->overtime );
}
else
{
empl->overtime = hours - 40;
return ( empl->overtime );
}
}

Although i can't still compile it, am i on the right track?
 
A

Alex

float OT_hours ( float hours )
struct person *emp; //allocating space for structure.
This line does NOT allocate space, it juest allocate a pointer to
structure.
 
B

Bill Pursell

gk245 said:
Chris F.A. Johnson presented the following explanation :

Hmm, i think its supposed to be like this:

float OT_hours ( float hours )
struct person *emp; //allocating space for structure.
{
struct person *empl;

if ( hours <= 40 )
{
empl->overtime = 0;
return ( empl->overtime );
}
else
{
empl->overtime = hours - 40;
return ( empl->overtime );
}
}

Although i can't still compile it, am i on the right track?

float OT_hours ( float hours )
{
static struct person empl;
if (hours <= 40) { empl.overtime =0; return 0}
etc..
}

or..
struct person *empl;
empl = malloc(sizeof(struct person));
etc.

However, the latter case requires that the caller free the empl, and
you're not returning the empl, so that is a guaranteed memory leak. It
also points out that since you're not returning the empl, you really
don't need it at all:
float OT_hours ( float hours )
{
return (hours > 40)? (hours -40) : 0;
}
works as well as your original function.
 
G

gk245

It happens that Bill Pursell formulated :
float OT_hours ( float hours )
{
static struct person empl;
if (hours <= 40) { empl.overtime =0; return 0}
etc..
}

or..
struct person *empl;
empl = malloc(sizeof(struct person));
etc.

However, the latter case requires that the caller free the empl, and
you're not returning the empl, so that is a guaranteed memory leak. It
also points out that since you're not returning the empl, you really
don't need it at all:
float OT_hours ( float hours )
{
return (hours > 40)? (hours -40) : 0;
}
works as well as your original function.

Thanks. :)
 
P

Peter Shaggy Haywood

Groovy hepcat gk245 was jivin' on Fri, 21 Apr 2006 20:02:43 -0400 in
comp.lang.c.
Function crashes if a integer is passed (pointer related).'s a cool
scene! Dig it!
This is part of a bigger program, but i made it simple (basically the
OT_hours function is supposed to determine if the number entered is
over 40 hours or not and return the appropriate answer):

#include <stdio.h>
#include <stdlib.h>

struct person
{
float overtime;
float hours;
};

float OT_hours ( float hours )

It would be better to use double, rather than float. You could
actually use an integer type, though, if you only count whole hours.
{
struct person *empl;

if ( hours <= 40 )
{
empl->overtime = 0;
return ( empl->overtime );
}
else
{
empl->overtime = hours - 40;
return ( empl->overtime );
}
}

main ()

int main(void)
{
printf("%f", OT_hours(5)); return 0;
}

And, the program crashes if compiled....i mean, i can't pass a integer
value to OT_hours? Gah, pointers are so confusing....

It's nothing to do with integers. You have not initialised empl, so
it points the way to the Moon. You are dereferencing the uninitialised
empl, thus causing undefined behaviour. What's more, if you did
allocate memory for empl to point at, it would cause a memory leak
unless you return it.
But you don't even need empl at all. You're only trying to return
the amount by which the input excedes 40. This is extremely trivial,
and does not require any structures or pointers. Regardez:

#include <stdio.h>

double OT_hours(double hours)
{
if(hours > 40.0)
return hours - 40.0;
else
return 0.0;
}

int main(void)
{
printf("%f\n", OT_hours(5.0));
return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top