Pelles C forum

C language => Beginner questions => Topic started by: jwzumwalt on April 25, 2009, 04:33:32 AM

Title: Troubles with array function
Post by: jwzumwalt on April 25, 2009, 04:33:32 AM
here is the error I get when attempting to create a function that returns array data, what am I doing wrong?
Thanks - JZ

Building C:\outtput\test.obj.
C:\test.c(29): error #2168: Operands of + have incompatible types 'float (*)[10]' and 'float'.
C:\test.c(29): error #2144: Type error: pointer expected.
C:\test.c(29): error #2144: Type error: pointer expected.
*** Error code: 1 ***
Done.
                                                                             
                                                                                                 
#include <stdio.h>                                                                               
#include <math.h>                                                                                 
                                                                                                 
float atmosphere_data(float alt, float air);                                                     
                                                                                                 
/*Atmosphere Table (US units)                                                                                                                                                           
    alt    sigma   delta   theta   temp   press   dens       a       visc   k_visc    k_ratio
*/                                                                                               
float air_data[4][10] = {                                                                         
/* -1 */ {1.0296, 1.0367, 1.0069, 522.2, 2193.8, 0.0024472, 1120.3, 0.376, 1.54E-4,  7.30},       
/*  0 */ {1.0000, 1.0000, 1.0000, 518.7, 2116.2, 0.0023769, 1116.5, 0.374, 1.57E-4,  7.10},       
/*  1 */ {0.9711, 0.9644, 0.9931, 515.1, 2040.9, 0.0023081, 1112.6, 0.372, 1.61E-4,  6.91},       
/*  2 */ {0.0747, 0.0562, 0.7519, 390.0,  118.9, 0.0001777,  968.1, 0.297, 1.67E-3,  0.58}       
                            };                                                                   
                                                                                                 
void main()                                                                                       
{                                                                                                 
float temp;                                                                                       
                                                                                                 
temp = atmosphere_data(0.0, 5.0);                                                                 
   printf("\n\n\t Here is the value for sealevel temperature: %f", temp);                         
                                                                                                 
   printf("\n\n\t Press <Enter> key to exit... ");                                               
   while ((getchar()) != '\n'); printf("\n");                                                     
}                                                                                                 
                                                                                                 
float atmosphere_data(float alt, float air)                                                       
{                                                                                                 
return (air_data[alt+2][air]);        // <== line with errors                                                           
}                                                                                     
Title: Re: Troubles with array function
Post by: AlexN on April 25, 2009, 11:23:56 AM
Quote from: jwzumwalt on April 25, 2009, 04:33:32 AM
float atmosphere_data(float alt, float air)                                                       
{                                                                                                 
return (air_data[alt+2][air]);        // <== line with errors                                                           
}                                                                                     
The index of an array has always to be something like an integer or something that is directly convertable to an integer like char, long int, unsigned, enum...

So you have say to the compiler to convert the float to an integer.
f.e.: cast the type at the access "air_data[(int)alt+2][(int)air]
   or change the type of the function "float atmosphere_data(int alt, int air);"

both will work (I have testet it ;-)
Title: Re: Troubles with array function
Post by: jwzumwalt on April 25, 2009, 07:29:20 PM
Thank you, that worked!

I was unaware of some of the little hidden gatchas of c. I had tried to use int(alt+2) instead of (int). Why does c require the function and not the value to be in ()?
Title: Re: Troubles with array function
Post by: AlexN on April 26, 2009, 09:50:07 AM
Quote from: jwzumwalt on April 25, 2009, 07:29:20 PM
Thank you, that worked!

I was unaware of some of the little hidden gatchas of c. I had tried to use int(alt+2) instead of (int). Why does c require the function and not the value to be in ()?


With "(int)air" you tell the compiler to convert the type of air into an integer and if the compiler knows how to do this the compiler will do it. If he did not kown you will get an error message. It is not a function call it is a so called casting operation.

You use it (type)variable.

Title: Re: Troubles with array function
Post by: jwzumwalt on April 26, 2009, 07:08:46 PM
Thank you for the explanation.