2

I am using a struct like:

struct{
    int a;
    char b[100];
}name;

I want to use static storage class specifier on name. How can I do that?

1
  • I have added it as an answer, we shall not answer a question with a comment. Commented May 15, 2019 at 12:12

2 Answers 2

3

use the word static in front of it:

static struct{
    int a;
    char b[100];
} name;

This will declare a variable named name with the type struct { ... } and the storage class specifier static.

To initialize the member of the struct, you can use

static struct{
    int a;
    char b[100];
} name = {5};
Sign up to request clarification or add additional context in comments.

3 Comments

The declaration works, but when I use name like name.a = 1, it shows error saying unknown type name ‘name’. Also, I am doing this globally.
Cannot reproduce: ideone.com/M9KatR Can you make a full example?
name.a = 5; can only be in a function. Do you want to initialize it?
1

If you want to initialize a structure members having static storage in file scope, you can do something like

static struct{
    int a;
    char b[100];
} name = {.a=5, .b = "test1"};

Comments

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.