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.

Photo

Pass by address in C programming


  • Please log in to reply
2 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:19 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.

CODE:

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


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:19 AM

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;
multiply(a);

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

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:19 AM

Posted 21 February 2011 - 11:56 PM

I think I've got it.

#include<stdio.h>
 24 
 25 //GLOBAL DECLARATIONS
 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);
 32 
 
 53 
 54 int main(){
 55 
 56   int length;
 57   int width;
 58   int discount;
 59   float costSF;
 60   float installPrice;
 61   float subtotal;
 62   float total;
 63 
 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 }

 92 
 93 
 94 void getInput(int* length, int* width, int* discount, float* costSF){
 95 
 96   printf("\nEnter the length of the room: ");
 97   scanf("%d", length);
 98 
 99   printf("Enter the width of the room: ");
100   scanf("%d", width);
101 
102   printf("Enter the customer discount: ");
103   scanf("%d", discount);
104 
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