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;
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
|
show 3 more comments
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
2
what do you think you need the pointer for? what doesmultiChoice
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 arelist[0]
,list[1]
,list[2]
,list[3]
andlist[4]
, right, like count from 0 to 4, or count from 0 until youlist[i]
isNULL
. I ask again: what doesmultiChoice
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. makei
an integer starting at 0, and usepointer[i]
instead of*i
). But the pointer syntax is idiomatic C.
– interjay
Mar 24 at 16:37
|
show 3 more comments
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
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
c pointers
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 doesmultiChoice
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 arelist[0]
,list[1]
,list[2]
,list[3]
andlist[4]
, right, like count from 0 to 4, or count from 0 until youlist[i]
isNULL
. I ask again: what doesmultiChoice
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. makei
an integer starting at 0, and usepointer[i]
instead of*i
). But the pointer syntax is idiomatic C.
– interjay
Mar 24 at 16:37
|
show 3 more comments
2
what do you think you need the pointer for? what doesmultiChoice
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 arelist[0]
,list[1]
,list[2]
,list[3]
andlist[4]
, right, like count from 0 to 4, or count from 0 until youlist[i]
isNULL
. I ask again: what doesmultiChoice
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. makei
an integer starting at 0, and usepointer[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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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]);
Your version can only access consecutive elements oflist
, 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
|
show 7 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]);
Your version can only access consecutive elements oflist
, 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
|
show 7 more comments
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]);
Your version can only access consecutive elements oflist
, 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
|
show 7 more comments
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]);
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]);
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 oflist
, 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
|
show 7 more comments
Your version can only access consecutive elements oflist
, 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
|
show 7 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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]
andlist[4]
, right, like count from 0 to 4, or count from 0 until youlist[i]
isNULL
. I ask again: what doesmultiChoice
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 usepointer[i]
instead of*i
). But the pointer syntax is idiomatic C.– interjay
Mar 24 at 16:37