By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Apr 22 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
gk245 wrote:
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.

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.

Apr 22 '06 #2

P: n/a
On 2006-04-22, gk245 wrote:
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?

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Apr 22 '06 #3

P: n/a
Chris F.A. Johnson presented the following explanation :
On 2006-04-22, gk245 wrote:
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?


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?
Apr 22 '06 #4

P: n/a
>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.{
struct person *empl;


Apr 22 '06 #5

P: n/a

gk245 wrote:
Chris F.A. Johnson presented the following explanation :
On 2006-04-22, gk245 wrote:
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?


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.

Apr 22 '06 #6

P: n/a
It happens that Bill Pursell formulated :
gk245 wrote:
Chris F.A. Johnson presented the following explanation :
On 2006-04-22, gk245 wrote:
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?


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.


Thanks. :)
Apr 22 '06 #7

P: n/a
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"?
Apr 24 '06 #8

P: 26
Before passing any value in the function .
Do like this.

struct person p1;
p1.hours=5.0f;

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

This will going to work definately.
Apr 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.