473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
8 1862
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
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
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
>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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tomaz Rotovnik | last post by:
Hi I created very simple dll (vc++) which has three functions (start, stop and initialization). it starts capturing sound from soundblaster and when the buffer is filled with the data, dll calls...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
51
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
8
by: mast2as | last post by:
I almost apologize to ask this question but I have been starting at this code for a bit of time and don't understand what's wrong with it. I am not arguing about the fact it's good or not coding,...
5
by: shanfeng | last post by:
such as a function: f(double v, int a) could this function return both a integer and array? Thank you very much. I have confused on this for a long time~
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.