473,805 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I call a function taking only "const char *a"

Hi all,

I'm totally new to C and hava a problem how to handle different types
of variables.

I have a function which which you call by:

double calcdiff(const char *old, const char *new)

I have at start two doubles that I need to convert in order to call the
above function. How would I go about to do that?

Thanks

Dec 14 '06 #1
13 1661
RoughRyde wrote:
I'm totally new to C and hava a problem how to handle different types
of variables.
[Get a decent C book. Dripfeeding via newsgroup is slow.]
I have a function which which you call by:

double calcdiff(const char *old, const char *new)
You have a function called `calcdiff` which returns a `double` and
it takes two /strings/ as arguments?

Something is broken.
I have at start two doubles that I need to convert in order to call the
above function. How would I go about to do that?
If you want to get the string representation of a double value,
probably you should use `sprintf`. But I'd apply the Birmingham
principle [1] first. If `calcdiff` takes two strings representing
doubles, turns them into doubles, subtracts them, and returns the
result, then it's a pretty naff function (and if it doesn't, it's
been given a naff name: heads you lose, tails I win).

[1] "I wouldn't start from here."

--
Chris "Perikles triumphant" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Dec 14 '06 #2

"RoughRyde" <fr**********@g mail.comwrote in message
news:11******** *************@1 6g2000cwy.googl egroups.com...
Hi all,

I'm totally new to C and hava a problem how to handle different types
of variables.

I have a function which which you call by:

double calcdiff(const char *old, const char *new)

I have at start two doubles that I need to convert in order to call the
above function. How would I go about to do that?

Thanks
You haven't given enough information. What does the function actually
do? What format are the inputs supposed to be in?

For example:

d = calcdiff( "3.555", "1.7E-16" );

or

d = calcdiff( "three and a half", "pi" );

If it is just the former, and the function does exactly what its name
implies, why don't you just use:

d = var1 - var2;

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Dec 14 '06 #3
What I want is to calculate the number of days between two dates.
Given my limited knowledge of C I found an example of this on the web
which looked as below. The problem is that I'm given indata as doubles.
So in order to take the easy way out I just wanted to convert those
doubles.

But if you have any other suggestions on how to solve this that's
appreciated.

double calcdiff(const char *old, const char *new)
{
struct tm t1 = {0};
struct tm t2 = {0};
time_t tt1 = {0};
time_t tt2 = {0};

stotm(&t1, old);
stotm(&t2, new);
tt1 = mktime(&t1);
tt2 = mktime(&t2);
if(tt1 != -1 && tt2 != -1)
{
long day = 0;
double d = difftime(tt1, tt2);
if(d < 0) d *= -1.0;
day = d / 86400;
return day;
}
else
{
return 0;
}

}

void stotm(struct tm *p, const char *s)
{
int i;
struct tm blank = {0};
*p = blank;

for(i = 0; i < 4; i++)
{
p->tm_year *= 10;
p->tm_year += *s++ - '0';
}
p->tm_year -= 1900;
for(i = 0; i < 2; i++)
{
p->tm_mon *= 10;
p->tm_mon += *s++ - '0';
}
--p->tm_mon;
p->tm_mday = 10 * (*s++ - '0');
p->tm_mday += *s++ - '0';

}

Fred Kleinschmidt skrev:
"RoughRyde" <fr**********@g mail.comwrote in message
news:11******** *************@1 6g2000cwy.googl egroups.com...
Hi all,

I'm totally new to C and hava a problem how to handle different types
of variables.

I have a function which which you call by:

double calcdiff(const char *old, const char *new)

I have at start two doubles that I need to convert in order to call the
above function. How would I go about to do that?

Thanks
You haven't given enough information. What does the function actually
do? What format are the inputs supposed to be in?

For example:

d = calcdiff( "3.555", "1.7E-16" );

or

d = calcdiff( "three and a half", "pi" );

If it is just the former, and the function does exactly what its name
implies, why don't you just use:

d = var1 - var2;

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Dec 14 '06 #4
RoughRyde wrote:
What I want is to calculate the number of days between two dates.
Given my limited knowledge of C I found an example of this on the web
which looked as below. The problem is that I'm given indata as doubles.
What do the doubles contain? Without knowing that, it's pretty hard to
suggest what might work better.

Based on the code you posted, let's pretend that the doubles you're
given represent a date as

10000 * year + 100 * month + day

so that today's date would be 20061214.0. If that's the case, you can
replace the stotm() function with something like this,

void dtotm( struct tm *t, double d )
{
memset( t, 0, sizeof( *t ));
t->tm_year = ( int ) d / 10000 - 1900;
t->tm_mon = ( int ) fmod( d, 10000 ) / 100 - 1;
t->tm_mday = ( int ) fmod( d, 100 );
}

Change calcdiff() to accept double arguments rather than strings, and
replace calls to stotm() with dtotm().

But now you have another problem. If either of the dates is before
January 1, 1970, calcdiff() will fail. In many implementations , it'll
also fail if either date is after January 18, 2038. This is the range
of dates that can typically be stored in a value of type time_t.

If you need to work with dates outside this range, consider converting
them to Julian day numbers. This is the number of days since January 1,
4713 B.C. Today is JD 2454084, for example. You can subtract these
numbers directly to find the number of days between two dates. Code for
converting to JD is easily googled.

- Ernie http://home.comcast.net/~erniew

Dec 14 '06 #5
Ernie Wright <er****@comcast .netwrites:
[...]
Based on the code you posted, let's pretend that the doubles you're
given represent a date as

10000 * year + 100 * month + day

so that today's date would be 20061214.0. If that's the case, you can
replace the stotm() function with something like this,

void dtotm( struct tm *t, double d )
{
memset( t, 0, sizeof( *t ));
t->tm_year = ( int ) d / 10000 - 1900;
t->tm_mon = ( int ) fmod( d, 10000 ) / 100 - 1;
t->tm_mday = ( int ) fmod( d, 100 );
}

Change calcdiff() to accept double arguments rather than strings, and
replace calls to stotm() with dtotm().

But now you have another problem. If either of the dates is before
January 1, 1970, calcdiff() will fail. In many implementations , it'll
also fail if either date is after January 18, 2038. This is the range
of dates that can typically be stored in a value of type time_t.
Typically, but not always. *Many* systems use a time_t representation
in which 0 represents January 1, 1970, but this is not specified in
the C standard. The standard says only that time_t is an arithmetic
type capable of representing times.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 14 '06 #6
Keith Thompson wrote:
*Many* systems use a time_t representation in which 0 represents
January 1, 1970, but this is not specified in the C standard. The
standard says only that time_t is an arithmetic type capable of
representing times.
Thanks for the clarification.

It looks like the standard does *not* specify

the resolution (usually 1 second),
the epoch (the meaning of (time_t) 0, usually January 1, 1970),
a maximum time_t value (often LONG_MAX),
whether time_t is signed (whether (time_t) -1 is meaningful),
or whether a time_t is aligned to a standard time scale (e.g. UTC).

I'm a little surprised it's that vague.

- Ernie http://home.comcast.net/~erniew

Dec 15 '06 #7
Ernie Wright wrote:
Keith Thompson wrote:
>*Many* systems use a time_t representation in which 0 represents
January 1, 1970, but this is not specified in the C standard. The
standard says only that time_t is an arithmetic type capable of
representing times.

Thanks for the clarification.

It looks like the standard does *not* specify

the resolution (usually 1 second),
the epoch (the meaning of (time_t) 0, usually January 1, 1970),
a maximum time_t value (often LONG_MAX),
whether time_t is signed (whether (time_t) -1 is meaningful),
or whether a time_t is aligned to a standard time scale (e.g. UTC).

I'm a little surprised it's that vague.
Why should you be surprised? When the standard was promulgated any
further specification of those items would have broken existing
code, and it was a specific objective to minimize such breakage.
It is always possible to wrap the standard in more restrictive
standards, e.g. Posix, but not to relax things.

Read the C99 Rationale (N897.pdf) which includes much of the C90
rationale.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 15 '06 #8
Ernie Wright <er****@comcast .netwrites:
Keith Thompson wrote:
>*Many* systems use a time_t representation in which 0 represents
January 1, 1970, but this is not specified in the C standard. The
standard says only that time_t is an arithmetic type capable of
representing times.

Thanks for the clarification.

It looks like the standard does *not* specify

the resolution (usually 1 second),
the epoch (the meaning of (time_t) 0, usually January 1, 1970),
a maximum time_t value (often LONG_MAX),
whether time_t is signed (whether (time_t) -1 is meaningful),
or whether a time_t is aligned to a standard time scale (e.g. UTC).

I'm a little surprised it's that vague.
It's vaguer than that. There's no guarantee that time_t is an integer
type, or that the relationship between time_t values and actual time
is linear, or even that it's monotonic (i.e., that increasing time_t
values represent increasing times).

To take just one example, a conforming implementation could represent
the current time (2006-12-15 08:27:23 UTC) as the integer value
20061215082723 or as the floating-point value 20061215.082723 . Or it
could use ranges of bits, rather than of decimal digits, to represent
the various fields. And so on.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '06 #9
CBFalconer wrote:
Ernie Wright wrote:
>I'm a little surprised [the standard re time_t is] that vague.

Why should you be surprised?
Partly ignorance. Not knowing what the standard said about it, I've
assumed it was along the lines of what POSIX says about it.

But also partly because, unless it's wrapped in something more rigorous
like POSIX, it can't be used portably and is almost useless. To quote
one example from the rationale,

Since no measure is given for how precise an implementation’ s best
approximation to the current time must be, an implementation could
always return the same date [from time()] instead of a more honest
–1. This is, of course, not the intent.

And throw in Keith Thompson's observations here as well.

Basically, without knowing anything about the precision or the range,
you can't portably rely on time() and mktime() to do anything at all.

Correction to my previous post: time_t must be signed (in some vague
sense: it must be able to represent (time_t) -1), but since this is
used as an error return by time() and mktime(), as a practical matter it
usually cannot represent times before its epoch ((time_t) 0).

- Ernie http://home.comcast.net/~erniew

Dec 15 '06 #10

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

Similar topics

23
6648
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
2
6493
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 printf("%sHello World%s\n", green, normal); 12 return 0; 13 }
8
10121
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) When I asked why, I was told that it facilitated calling it as:
4
6094
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
9
1269
by: Gary | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of the const pointer (i.e. t)? I got a segmentation fault in the last for loop. I wrote the code in c++, but the language is not the point, right? :) Thanks in advance! #include <iostream>
26
11775
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
20
3572
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
4
3385
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.