strtol

F

Frank

#include <stdlib.h>
long strtol(
const char * restrict str, char ** restrict ptr, int base);

How does one print everything between the restricted pointer to a
pointer to char named ptr, and the char itself?

Thanks for your comment,
 
P

Peter Nilsson

#include <stdlib.h>
long strtol(
   const char * restrict str, char ** restrict ptr, int base);

How does one print everything between the restricted pointer to a
pointer to char named ptr, and the char itself?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc)
while (argv++, --argc)
{
long n;
char *endp;
n = strtol(*argv, &endp, 0);
printf("%.*s: %ld\n", (int) (endp - *argv), *argv, n);
}

return 0;
}
 
F

Frank

F:\gfortran\dan>gcc j1.c -Wall -o j.exe
j1.c:18:26: warning: no newline at end of file

F:\gfortran\dan>j 5 ot3.txt
5: 5
: 0

F:\gfortran\dan>type j1.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc)
while (argv++, --argc)
{
long n;
char *endp;
n = strtol(*argv, &endp, 0);
printf("%.*s: %ld\n", (int) (endp - *argv), *argv, n);
}

return 0;
}

//gcc j1.c -Wall -o j.exe
F:\gfortran\dan>

The thing that really catches my attention in the above is:

F:\gfortran\dan>j 5 ot3.txt
5: 5
: 0

What does the ultimate zero say?
 
J

Jens Thoms Toerring

F:\gfortran\dan>type j1.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc)
while (argv++, --argc)
{
long n;
char *endp;
n = strtol(*argv, &endp, 0);
printf("%.*s: %ld\n", (int) (endp - *argv), *argv, n);
}
return 0;
}
//gcc j1.c -Wall -o j.exe
F:\gfortran\dan>
The thing that really catches my attention in the above is:
F:\gfortran\dan>j 5 ot3.txt
5: 5
: 0
What does the ultimate zero say?

That strtol() failed to convert anything from "ot3.txt" - it
finds that the string starts with nothing that could be part
of a number and returns 0. That's what then gets printed.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top