Thread: Problems passing an array to a function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Problems passing an array to a function

    I am trying to pass a dynamically allocated array into a function to enter values. I have a function that allocates the array to the correct size and then that is passed into the function. My problem is that when the values are enter they don't seem to make it into the array. The program prints out a random number.
    Code:
    void readMatrix(double *mPtr, int size)
    {
    	int input = 0;
    	int count = 0;
    
    	printf("Please enter the values of your matrix row by row, left to right:\n");
    	do	//begin do/while
    	{
    		scanf("%f", &input);
    		mPtr[count] = input;
    		count++;
    	}while(count<size);	//end do/while
    
    }
    Whenever I try to print a value out of that do/while loop I get a number like 12486144843. I just want the inputs to make it into the array and be available outside of the function. This is passed by reference correct?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, never.

    C ALWAYS passes by copying. It JUST HAPPENS however, that when you copy a memory address (like an array name or a pointer), that THAT particular kind of a copy is still perfectly good for appearing to pass by reference. So it's acting like a pass by reference, but it's actually a pass by copying, same as every other parameter passed in C.

    Try that!

    Adak

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are trying to pass a float (%f) into an int (input), you don't want to do this.

    Other than that, at first glance, your array filling function appears to be reasonable. The code calling readMatrix(foo, size) will pass the address of your foo array to the function.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    I changed the int input to double input, but now all the functions seems to do is spit out zeros. I check what the input is by printing it right after it is entered and it says that it is zero. It is like the values are never making it into the variable input.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by ndenklau
    I changed the int input to double input, but now all the functions seems to do is spit out zeros. I check what the input is by printing it right after it is entered and it says that it is zero. It is like the values are never making it into the variable input.
    I said %f was float. A double is not the same as a float.

    In scanf, use %lf for a double, and %f for a float.

    In printf, use %f for both. It's inconsistent, but there's a reason for it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    That worked! Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM