Author Topic: need help with sscanf  (Read 2140 times)

0 Members and 3 Guests are viewing this topic.

jack

  • Guest
need help with sscanf
« on: May 15, 2018, 03:54:58 PM »
ok, I need some help here,
Code: [Select]
#include <stdio.h>
int main()
{
long double x;
char s[]="3.1415926535897932384626433832795";
sscanf(s,"%Lf",&x);
printf("%24.19Lf\n",x);
    return 0;
}
this works ok, but why does the following not work?
Code: [Select]
#include <stdio.h>
int main()
{
long double *x;
char s[]="3.1415926535897932384626433832795";
sscanf(s,"%Lf",x);
printf("%24.19Lf\n",*x);
    return 0;
}

Mike Lobanovsky

  • Guest
Re: need help with sscanf
« Reply #1 on: May 15, 2018, 07:11:54 PM »
Which compiler and OS, please?

jack

  • Guest
Re: need help with sscanf
« Reply #2 on: May 16, 2018, 05:03:12 AM »
I am using Windows 10 and msys2 with gcc 7.3.0

jack

  • Guest
Re: need help with sscanf
« Reply #3 on: May 16, 2018, 05:29:01 AM »
silly me, I overlooked the fact that you need to allocate memory for the pointer, the following works.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main()
{
long double *x;
x = malloc (sizeof (long double));
if (x != 0)
{
char s[]="3.1415926535897932384626433832795";
sscanf(s,"%Lf",x);
printf("%24.19Lf\n",*x);
free (x);
return 0;
}
else return 1;
}
« Last Edit: May 16, 2018, 05:43:08 AM by jack »

JRS

  • Guest
Re: need help with sscanf
« Reply #4 on: May 16, 2018, 05:58:12 AM »
silly me, I overlooked the fact that you need to allocate memory for the pointer, the following works.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main()
{
long double *x;
x = malloc (sizeof (long double));
if (x != 0)
{
char s[]="3.1415926535897932384626433832795";
sscanf(s,"%Lf",x);
printf("%24.19Lf\n",*x);
free (x);
return 0;
}
else return 1;
}

Memory issues are common with aging programmers.  :o

Mike Lobanovsky

  • Guest
Re: need help with sscanf
« Reply #5 on: May 16, 2018, 12:18:01 PM »
Jack,

If you incidentally obtain any results different from what I described on Jose's forum, please let me know. I'm curious if vanilla MinGW and TDM would perform differently in such extremely compiler-dependent cases.

TIA

jack

  • Guest
Re: need help with sscanf
« Reply #6 on: May 16, 2018, 02:12:51 PM »
Mike,
you are right about the waste of memory by padding the long double structure, I wonder how PB handles the extended type, my guess it's intended to keep data aligned
as far as being msvcrt.dll compatible PB must use it's own routines to deal with the extended type so I don't understand your objection to gcc using their own routines.

Mike Lobanovsky

  • Guest
Re: need help with sscanf
« Reply #7 on: May 16, 2018, 04:05:29 PM »
Jack,

msvcrt.dll contains 1,400 exported functions and is 620KB large. MINGW_ANSI_STDIO contains probably a dozen "overload" functions only and adds more than 20KB of associated function code to the executable. The code size (efficiency) score is 4:1 in msvcrt's favor.

msvcrt zeroes out its output neatly avoiding random trash in meaningless digits. MINGW_ANSI_STDIO doesn't do that.

msvcrt doesn't crash at GCC's linuxoid frivolities; it just keeps on outputting zeros. MINGW_ANSI_STDIO crashes user programs at some of GCC's own options.

If I were to write some serious code for Linux, I would probably choose cross-platform MS VC(++) for the task. Better still, I would be glad to use the Intel C(++) compiler or suite if only I could afford spending thousands of bucks on it.

Free and open-source does not mean the best.