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

0 Members and 1 Guest are viewing this topic.

jack

  • Full Member
  • ***
  • Posts: 107
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

  • Admin Support Member
  • *****
  • Posts: 1856
Re: need help with sscanf
« Reply #1 on: May 15, 2018, 07:11:54 PM »
Which compiler and OS, please?
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

jack

  • Full Member
  • ***
  • Posts: 107
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

  • Full Member
  • ***
  • Posts: 107
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 »

John

  • Hero Member
  • *****
  • Posts: 3249
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

  • Admin Support Member
  • *****
  • Posts: 1856
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
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

jack

  • Full Member
  • ***
  • Posts: 107
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

  • Admin Support Member
  • *****
  • Posts: 1856
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.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)