Thread: Trouble with Call by Address

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question Trouble with Call by Address

    Hello,

    I'm trying to wirte a program but I'm having trouble with call by address. When I use the * in the function prototype and in the function itself and an & in the function call in main, I don't get an error, but the value of 0 is not passed from main to the function AddData. Can anyone tell me how to make this work and where I have made my mistake?

    Thanks,
    Vireyda

    Code:
    #include<stdio.h> //include standard input output functions
    
    //definitions section
    //structure for travel expense account
    struct TravelCosts
    	{
    	char employee_name[20];
    	int emp_num;
    	float distance_traveled;
    	float mileage_rate;
    	float meals;
    	float total_expenses;
    	};
    void AddData(struct TravelCosts expenses[], int *num);
    void PrintTravel(struct TravelCosts expenses[], int num);
    void ChangeCosts(struct TravelCosts expenses[], int num);
    void FindSpenders(struct TravelCosts expenses[], int num);
    
    void main()
    {
    	//declarations section
    	char response;
    	static struct TravelCosts travel[20];
    	int num_records = 0;
    
    	do
    	{
    	printf("Please select an action:\n\n");
    	printf("Add employee expenses\t\t- A or a\n"
    	       "Print expense database\t\t- P or p\n"
    	       "Change expense data\t\t- C or c\n"
    	       "Find over spending employees\t- F or f\n"
    	       "Quit Program\t\t\t- Q or q\n");
    	fflush(stdin); 
    	scanf("%c", &response);
    
    	//switch to case based on response value
    	switch(response)
    	{case 'A':case 'a': 
    		AddData(travel, &num_records);
    		break;
    	case 'P': case 'p':
    		PrintTravel(travel, num_records);
    		break;
    	case 'C': case 'c':
    		ChangeCosts(travel, num_records);
    		break;
    	case 'F': case 'f':
    		FindSpenders(travel, num_records);
    		break;
    	case 'Q': case 'q':
    		break;
    	default:
    		printf("\nThat selection is invaild.  Please try again.\n");
    	}//end switch
    	}while(response!='Q'&&response!='q');//end do
    }//end main
    
    void AddData(struct TravelCosts expenses[], int *num)
    	{
    	printf("%d", num\n);
    	}

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to dereference your pointer num in the function if you want the value stored there. Should be *num.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    be dereference do you mean that I should drop the int in the function? Is there a way I can do this with &'s instead?

    Thanks,
    Vireyda
    Last edited by Vireyda; 04-09-2004 at 05:38 PM.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Inside AddData, num is a pointer. To get the integer value that the pointer is pointing at, you must dereference the pointer by adding the * to it. For example:

    printf("%d\n", *num);

  5. #5
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Do I need to use *num if I want to read an input into that variable?

    Thanks,
    Vireyda

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It depends. If you are using scanf, which takes a pointer or the address of a variable, then just use num (because num is a pointer). If you are using some other method, like cin, that wants the int itself, then use *num.

    Remember that when declaring a variable, the '*' means that the variable is a pointer. When not declaring a variable, the '*' means go to the memory location pointed to by this variable. Normally, the variable is a pointer, so you are telling the code to look at the value at which it is pointing. Since scanf takes a pointer as an argument, you pass num by itself. Since printf wants the actual value, you tell it to go to the memory location at which num is pointing, by adding the '*' to it.

    Of course, num_records (in your main function) is just an int, it is not a pointer. So if you pass num_records to printf, you will not need the '*' - it already contains the value. If you pass num_records to scanf, you will need to pass a pointer to that value. That is why you use '&', the address of operator.

  7. #7
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I think I'm starting to understand, but I still feel very confused about pointers. I'm trying to use num in a for loop now, but once the new value is input by the user and read it crashes before it can do the loop. I would have thought that I would leave out the '*', but then i get an error while I compile.

    Thanks again,
    Vireyda


    [CODE]
    void AddData(struct TravelCosts expenses[], int *num)
    {
    //declaraions section
    int i;

    printf("How many travel claims are ther to be entered? ");
    scanf("%d", &num);

    for(i=0;i<=*num-1;++i)
    [/CODE}

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by jlou
    If you are using scanf, which takes a pointer or the address of a variable, then just use num (because num is a pointer).
    You put &num instead of num.

    Remember, '&' turns an int into a pointer to int. It turns a pointer to int into a pointer to a pointer to an int. Since scanf takes a pointer to an int, and num is a pointer to an int, all you need to do is pass in num.

    Also, just to be more clear, put parentheses around the *num in your for loop, so it is i <= (*num) - 1, or even better, use:

    Code:
    for(i=0;i<*num;++i)
    Last edited by jlou; 04-09-2004 at 07:05 PM.

  9. #9
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Okay, I made the reccommended change, but my program still crashes when I get to the for loop. I don't know what's going on here.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Paste your latest code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I'm not getting any errors when I compile the program or build it. It just crashes when I get to the for loop.

    Code:
    void AddData(struct TravelCosts expenses[], int *num)
    	{
    	//declaraions section
    	int i;
    
    	printf("How many travel claims are ther to be entered? ");
    	scanf("%d", &num);
    
    	for(i=0;i<*num;++i)
    		{
    		printf("/nPlease enter the following information:");
    		printf("Employee %d Name:  ", i);
    		gets(expenses[i].employee_name);
    		printf("Employee Number:  ");
    		scanf("%d", &expenses[i].emp_num);
    }
    Last edited by Vireyda; 04-10-2004 at 07:32 AM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%d", &num);
    Remove the &, num is already a pointer. And your bracing is off, but that's probably just a copying error.
    My best code is written with the delete key.

  13. #13
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Thanks so much. It works now. I get so confused about pointers.

    Vireyda

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Why are you passing num into the function as a pointer and returning nothing? If the idea is to pass num back to the calling routine, change this function to:
    Code:
    int AddData(struct TravelCosts expenses[])
    {
        int num;
        ...
        return num;
    }
    But if you don't need num in the calling routine use
    Code:
    void AddData(struct TravelCosts expenses[])
    {
        int num;
        ...
        return;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  4. Just finished assembly assignment
    By xddxogm3 in forum Tech Board
    Replies: 6
    Last Post: 10-08-2005, 04:01 PM
  5. I have the Memory Address of a function, how do I call it?
    By vulcan_146 in forum C++ Programming
    Replies: 8
    Last Post: 05-22-2005, 02:00 AM