473,406 Members | 2,208 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,406 software developers and data experts.

Trouble with pointers

Hi in here,

I have a problem with the following lines of code:

printf("\tCalc this:\t %f * (%f -%f) = %f\n",
tCtrl->dGain, *p_dDesiredValues, *p_dActualValues,
( tCtrl->dGain * (*p_dDesiredValues-*p_dActualValues )));

They are part of a function, where
tCtrl is a pointer to a struct
containing a double called
'dGain'
p_dDesiredValues Pointer to double
p_dActualValues Pointer to another double.

Sometimes the command gives the weird output:
Calc this: 6.000000 * (2.000000 -1.800000) = -1.#IND00

If I put the same statement twice, the second always gives:
Calc this: 6.000000 * (2.000000 -1.800000) = 1.200000

Well, the real thing is that this is not part of an
C-program, but an subroutine to an numerical math tool.
The subroutine is written in c.
I know this is off topic here. That's why I just want to
know if something of the above thing is wrong in 'c-sense',
or if it's ok and the error must be somewhere else.

Regards,
Michael

Nov 14 '05 #1
9 1256


Michael wrote:
Hi in here,

I have a problem with the following lines of code:

printf("\tCalc this:\t %f * (%f -%f) = %f\n",
tCtrl->dGain, *p_dDesiredValues, *p_dActualValues,
( tCtrl->dGain * (*p_dDesiredValues-*p_dActualValues )));

They are part of a function, where
tCtrl is a pointer to a struct
containing a double called
'dGain'
p_dDesiredValues Pointer to double
p_dActualValues Pointer to another double.

Sometimes the command gives the weird output:
Calc this: 6.000000 * (2.000000 -1.800000) = -1.#IND00

If I put the same statement twice, the second always gives:
Calc this: 6.000000 * (2.000000 -1.800000) = 1.200000

Well, the real thing is that this is not part of an
C-program, but an subroutine to an numerical math tool.
The subroutine is written in c.
I know this is off topic here. That's why I just want to
know if something of the above thing is wrong in 'c-sense',
or if it's ok and the error must be somewhere else.


If the variables are as you describe, I do not see anything
wrong with the printf statement. I applied the printf
statement in the following code and got the output:

Calc this: 8.000000 * (2.000000 -1.800000) = 1.600000

The Code:

#include <stdio.h>

struct TEST
{
double dGain;
};

int main(void)
{
double arr[] = {2.0,1.8};
struct TEST test = {8.0};
struct TEST *tCtrl = &test;
double *p_dDesiredValues = &arr[0];
double *p_dActualValues = &arr[1];

printf("\tCalc this:\t %f * (%f -%f) = %f\n",
tCtrl->dGain, *p_dDesiredValues, *p_dActualValues,
( tCtrl->dGain * (*p_dDesiredValues-*p_dActualValues )));
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2
Hi thanks,

hmm, so there are no side effects. What really
scares me is the fact that the parameters are
printed correct, but their combination isn't.

Thanks again.

What does the wrong output "-1.#IND00" mean?
Is this already crap from the program on top,
or created by printf in case wrong values in variables?

Regards
Michael

Al Bowers schrieb:

If the variables are as you describe, I do not see anything
wrong with the printf statement. I applied the printf
statement in the following code and got the output:

Calc this: 8.000000 * (2.000000 -1.800000) = 1.600000

The Code:

[...]

Nov 14 '05 #3
On Tue, 11 May 2004 17:50:32 +0200, in comp.lang.c , Michael
<mi****@gmx.net> wrote:
Hi thanks,

hmm, so there are no side effects. What really
scares me is the fact that the parameters are
printed correct, but their combination isn't.

Thanks again.

What does the wrong output "-1.#IND00" mean?


Given that your code is valid, it means that your stack was scrambled
before you even got into that part of the code.

You're debugging the wrong place. Somewhere else in your programme, you
wandered off an array bound, used an uninitialised variable, or otherwise
Broke the Rules. This resulted in your programme going subtly wrong, but
the problem did not manifest itself till the line you are examining.

You need to examine the code earlier in the execution cycle.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #4
Hi Mark,

that was a good hint, thanks a lot. I missed
to include math.h at the right place so I guess,
after calling 'floor' I got a mess somewhere.
But wonder why my compiler doesn't notify that
there is no floor-function. Maybe that's because
it is part of the 'num-math'-programm

Bye,
Michael

Mark McIntyre wrote:

Given that your code is valid, it means that your stack was scrambled
before you even got into that part of the code.

You're debugging the wrong place. Somewhere else in your programme, you
wandered off an array bound, used an uninitialised variable, or otherwise
Broke the Rules. This resulted in your programme going subtly wrong, but
the problem did not manifest itself till the line you are examining.

You need to examine the code earlier in the execution cycle.


Nov 14 '05 #5
In <c7*********@rzcomm2.rz.tu-bs.de> Michael <mi****@gmx.net> writes:
that was a good hint, thanks a lot. I missed
to include math.h at the right place so I guess,
after calling 'floor' I got a mess somewhere.
But wonder why my compiler doesn't notify that
there is no floor-function.


For the simple reason that there *is* a floor function, whether you
include <math.h> or not. The purpose of including <math.h> is not
providing the function, it is providing *correct* information about the
function, so that the compiler knows how to *correctly* call it. In the
absence of this information, the compiler *must* assume that the function
returns an int value, which is wrong in the case of floor.

Most compilers have an option that triggers warnings about functions that
are called without being previously declared. You *really* want to use
it.

The good news is that C99 enforces such declarations. The bad news is
that old style declarations are enough to make C99 happy.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Hi Dan,

thanks for reply. I was used to get warnings
in those cases, so I thought there will always be
one. Always nice to get new knowledge about c, helping
me to avoid such errors in future. I'll check the flags
more carefully then.
But why isn't there a problem when linking the stuff?
I would expect at least a warning, because there is no
'int floor(.)' arround?

Thanks,
Michael

Dan Pop wrote:
For the simple reason that there *is* a floor function, whether you
include <math.h> or not. The purpose of including <math.h> is not
providing the function, it is providing *correct* information about the
function, so that the compiler knows how to *correctly* call it. In the
absence of this information, the compiler *must* assume that the function
returns an int value, which is wrong in the case of floor.
[...]


Nov 14 '05 #7
In <c7**********@rzcomm2.rz.tu-bs.de> Michael <mi****@gmx.net> writes:
But why isn't there a problem when linking the stuff?
I would expect at least a warning, because there is no
'int floor(.)' arround?


The typical linker is very dumb. It merely looks for a symbol named
"floor" in the libraries, without paying any attention to how it was
declared in the C program. Once it finds it, it is happy. Proof:

fangorn:~/tmp 10> cat test.c
#include <stdio.h>
extern int floor;

int main()
{
printf("%d\n", floor);
return 0;
}
fangorn:~/tmp 11> gcc -Wall test.c -lm
fangorn:~/tmp 12> ./a.out
-1783093761
fangorn:~/tmp 13> gcc -Wall test.c
/tmp/ccggvCnb.o(.text+0x11): In function `main':
: undefined reference to `floor'

This example clearly shows that the floor function from libm.a was good
enough for the floor identifier declared by my program, as far as the
linker was concerned, despite the complete incompatibility of type.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
On Wed, 12 May 2004 17:35:10 +0200, in comp.lang.c , Michael
<mi****@gmx.net> wrote:
Hi Dan,

thanks for reply. I was used to get warnings
in those cases, so I thought there will always be
one. Always nice to get new knowledge about c, helping
me to avoid such errors in future. I'll check the flags
more carefully then.
Absolutely. Always have warninglevels as high as possible.
But why isn't there a problem when linking the stuff?
I would expect at least a warning, because there is no
'int floor(.)' arround?


at the linker stage, the linker is merely looking for a fn with the right
name, its no longer concerned about arguments or return types.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #9
Hi,
and thanks to both of you.
Slowly I understand the advice of my uncle,
"Think of the compiler working like:
Thats the way you want it, here you are!"
Bye and enjoy the rest of the week

Michael

Mark McIntyre wrote:
at the linker stage, the linker is merely looking for a fn with the right
name, its no longer concerned about arguments or return types.


Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
4
by: OzgurGul | last post by:
int main(){ /* I have a function :) and I want to use it but I dunno how to call it... (I have trouble that pointers) please help me... mmm... thanks for all advance!! */
14
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events. The method definitions for EventManager have...
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
7
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
11
by: Eric | last post by:
hi, I want to convert a C# class into COM, so that I can use the class in C++. The codes compile and link well. But when I run the program, I got a exception. Any comment is welcome. Thanks in...
3
by: benjamin sago | last post by:
Hello all. I am writing a command shell, but am having trouble with memory allocations. I am trying to tokenise a string into a **char array for execvp (the unix launch-this-program function), but...
27
by: Neil | last post by:
Hello all! I wrote program with a array of pointers, and I suspect they are pointing at each other in the Do ...While loop. Something is messed up with the increment variable word. A program...
4
by: aj | last post by:
Can someone explain why this example works: bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2, int &opOct3, int &opOct4) { int b1, b2, b3, b4; unsigned char c;
3
blackstormdragon
by: blackstormdragon | last post by:
My trouble causing lines are maked within the code. #include<iostream> #include<string> using namespace std; void main() { string* head; string* tail;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.