LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem about pointer in C (https://www.linuxquestions.org/questions/programming-9/problem-about-pointer-in-c-164807/)

akin81 03-31-2004 11:14 PM

Problem about pointer in C
 
I am not so familiar with pointers.
For the function "long strtol(const char *str, char **endptr, int base);"
Can anyone give me an example for how to use the endptr?

I am trying to convert a string to an integer and then get the sub-string follows that integer.

nhs 04-01-2004 05:56 AM

This code will convert the 52000 in tmpstr to an integer 52000 and invalidstr will point to " Invalid" (the first invalid character of the initial string)

int main(int argc,char **argv)
{
char *tmpstr = "52000 Invalid";
char *invalidstr = NULL;

int result = strtol(tmpstr,&invalidstr,10);

printf("Integer Value: %d\n",result);
printf("Invalid String: %s\n",invalidstr);

return 0;
}

The code will output:

Integer Value: 52000
Invalid String: Invalid

The & in front of invalidstr supplies a pointer to the pointer which allows the function to change the value of invalidstr inside the function. Note also that as both tmpstr and invalidstr point to the same bit of memory one could retrieve the number of valid characters by subtracting tmpstr from invalidstr.

worldmagic 04-01-2004 06:05 AM

Lets do a trick, read it backwards "char * * endPtr":
"EndPtr" are a "pointer" to a "pointer" of type "char".

A pointer is a 32bit value in the memory (on i386). This memory holds a value thats an address of some other storage area.

[4BytesAddress] -> [4BytesAddress] -> ["SomeString"]

The only thing you realy can pass to a function in C are values, so.. to give the function a chance to return something more then its return _value_ we have to send in a value, that means something both to us and to the function. The value we sendin is an memory address of an storage area we know about. The function stores its second return argument in this memory area. After the function returns we can look in this area, in this case it will contain an address to the start of the "NonDigitPart" of the String we sent in.

This is how it looks like before we sent **endPtr into the function.
[4bytesAddress]->[NULL]

This is how it looks like after we called strtol()
[4bytesAddress]->[4bytesAddress]->["ABC"]

I used an extra variable to make it totaly clear whats going on.
Normaly you just call the function like:
myNumber = strtol(myNumStr, &pEndOfString, 10);

Code:

#include <string.h>
#include <stdlib.h>

int main() {

        char *myNumStrMix = "123ABC";
        char *pEndOfDigits = NULL;
        char **ppEndOfDigits;
        int  myNumber;

        ppEndOfDigits = &pEndOfDigits;
        myNumber = strtol(myNumStrMix, ppEndOfDigits, 10);

        printf("From '%s' I got '%d' and this '%s'\n",
              myNumStrMix, myNumber, pEndOfDigits);

        return EXIT_SUCCESS;
}

Hope this helped.

akin81 04-02-2004 12:48 AM

Thanks for the detail explanations.
What didn't work for me is i didn't know to put the "&" sign before the pointer.

But one thing not so clear to me is why we need a pointer to pointer?
What is the advantage of using that?

itsme86 04-02-2004 06:35 PM

It servers a lot of purposes ;)

Hko 04-03-2004 06:44 AM

Quote:

Originally posted by akin81
But one thing not so clear to me is why we need a pointer to pointer?
What is the advantage of using that?

In the case of strtol() you may need to know where the string-to-integer conversion stopped because strtol() ran into character that is invalid for the conversion.

Strings are pointer-to-char's in C, so strtol() wants to return a pointer-to-char to the calling code. Strtol() could just return that pointer, but the strtol() already returns the result of the conversion: a long int.

Now, if we pass a pointer-to-char to strtol(), any changes made to that pointer by strtol() are lost at return-time, because a function always receives a temporary copy of it's parameters.

If the strtol() needs to be able to change a parameter, you can pass it the address of a parameter, which is a pointer to the parameter, from strtol()'s point of view.

In this case the parameter we want strtol() to change is a pointer (to char) itself already. So we pass the address of the pointer-to-char to strtol() by putting a '&' in front of it. What strtol() then receives is the address of a pointer-to-char, in other words: a pointer-to-pointer-to-char. So now it can change the pointer-to-char "owned" ("declared" to be more precise) by the calling code.

Hope this helps.


All times are GMT -5. The time now is 07:40 PM.