0

Hey so I'm simply trying to define a struct. I'm probably being an idiot, but hey, I'm trying.

Doing this:

struct Neuron{
    float mu;
    float stim[10];
    float hist[10];
    int ns[10000];
    float st[10000];
    float cup[8][10];

};


struct Neuron nur1;


nur1.mu = -0.7;

Getting this:

error: unknown type name 'nur1'

I do not understand why this is. It's all in the same .c file. Maybe compilation issues? Simply using gcc my_file.c on mac OS X. <3 <3

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
H X
  • 43
  • 1
  • 7
  • Don't use `gcc my_file.c` if you are a begginer, use `gcc -Wall -Werror my_file.c`. – Iharob Al Asimi Jun 29 '15 at 20:50
  • 6
    `nur1.mu = -0.7;` cannot be performed from the global scope. – mah Jun 29 '15 at 20:51
  • 2
    You need to post more code, minimum the `main()` function. – Iharob Al Asimi Jun 29 '15 at 20:51
  • Ditto all the above. Works fine within an actual function: https://ideone.com/NAUcTc – Paul Roub Jun 29 '15 at 20:51
  • 1
    If that's your entire code, you need to put everything other than the struct declaration inside of a function (`main()` in this case). – Ricky Mutschlechner Jun 29 '15 at 20:53
  • 2
    I don't think posting more code is necessary. The error he addresses is the exact first error gcc on a Mac gives when what he posted is truly the full file contents. (Second error is error: expected identifier or '(', strangely enough. – mah Jun 29 '15 at 20:53
  • 1
    You are leaving out something. Please post the complete, exact code. – Jiminion Jun 29 '15 at 20:54
  • Thanks for the advice. It'd be really tight if I could declare these at global scope, is there anyway this is possible? If not, why not? (for curiosities sake). I ask because I'm trying to rewrite a python/cython script I have written, and would not like to spend any time restructuring my actual code, as it gets a little hairy. – H X Jun 29 '15 at 20:56
  • C programs need, at minimum, a `main()` function. Period. – Paul Roub Jun 29 '15 at 20:57
  • 1
    @HX Well, I tried to help you, but apparently my answer was not useful. Whatever. You can use designated initializers and do `struct Neuron nur1 = { .mu = -0.7 };`, but of course, you always need a `main()` function. Just thought I'd leave this here because it's probably what you're looking for. – Filipe Gonçalves Jun 29 '15 at 20:58
  • Haha, yup I saw your comment, I was just looking for other shortcuts as well, thanks man! – H X Jun 29 '15 at 21:00

3 Answers3

2

You can't just say nur1.mu = -0.7 like that. Try putting it in a function (maybe your main function):

int main()
{
nur1.mu = -0.7;
}
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Another option, since the OP is using GCC, is to use its (non-standard, non-portable) _constructor_ attribute on a function of any name; that function will get called before `main()` to perform whatever initialization it needs. http://stackoverflow.com/questions/8433484/c-static-initialization-vs-attribute-constructor has some details. – mah Jun 29 '15 at 21:00
1

You need to have a main function; otherwise the complier doesn't know where to look for the start of the first function. The struct is declared outside of main() with everything else inside, like this:

struct Neuron{
    float mu;
    float stim[10];
    float hist[10];
    int ns[10000];
    float st[10000];
    float cup[8][10];
};

int main()
{

struct Neuron nur1;

    nur1.mu = -0.7;
    printf("%f\n", nur1.mu);

}
Ulrick
  • 33
  • 7
0

If it's just to initialize the struct properly, you can use an initializer. This is allowed at the file-level (using only a constant expression), or for a local variable.

struct Neuron nur1 = {
    .mu = -0.7;
};

The syntax like .mu is called designated initializer (C99).

Note this will be performed once during program startup and will also initialize all not explicitly initialized fields to 0 as for any global object (see C11 6.7.9p10).

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • Not "all bits zero", but as if initialized by the expression `0`. For example pointers become null, and doubles become `0.0`, even if those values are not represented by all-bits-zero – M.M Jun 29 '15 at 23:19