Thread: Passing array into function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Passing array into function

    I am trying to write a program that will compute an angle and magnitude of a given complex number. I have therefore declared an array of 2 floats in which [0] stores the real part and [1] the imaginary part. My code looks as follows:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float magnitude(float c[]);
    
    void arc(float c, int *arcn);
    
    int main()
    {
    	float complex[2];
    	float owned;
    
    	printf("Enter real part > ");
    	scanf("%f", &complex[0]);
    	printf("Enter imaginary part > ");
    	scanf("%f", &complex[1]);
    
    	printf("Magnitude: %f", magnitude(complex));
    	arc(complex, &owned);
    	printf("Angle: %f", owned);
    }
    
    float magnitude(float c[])
    {
    	float temp;
    
    	temp = sqrt(c[0]*c[0] + c[1]*c[1]);
    
    	return temp;
    }
    
    void arc(float c[], float *arcn)
    {
    	*arcn = atan(c[1]/c[0]);
    }
    However I am getting an error:
    Code:
    main.c(19) : error C2440: 'function' : cannot convert from 'float [2]' to 'float'
    What is wrong with the program? Please help.

    I know I could use a return statement in the second function, but I do not want to. The whole purpose of this program is to excercise functions which return a value, and which manupulate values passed by a pointer.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You've defined arc() to accept a single float value as the first arg, not an array of floats.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    changed this line
    Code:
    void arc(float c, int *arcn);
    to
    Code:
    void arc(float *c, int *arcn);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to a function
    By mickpc in forum C Programming
    Replies: 1
    Last Post: 05-09-2010, 04:45 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM