Any way to avoid ugly pointers in CWhat are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?How do function pointers in C work?C pointer to array/array of pointers disambiguationproblem with flushing input stream CPointer to a casted Pointer?Typedef function pointer?What does “dereferencing” a pointer mean?Why should I use a pointer rather than the object itself?C Pass arguments as void-pointer-list to imported function from LoadLibrary()

What should the arbiter and what should have I done in this case?

How to tell your grandparent to not come to fetch you with their car?

What makes Ada the language of choice for the ISS's safety-critical systems?

Trapping Rain Water

PhD - Well known professor or well known school?

Should an arbiter claim draw at a K+R vs K+R endgame?

When 2-pentene reacts with HBr, what will be the major product?

Cross-dimension teleportation using command block or datapack?

At what point in time did Dumbledore ask Snape for this favor?

Why doesn't Adrian Toomes give up Spider-Man's identity?

Is using haveibeenpwned to validate password strength rational?

What can plausibly explain many of my very long and low-tech bridges?

BGP multihome issue

Using "subway" as name for London Underground?

Hottest Possible Hydrogen-Fusing Stars

Was Jesus good at singing?

Was the output of the C64 SID chip 8 bit sound?

Can a user sell my software (MIT license) without modification?

The eyes have it

Implement Homestuck's Catenative Doomsday Dice Cascader

How to project 3d image in the planes xy, xz, yz?

How to chain Python function calls so the behaviour is as follows

Using a found spellbook as a Sorcerer-Wizard multiclass

How does an ordinary object become radioactive?



Any way to avoid ugly pointers in C


What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?How do function pointers in C work?C pointer to array/array of pointers disambiguationproblem with flushing input stream CPointer to a casted Pointer?Typedef function pointer?What does “dereferencing” a pointer mean?Why should I use a pointer rather than the object itself?C Pass arguments as void-pointer-list to imported function from LoadLibrary()






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-3















I'm fairly new to C in general but have a basic code knowlege. i'm having a tough time getting pointers into my head and was wondering if anybody has any good way of thinking of them (like getting '*' and '&' the right way around). I'm trying to make a header file for console inputs and in doing so am returning a pointer to an array of answers. I want to make the header file as easy to use for the end user.



This is what I have and it works but is there a way of eliminating the pointer (*) in printf("%sn", list[*i]); by modifying the for loop attributes?



 int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int *i = pointer; *i >= 0; i++)

printf("%sn", list[*i]);



Final Solution:



#include "inquire.h"

int main(int argc, char *argv[])

int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int i = 0; pointer[i] >= 0; i++)

printf("%sn", list[pointer[i]]);











share|improve this question



















  • 2





    what do you think you need the pointer for? what does multiChoice do?

    – Antti Haapala
    Mar 24 at 16:20











  • @AnttiHaapala MultiChoice returns a pointer to an array of answers

    – hamish sams
    Mar 24 at 16:21











  • But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

    – Antti Haapala
    Mar 24 at 16:23







  • 1





    I find this question hard to answer, because I think pointers are rather beautiful :)

    – Josh Greifer
    Mar 24 at 16:24







  • 1





    I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

    – interjay
    Mar 24 at 16:37


















-3















I'm fairly new to C in general but have a basic code knowlege. i'm having a tough time getting pointers into my head and was wondering if anybody has any good way of thinking of them (like getting '*' and '&' the right way around). I'm trying to make a header file for console inputs and in doing so am returning a pointer to an array of answers. I want to make the header file as easy to use for the end user.



This is what I have and it works but is there a way of eliminating the pointer (*) in printf("%sn", list[*i]); by modifying the for loop attributes?



 int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int *i = pointer; *i >= 0; i++)

printf("%sn", list[*i]);



Final Solution:



#include "inquire.h"

int main(int argc, char *argv[])

int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int i = 0; pointer[i] >= 0; i++)

printf("%sn", list[pointer[i]]);











share|improve this question



















  • 2





    what do you think you need the pointer for? what does multiChoice do?

    – Antti Haapala
    Mar 24 at 16:20











  • @AnttiHaapala MultiChoice returns a pointer to an array of answers

    – hamish sams
    Mar 24 at 16:21











  • But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

    – Antti Haapala
    Mar 24 at 16:23







  • 1





    I find this question hard to answer, because I think pointers are rather beautiful :)

    – Josh Greifer
    Mar 24 at 16:24







  • 1





    I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

    – interjay
    Mar 24 at 16:37














-3












-3








-3


0






I'm fairly new to C in general but have a basic code knowlege. i'm having a tough time getting pointers into my head and was wondering if anybody has any good way of thinking of them (like getting '*' and '&' the right way around). I'm trying to make a header file for console inputs and in doing so am returning a pointer to an array of answers. I want to make the header file as easy to use for the end user.



This is what I have and it works but is there a way of eliminating the pointer (*) in printf("%sn", list[*i]); by modifying the for loop attributes?



 int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int *i = pointer; *i >= 0; i++)

printf("%sn", list[*i]);



Final Solution:



#include "inquire.h"

int main(int argc, char *argv[])

int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int i = 0; pointer[i] >= 0; i++)

printf("%sn", list[pointer[i]]);











share|improve this question
















I'm fairly new to C in general but have a basic code knowlege. i'm having a tough time getting pointers into my head and was wondering if anybody has any good way of thinking of them (like getting '*' and '&' the right way around). I'm trying to make a header file for console inputs and in doing so am returning a pointer to an array of answers. I want to make the header file as easy to use for the end user.



This is what I have and it works but is there a way of eliminating the pointer (*) in printf("%sn", list[*i]); by modifying the for loop attributes?



 int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int *i = pointer; *i >= 0; i++)

printf("%sn", list[*i]);



Final Solution:



#include "inquire.h"

int main(int argc, char *argv[])

int *pointer;
char *list[] = "Hello1", "Hello2", "Hello3", "Hello4", "Hello5", NULL;

pointer = multiChoice(list);

printf("Choices:n");
for (int i = 0; pointer[i] >= 0; i++)

printf("%sn", list[pointer[i]]);








c pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 10:09







hamish sams

















asked Mar 24 at 16:17









hamish samshamish sams

606




606







  • 2





    what do you think you need the pointer for? what does multiChoice do?

    – Antti Haapala
    Mar 24 at 16:20











  • @AnttiHaapala MultiChoice returns a pointer to an array of answers

    – hamish sams
    Mar 24 at 16:21











  • But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

    – Antti Haapala
    Mar 24 at 16:23







  • 1





    I find this question hard to answer, because I think pointers are rather beautiful :)

    – Josh Greifer
    Mar 24 at 16:24







  • 1





    I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

    – interjay
    Mar 24 at 16:37













  • 2





    what do you think you need the pointer for? what does multiChoice do?

    – Antti Haapala
    Mar 24 at 16:20











  • @AnttiHaapala MultiChoice returns a pointer to an array of answers

    – hamish sams
    Mar 24 at 16:21











  • But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

    – Antti Haapala
    Mar 24 at 16:23







  • 1





    I find this question hard to answer, because I think pointers are rather beautiful :)

    – Josh Greifer
    Mar 24 at 16:24







  • 1





    I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

    – interjay
    Mar 24 at 16:37








2




2





what do you think you need the pointer for? what does multiChoice do?

– Antti Haapala
Mar 24 at 16:20





what do you think you need the pointer for? what does multiChoice do?

– Antti Haapala
Mar 24 at 16:20













@AnttiHaapala MultiChoice returns a pointer to an array of answers

– hamish sams
Mar 24 at 16:21





@AnttiHaapala MultiChoice returns a pointer to an array of answers

– hamish sams
Mar 24 at 16:21













But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

– Antti Haapala
Mar 24 at 16:23






But the choices are list[0], list[1], list[2], list[3] and list[4], right, like count from 0 to 4, or count from 0 until you list[i] is NULL. I ask again: what does multiChoice do, please provide the code!

– Antti Haapala
Mar 24 at 16:23





1




1





I find this question hard to answer, because I think pointers are rather beautiful :)

– Josh Greifer
Mar 24 at 16:24






I find this question hard to answer, because I think pointers are rather beautiful :)

– Josh Greifer
Mar 24 at 16:24





1




1





I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

– interjay
Mar 24 at 16:37






I suppose you can use array-like syntax instead of pointer arithmetic and dereferencing (i.e. make i an integer starting at 0, and use pointer[i] instead of *i). But the pointer syntax is idiomatic C.

– interjay
Mar 24 at 16:37













1 Answer
1






active

oldest

votes


















-5














As you might know, pointers store addresses of, for example, variables and functions. The '*' operator is used for two different operations in this case. One in declaring a pointer, as you did with pointer. The other operation is dereferencing, which returns the value stored at the address the pointer points to.



I don't know what multiChoice() does, so I can not eliminate the pointer pointer variable. But I would, in fact, rewrite in initialization if i, because of simplicity. It is shorter and more readable to dereference pointer once, instead of dereferencing i three times.



for (int i = *pointer; i >= 0; i++)

printf("%sn", list[i]);






share|improve this answer

























  • Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

    – interjay
    Mar 24 at 16:30












  • you force the chosen indexes to be consecutive

    – bruno
    Mar 24 at 16:30











  • That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

    – Martin Pekár
    Mar 24 at 16:31






  • 3





    Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

    – interjay
    Mar 24 at 17:19






  • 2





    Not semantically identical to the original.

    – Clifford
    Mar 24 at 18:23












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325848%2fany-way-to-avoid-ugly-pointers-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-5














As you might know, pointers store addresses of, for example, variables and functions. The '*' operator is used for two different operations in this case. One in declaring a pointer, as you did with pointer. The other operation is dereferencing, which returns the value stored at the address the pointer points to.



I don't know what multiChoice() does, so I can not eliminate the pointer pointer variable. But I would, in fact, rewrite in initialization if i, because of simplicity. It is shorter and more readable to dereference pointer once, instead of dereferencing i three times.



for (int i = *pointer; i >= 0; i++)

printf("%sn", list[i]);






share|improve this answer

























  • Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

    – interjay
    Mar 24 at 16:30












  • you force the chosen indexes to be consecutive

    – bruno
    Mar 24 at 16:30











  • That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

    – Martin Pekár
    Mar 24 at 16:31






  • 3





    Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

    – interjay
    Mar 24 at 17:19






  • 2





    Not semantically identical to the original.

    – Clifford
    Mar 24 at 18:23
















-5














As you might know, pointers store addresses of, for example, variables and functions. The '*' operator is used for two different operations in this case. One in declaring a pointer, as you did with pointer. The other operation is dereferencing, which returns the value stored at the address the pointer points to.



I don't know what multiChoice() does, so I can not eliminate the pointer pointer variable. But I would, in fact, rewrite in initialization if i, because of simplicity. It is shorter and more readable to dereference pointer once, instead of dereferencing i three times.



for (int i = *pointer; i >= 0; i++)

printf("%sn", list[i]);






share|improve this answer

























  • Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

    – interjay
    Mar 24 at 16:30












  • you force the chosen indexes to be consecutive

    – bruno
    Mar 24 at 16:30











  • That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

    – Martin Pekár
    Mar 24 at 16:31






  • 3





    Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

    – interjay
    Mar 24 at 17:19






  • 2





    Not semantically identical to the original.

    – Clifford
    Mar 24 at 18:23














-5












-5








-5







As you might know, pointers store addresses of, for example, variables and functions. The '*' operator is used for two different operations in this case. One in declaring a pointer, as you did with pointer. The other operation is dereferencing, which returns the value stored at the address the pointer points to.



I don't know what multiChoice() does, so I can not eliminate the pointer pointer variable. But I would, in fact, rewrite in initialization if i, because of simplicity. It is shorter and more readable to dereference pointer once, instead of dereferencing i three times.



for (int i = *pointer; i >= 0; i++)

printf("%sn", list[i]);






share|improve this answer















As you might know, pointers store addresses of, for example, variables and functions. The '*' operator is used for two different operations in this case. One in declaring a pointer, as you did with pointer. The other operation is dereferencing, which returns the value stored at the address the pointer points to.



I don't know what multiChoice() does, so I can not eliminate the pointer pointer variable. But I would, in fact, rewrite in initialization if i, because of simplicity. It is shorter and more readable to dereference pointer once, instead of dereferencing i three times.



for (int i = *pointer; i >= 0; i++)

printf("%sn", list[i]);







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 16:30

























answered Mar 24 at 16:28









Martin PekárMartin Pekár

274




274












  • Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

    – interjay
    Mar 24 at 16:30












  • you force the chosen indexes to be consecutive

    – bruno
    Mar 24 at 16:30











  • That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

    – Martin Pekár
    Mar 24 at 16:31






  • 3





    Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

    – interjay
    Mar 24 at 17:19






  • 2





    Not semantically identical to the original.

    – Clifford
    Mar 24 at 18:23


















  • Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

    – interjay
    Mar 24 at 16:30












  • you force the chosen indexes to be consecutive

    – bruno
    Mar 24 at 16:30











  • That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

    – Martin Pekár
    Mar 24 at 16:31






  • 3





    Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

    – interjay
    Mar 24 at 17:19






  • 2





    Not semantically identical to the original.

    – Clifford
    Mar 24 at 18:23

















Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

– interjay
Mar 24 at 16:30






Your version can only access consecutive elements of list, but OP's version can access any element (skipping over some). Also, your loop will never terminate.

– interjay
Mar 24 at 16:30














you force the chosen indexes to be consecutive

– bruno
Mar 24 at 16:30





you force the chosen indexes to be consecutive

– bruno
Mar 24 at 16:30













That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

– Martin Pekár
Mar 24 at 16:31





That's on purpose. I didn't want to fix the errors, just decreasing the amount of pointers.

– Martin Pekár
Mar 24 at 16:31




3




3





Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

– interjay
Mar 24 at 17:19





Seriously? Obviously the point was to remove pointers without changing the functionality of the code or introducing bugs. Your answer is about as useless as telling him to delete all his code, as that will also get rid of the pointers.

– interjay
Mar 24 at 17:19




2




2





Not semantically identical to the original.

– Clifford
Mar 24 at 18:23






Not semantically identical to the original.

– Clifford
Mar 24 at 18:23




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325848%2fany-way-to-avoid-ugly-pointers-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript