2
int a;
scanf("%d",a);

typedef struct mylist {
    int info[a];
    struct mylist *link;

} Node;

this is my very simple struct and variable 'a' that don't work.

The thing which I want my program to do is to read input from the user and insert the digit into the struct (the user can determine the size of the array), however, I don't know how to do it since I get an error:

"variably modified ‘info’ at file scope".

I want to know whether it is possible to do this in a simple way.

4
  • scanf("%d", a); should be scanf("%d", &a); Commented Jan 11, 2016 at 20:36
  • Use int *info; and use malloc: Node n;, n.info = malloc((sizeof int) * a);. Commented Jan 11, 2016 at 20:37
  • Use a flexible array member. But that must be the last member in a struct. Commented Jan 11, 2016 at 20:40
  • That won't work, the structure definition is created a the time of compilation, and you get the size as run-time. You need to come up with another way (like the two in the above comments). Commented Jan 11, 2016 at 20:42

3 Answers 3

3

When you use scanf into an integer you must pass a pointer. You may want to check the documentation.

 scanf("%d", &a);

As far as using a variable to allocate data, you can do it, but you will want to use malloc most likely after defining your structure as a type.

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

5 Comments

What else does the documentation say that is very important about scanf()?
@iharob you should check it for errors like in my example
@tinky_winky Exactly, specially when the "%d" specifier is used, things can go very wrong if one assumes that it worked!
Exactly. Silently neglected errors are very very bad thing, though many seem to enjoy them!
Honestly, I'm surprised that this question didn't get voted closed. :)
2

As pointed out by @David Hoelzer

scanf("%d",a);

should be

scanf("%d",&a);

If you are under C99 you can use flexible array members:

typedef struct mylist {
    struct mylist *link;
    int info[]; /* should be the last member */
} Node;

and then

Node *node = malloc(sizeof *node + (a * sizeof(int)));

Comments

1

You must have pointer to array of correct size or use flexible array. Here is how to use a pointer initializing it to point to storage of proper size.

struct mylist {
    int *info;    // or int info[] at the end of struct def if C99 is used
    int info_size;
    struct mylist *link;
};

int main(void)
{
    int *array;
    struct mylist m;
    if (scanf("%d", &m.info_size) != 1)
    {
        perror("scanf failed");
        exit(EXIT_FAILURE);
    }
    // ok
    array = malloc(sizeof(int) * m.info_size);
    if (array == NULL)
    {
        perror("malloc failed");
        exit(EXIT_FAILURE);
    }
    // ok, commit changes
    m.info = array;

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.