1
#include <stdio.h>
#include <math.h>
#define Hey {0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648,0.9883}
float average(float Hello[]){
    int i;
    float sum;
        for (i=0; i<100;i++) {
            sum+= Hello[i];

    }
    return sum/100;
}
int main(){
    printf("%f\n",average(Hey));

//so here the compiler says that expected error without giving me what the error actually is. and i suspect because of the bad definition of the vector that i have at the very beginning of my code. }

Yeah, so as i stated in my comment the problem is with the c preprocesses as its referred to here in stack overflow. is my predefinition for the array Hey wrong? and why?

12
  • Add a cast: average((float[])Hey). Also, initialize your variables and close your }s. Commented Nov 16, 2015 at 11:11
  • @EOF that won't work. the initializer will work just when initializing a variable. It's C not C++ and you need to initialize a variable, then pass a pointer of it to the function. You cannot make an array like that Commented Nov 16, 2015 at 11:19
  • @Zorgatone: Well, it certainly works on gcc. Is this a gcc extension? I dunno. Commented Nov 16, 2015 at 11:23
  • 1
    @Zorgatone: Well, a bit of testing reveals that it works with -std=c99, but not with -std=c89 (both with -Wpedantic), so I guess you could start looking at the C99 standard for the answer. Commented Nov 16, 2015 at 11:26
  • 1
    @EOF: Something like (int []){ 1,2,3,4} is a compound literal (standard since C99). The (int []) part is not a cast, but part of the construct and tells the compiler the type of the part in the braces. Commented Nov 16, 2015 at 11:42

3 Answers 3

2

Usually this is how to predefine an array in C, omit the size and provide the values in brackets. Eg,

const float Hey[] = {0.9501, 0.2311, 0.6068 };

Sign up to request clarification or add additional context in comments.

5 Comments

Maybe add a const so the semantics are closer to what the OP tried to achieve with #define
that also could be a global if you compare it to define
A small hiccup is that this can't occur in a header file (unless you make it static also)
@M.M: You can declare it in the header and define in the corresponding implementation file. As it is standard in C.
@Olaf I mean the code as posted by artm can't occur in the header.
1

If you really want to, you can do something like this.

#include <stdio.h>
#include <math.h>

#define HEY_INIT { 0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648, 0.9883 }

static const float Hey[] = HEY_INIT; // global array

float average(float Hello[]) {
    int i;
    float sum;

    for (i = 0; i < 100; i++) {
        sum += Hello[i];
    }

    return sum / 100;
}

int main(void) {
    printf("%f\n", average(Hey));

    return 0;
}

6 Comments

That makes the macro hyperfluid (aka more than superfluous). One could also say it is nonsense and contraproductive (never use a macro if other constructs do the same job).
that's just if he really want to keep that a macro. ie. he could make more arrays like that elsewhere
What would that be good for? There is no need for another such array, as he can reference the single one. More arrays are (possibly) just more memory consumption.
I know it's useless in my code. But my assumption was that @reddevil wanted probably to use a define for some reason
@Zorgatone Man, your solution is what I was looking for! Thank you very much stranger! I guess not working with defines that often really makes me a noob with them. -.-
|
-1

example: an array containing 4 integer values of type int called abc could be represented as: int abc [4]; by default, regular arrays of local scope (in this example, those declared inside the function) are ignored. But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example: int abc [4] = { 1, 2, 47, 50, -371 }; in other case you can let empty the brackets so you can initialize them when the program is running e.g. int abc [] ; or you can use a matrix like :int abc [] []; with X lines & Y columns.

3 Comments

You cannot define a variable with an incomplete type.
i couldn't agree more, but i didn't meant to show on how you have to complete a type, i was referring to arrays in general.
"... in other case you can let empty the brackets so you can initialize them when the program is running e.g. int abc [] ; or you can use a matrix like :int abc [] []; with X lines & Y columns."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.