It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 4 articles in this topic
Showing 4 of 919 articles.
Currently 3 other people reading this forum.


 UWA week 36 (2nd semester, mid-semester break) ↓
SVG not supported

Login to reply

👍?
helpful
2:04pm Wed 7th Sep, ANONYMOUS

hello, i have a question about the use of pointers in strtol, strtol(char[], **endptr, int) takes: a string input (which it attempts to convert into an int), a pointer to a (previously declared) empty pointer, which it uses to return a pointer to the first element in the string that was not able to be converted to an int an int for what base we want it to perform the conversion in an example implementation is: char* endptr; char string[] = "12345n"; int i = strtol(string, &endptr, 10); this sets the value of i to 12345, and endptr now points to a string "n" I don't understand why the second parameter has to be a pointer to a pointer; why can we not just pass endptr to strtol instead of passing &endptr, the address of the pointer? where does this pointer-ception end???


SVG not supported

Login to reply

👍?
helpful
3:28pm Wed 7th Sep, Christopher M.

ANONYMOUS wrote:
> I don't understand why the second parameter has to be a pointer to a pointer; why can we not just pass endptr to strtol instead of passing &endptr, the address of the pointer?
If endptr was just an integer, and you wanted the function to be able to change that integer's value, you'd have to pass the address of endptr to the function. Similarly, even if endptr is a pointer to a char, if you want the function to be able to change that pointer's value, you have to pass the address of endptr to the function.


SVG not supported

Login to reply

👍?
helpful
3:37pm Wed 7th Sep, Gihad C.

Sorry I'm still not quite clear on this; what would you effectively be asking the compiler/processor to do if you just passed endptr instead of &endptr to strtol? If endptr was a simple char and we wanted to change its value, I understand you'd have to pass &endptr for its new value to be set to the char returned by the function. And ultimately we want endptr to point to a char, so why not declare "char endptr" instead of "char* endptr" and pass its address "&endptr"? will this not also result in the function changing the value of endptr to the char at the end of the input string?


SVG not supported

Login to reply

👍?
helpful
3:46pm Wed 7th Sep, Christopher M.

"Gihad Coorey" <23*9*7*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> Sorry I'm still not quite clear on this; what would you effectively be asking the compiler/processor to do if you just passed endptr instead of &endptr to strtol?
If you just pass endptr to the function, a copy of endptr will be passed (just the same as if you just passed an integer, or the result of any expression). If the function tried to modify that parameter, it would modify the *copy* of the original value, not the original value itself. When the function returned, the value of endptr would remain unchanged.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy