Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing char by reference 3

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
CA
When I compile the below program I get no errors or warnings; hever, when I run it, my output isn't as it should be. I know that the problem is passing the char taxcat because when I take it out, the output is fine. What am I doing wrong?

#include <stdio.h>

void readData (int*, char*, int*, int*);

main () {
int prodnum, quantity, price;
char taxcat;


printf(&quot;Please give PRODUCT NUMBER, TAX CATEGORY, QUANTITY and PRICE :\n&quot;);

readData (&prodnum, &taxcat, &quantity, &price);

printf (&quot;Product Number: %d\nTax Category: %c\nQuantity: %d\nPrice: $%d\n\n&quot;, prodnum, taxcat, quantity, price);
printf (&quot;Product Number: %d\nTax Category: %c\nQuantity: %d\nPrice: %d\n\n&quot;, &prodnum, &taxcat, &quantity, &price);

}

void readData (int *prodnum, char *taxcat, int *quantity, int *price){
scanf(&quot;%d%c%d%d&quot;, *&prodnum, *&taxcat, *&quantity, *&price);
}
 
try 'c'
char taxcat[MAXBUFF];
or
char *taxcat; /* and alloc it */
....
readData (&prodnum, taxcat, &quantity, &price);
....
/* they are allready pointers...*/
scanf(&quot;%d%s%d%d&quot;, prodnum, taxcat, quantity, price);
vox clamantis in deserto.
 
So are char variables always pointers? Why are they different?
 
You don't need to change the 'char' to an array.
The problem as Jamisar pointed out was to do with
the scanf statement. The *& was not correct.

By the way scanf scans for input as written.
so if you write:
scanf(&quot;%d%s%d%d&quot;, prodnum, taxcat, quantity, price);
Scanf expects four inputs together with no seperation!
e.g 10c58 --- how would the third and fourth input
be separated.
Much better something like:

scanf(&quot;%d,%s,%d,%d&quot;, prodnum, taxcat, quantity, price); //items entered seperated by a comma.
OR
scanf(&quot;%d %s %d %d&quot;, prodnum, taxcat, quantity, price);//items entered separated by a space
 
sure guestgulkan, you are rigth. vox clamantis in deserto.
 
Hi,

Just wanted to clarify something. char variables are NOT always pointers.

char c;
char *pc;


The first is a char and the second is a pointer-to-char. They have different sizes and react differently when passed as parameters to functions. If you pass the regular char as a char to the function when you return the value will not stick. That is why you must pass the address of a regular char variable to the function...

void alterCharFunction(char * c); /*declartion*/

char c;
alterCharFunction(&c);


In the next case, you declare a pointer-to-char and pass it to alter it...

void alterCharFunction(char * c); /*declartion*/

char *pc = (char*)malloc(1);
alterCharFunction(pc);


I just wanted to make sure you saw the differnce. An array of characters is also different, but that is for another discussion.

-Tyler

 
OK gonzilla, do you really hope TFfan (see his first code ) will|can undestand this ?? vox clamantis in deserto.
 
While it is true, I am still learning C, I do understand gonzilla's explanation and code. Call by reference was just introduced to me on Wednesday, so I'm am still a little unsure about the syntax. I was told to use a '*' in the prototype and an '&' in the function call.

What I am confused about is why the below program passes taxcat to main.

I know the program doesn't quite work (there are logical problems), but the fact remains that taxcat gets passed.

#include <stdio.h>

void readData (int*, char*, int*, int*);
void calTotal (float, float);
float addTax (float);

main () {
int prodnum, quantity, price;
float cost;
char taxcat[1];


printf(&quot;Please give PRODUCT NUMBER, TAX CATEGORY, QUANTITY and PRICE :\n&quot;);

readData (&prodnum, taxcat, &quantity, &price);

calTotal (quantity, price);

printf (&quot;Product Number: %d\nTax Category: %s\nQuantity: %d\nPrice: $%2.2f\n\n&quot;, prodnum, taxcat, quantity, cost);
printf (&quot;Product Number: %d\nTax Category: %s\nQuantity: %d\nPrice: %f\n\n&quot;, &prodnum, &taxcat, &quantity, &cost);

}

void readData (int *prodnum, char taxcat[1], int *quantity, int *price){
scanf(&quot;%d %s %d %d&quot;, prodnum, taxcat, quantity, price);
}
void calTotal (float q, float p){
float cost=q*p;
cost=addTax(cost);
}
float addTax (float cost){
float tax=.15;
tax=cost*tax;
cost=cost+tax;
return cost;}
 
Btw, I have since managed to pass cost to main, but when I try to print the address of cost, I get 0.00000.
 
SORRY, TFfan i was a little hard.

i believe the answer should be at the same level as the question.
a guru-answer to a beginner-question does not help the beginner, it's just confusing.
c is a beautiful language, and (like all other) will be learned.
i started reading k&r bible, programming c, can i propose to do the same ?
you will solve 90% your problems by (learning) yourself, post the rest!!
tanks. vox clamantis in deserto.
 
No need for an apology, I wasn't offended. I'm taking a C programming class at university. Unfortunately, my prof is a little inexperienced and can't really explain the concepts very well so I am left to learn on my own.

Perhaps I will pick up that book, but I had to buy a $100 text for the class and am hesitant to buy another.

I thought this was a place where beginners and experts can look for help. If you don't want to help beginners, then don't, if no one responds to my beginner questions, I'll get the point and go elsewhere.

I have helped beginners here in the flash, photoshop and HTML forums, I thought that was the point.
 
Hi TFfan,

Well, the idea of forum like this is for people to help others with problems they have. Although I do agree with jamisar that most things can be figured out by reading a book, I have found this forum to be very helpful in general. As long as you pose a question with some code you generally get some help.

I don't personally feel I am a guru at C yet...so I'll be glad to help you some more. :) Let me give you a suggestion. This was suggested to me when I was a beginner and it has really paid off.

If you are using an IDE like MS VC++, learn how to use the debugger to step through your code and set breakpoints. You can step into and out of code very easily once you get the hang of it. You can place watches on variables and see what is in them at almost any time. It is an invaluable tool (I have found) when something seems to be correct and but isn't. If you don't have an IDE, then doing that will be tough.

One tip before I go. You are delcaring &quot;float cost&quot; in main(). (BTW - you should be initalizing them...ie. float cost = 0.0;) Then you are displaying it at the end of main() and getting 0.0000. The problem is - you aren't actually changing the value of THAT cost anywhere in your program. The &quot;cost&quot; declaration you ARE changing is in calTotal(). A question for you to answer: can main's cost see calTotal's cost?

Hopefully, that will give you enough to get on with rest. Let me know if you still are getting stuck.

-Tyler
 
To put that another way, you have two variables named cost located in different areas of your program; just because they have the same name doesn't make them the same variable.


jamisar: gonzilla was making an important clarification about the difference between char and char*. First of all TFfan actually asked whether or not they were the same; gonzilla was well within his rights to answer him. Second, your answer implied that they were indeed the same, which is wrong; gonzilla was right in correcting you; just because TFfan is a beginner does not mean he deserves to be misled.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top