LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2004, 11:14 PM   #1
akin81
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Rep: Reputation: 0
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.
 
Old 04-01-2004, 05:56 AM   #2
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
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.
 
Old 04-01-2004, 06:05 AM   #3
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
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.
 
Old 04-02-2004, 12:48 AM   #4
akin81
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 04-02-2004, 06:35 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
It servers a lot of purposes

Last edited by itsme86; 04-02-2004 at 06:41 PM.
 
Old 04-03-2004, 06:44 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 04-03-2004 at 06:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
pointer dereference problem cranium2004 Programming 4 05-03-2005 09:34 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
Mouse pointer problem thort Mandriva 1 08-03-2004 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration