Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Pass by address in C programming

  • Please log in to reply
2 replies to this topic

#1 altair05


  • Members
  • 110 posts
  • Gender:Male
  • Local time:12:23 AM

Posted 21 February 2011 - 02:46 PM

I need some guidance on how to take a variable from one function and put it in another variable in another function. I have two functions, main and getInput.

Main calls the getInput function and getInput collects the data and stores it in 4 variables: length, width, discount and cost.

I want to pass the numbers from getInput into the main without declaring these variables as global since that's what my professor wants me to do.
This is only part of the homework so it really doesn't do anything yet.


1 #include<stdio.h>
  3 void getInput(void);
  7 int main(){
  9 int length;
 10 int width;
 11 int discount;
 12 float cost;
 14 getInput();
 16 printf("\nLength is = %d", length);
 19 return(0);
 20 }
 22 void getInput(void){
 24   int length;
 25   int width;
 26   int discount;
 27   float cost;
 29   printf("Enter the length of the room: ");
 30   scanf("%d", &length);
 32   printf("Enter the width of the room: ");
 33   scanf("%d", &width);
 35   printf("Enter the customer discount: ");
 36   scanf("%d", &discount);
 38   printf("Enter the cost per square foot: ");
 39   scanf("%f", &cost);
 40 }

BC AdBot (Login to Remove)


#2 groovicus


  • Security Colleague
  • 9,963 posts
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:23 PM

Posted 21 February 2011 - 07:10 PM

I don't want to give this away exactly because this is an important lesson, but here is a basic example using syntax from a different language that is not similar to c.
int a = 1;

function multiply(int a)
   print a*a;

Basically, you have some value that you want to pass to a function. You then need to have a function that is going to take that type of input. I called it int a, but I could have called it int foo. In my example, I am just printing out the result of the multiplication operation. However if I wanted to, I could gave returned the value instead, like this:
int a = 1;
int b = multiply(a);
print b;

function multiply(int a)
   return a*a;

Functions are just black boxes. You feed them an input, they provide an output more or less. You don't necessarily have to provide an input to a function. The important thing is that your function signature matches your input. Here is an example using a function with the same name, but different signatures:
function multiply( int a )

function multiply (int a, int <img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />

This is a very simple lesson once you wrap your head around it. The first thing I would do if I were you would be to write a simple program that declares a single integer value, and then passes it to a multiplication function, which then prints out the result. (just like the first example).

Edited by groovicus, 21 February 2011 - 07:11 PM.

#3 altair05

  • Topic Starter

  • Members
  • 110 posts
  • Gender:Male
  • Local time:12:23 AM

Posted 21 February 2011 - 11:56 PM

I think I've got it.

 26 void getInput(int*, int*, int*, float*);
 27 float calcInstallPrice(int, int, float);
 28 float calcSubtotal(float, int);
 29 float calcTotal(float);
 30 void printMeasure(int, int);
 31 void printCharges(int, int, float, float, int, float, float);
 54 int main(){
 56   int length;
 57   int width;
 58   int discount;
 59   float costSF;
 60   float installPrice;
 61   float subtotal;
 62   float total;
 64   getInput(&length, &width, &discount, &costSF);
 65   installPrice = calcInstallPrice(length, width, costSF);
 66   subtotal = calcSubtotal(installPrice, discount);
 67   total = calcTotal(subtotal);
 68   printMeasure(length, width);
 69   printCharges(length, width, costSF, installPrice, discount, subtotal, total);
 70   return(0);
 71 }

 94 void getInput(int* length, int* width, int* discount, float* costSF){
 96   printf("\nEnter the length of the room: ");
 97   scanf("%d", length);
 99   printf("Enter the width of the room: ");
100   scanf("%d", width);
102   printf("Enter the customer discount: ");
103   scanf("%d", discount);
105   printf("Enter the cost per square foot: ");
106   scanf("%f", costSF);
107 }

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users