Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 11th, 2005, 11:55 AM   #1
cjaime
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 cjaime is on a distinguished road
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:
#include<stdio.h>
#include<math.h>
#include<string.h>

/*prototype functions*/
void scan_data(const char *colors[], int *dig1, int *dig2, int *dig3);
int search(const char *arr[], const char *target, int n);


int
main(void)
{
char *colors[10][7] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
int digit1, digit2, digit3;

scan_data(colors, &digit1, &digit2, &digit3);
printf("The numbers should be %d, %d, %d.\n", &digit1, &digit2, &digit3);
return(0);
}

/*This program will get the colors*/
void scan_data(const char *colors[], int *dig1, int *dig2, int *dig3)
{
char color1[7], color2[7], color3[7];

printf("Enter the colors of the resistor's three bands, beginning with\n");
printf("the band nearest the end. Type the colors in lowercase letters\n");
printf("only, NO CAPS.\n");
scanf("%s%s%s", &color1, &color2, &color3);

*dig1 = search(colors, color1, 10);
*dig2 = search(colors, color2, 10);
*dig3 = search(colors, color3, 10);
printf("The numbers should be %d, %d, %d.\n", &dig1, &dig2, &dig3);
}
/*This program will turn the strings into numbers*/
int search(const char *arr[], const char *target, int n)
{
int i,
found = 0,
where;

i = 0;
while(!found && i < n){
if(strcmp(arr[i], target) == 0)
found = 1;
else
++i;
}

if(found)
where = i;
else
where = 10;
return(where);
}
cjaime is offline   Reply With Quote
Old Nov 11th, 2005, 1:52 PM   #2
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
 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.
Animatronic is offline   Reply With Quote
Old Nov 11th, 2005, 10:59 PM   #3
cjaime
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 cjaime is on a distinguished road
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.
cjaime is offline   Reply With Quote
Old Nov 12th, 2005, 12:26 AM   #4
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
http://www.daweidesigns.com/pointers.php

check out that site by a resident guru.
linuxpimp20 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC