G
G
Gys2021-11-09 20:58:21
C++ / C#
Gys, 2021-11-09 20:58:21

How to compare pointers?

There is a code
I need to compare the pointers num and endptr, namely to get something like this
num + strlen(num) == *endptr
But when I try to get the value of the pointer as a number, the program crashes. And it works weird.
In the code, I assign the value of the endptr pointer to the variable k. And then I output this value. And it works.
However, if I try to compare the value of k, seemingly independent, with some other number (this if), then the program crashes.
I do not understand why.
How do I end up comparing pointers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2021-11-09
@Gys

It was necessary to bring the problematic code directly in the question
. The problem is naturally here:

char ** endptr;
long a = strtol(num, endptr,  10);

Correction:
char * endptr;
long a = strtol(num, &endptr,  10);

Because strtol is waiting for a pointer to the place where the pointer should be written, and not a pointer to nowhere , as you wrote. Passing a pointer to nowhere leads to UB - and the program crashes even in a place where the error is not actually located.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question