Passing array into function

This is a discussion on Passing array into function within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that will compute an angle and magnitude of a given complex number. I ...

  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
    Katy, Texas
    Posts
    2,309
    You've defined arc() to accept a single float value as the first arg, not an array of floats.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21