![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
pointers are difficult
before i start this question i wanna say thanks for all the helpful comments about my switch statement post. i realized however, that in that case my problem was also due to incorrect pointer use. atleast thats what the teacher tells me. the switch statement itself was fine. but im still having a lot of trouble with pointers, i think.
i have this program which requires three different colors to be inputed, each color represents a number, the first two are two digits in a two digit number, and the last one a power of ten. i understand what i need to do but i cant get a proper output from a function. you see, i started practicing using pointers before i started the rest of the program, and all i want to do is have the program print the scanned variables on the screen. in order to get the hang of it i put the same printf statement inside and outside of the function. at one point the inside one worked, but the outside one didnt, leading me to believe my pointers were wrong. then, after some fiddling, i cant get either to work. heres my code. when you input "black brown red" you should get 0, 1, 2. i appreciate any help. Quote:
|
|
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
char *colors[10][7] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
//should be
const char *colors[10] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};You only need an array of char pointers to point to string literals not a 2d array of pointers. Its also common to mark pointers as const when they point to string literals as string literals will normaly be placed in read-only memmory. printf("The numbers should be %d, %d, %d.\n", &digit1, &digit2, &digit3);
printf("The numbers should be %d, %d, %d.\n", &dig1, &dig2, &dig3);
//should be:
printf("The numbers should be %d, %d, %d.\n", digit1, digit2, digit3);
printf("The numbers should be %d, %d, %d.\n", *dig1, *dig2, *dig3);printf expects its arguments by value so you need to dereference a pointer and do nothing to the int values. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
thank you so much. i been racking my brain trying to figure that out. the damned TA's that are supposed to help the students give these really crappy "hints" then tell us "we wont give you the answer." you are a life saver, now i can finish my program and die happy...ok well maybe without the dying part.
in all honesty though, thank you. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|