There's several problems with your code.
First, you seem to be confusing a declaration of what a data type is with actually having a variable. You declare a struct
, and from then on it's a data type, much like int
or double
. Before you assign anything to one, you need to have one. You can get one either by defining one in the function (ALPHA a;
) or by allocating memory for one with malloc()
or calloc()
.
To use calloc()
, you have two arguments, one being how many whatevers you want, and one being the size of a whatever. For malloc()
, you multiply the two. The other difference is that malloc()
returns memory with whatever used to be in it, while calloc()
initializes everything to zero. (That's zero-length string or integral zeros according to the Standard. Other values are not guaranteed, but with most modern systems you'll get the equivalent of a zero.) These functions return a pointer to the memory allocated.
You seem to want tot
int
s, so (using calloc()
), the correct statement is something like int * a = calloc(tot, sizeof(int));
, or int * a = calloc(tot, sizeof(*a));
. No cast is required in C (it is required in C++, but you usually wouldn't use malloc()
or calloc()
in C++), and the only thing it can do is cover up a possible mistake (leaving out #include <stdlib.h>
to be specific).
Once you have that, you can refer to the int
s as something like a[3]
.
Putting the result in a field of an ALPHA
is doable, but you really do need an ALPHA
, so something like
ALPHA a;
a.size = calloc(tot, sizeof(*a));
will work. You would therefore refer to it as a.size[3]
, for example.
Also, I don't see what name
is doing. It's one character, which is not enough for any non-empty string, and I don't know why you've got it in the calloc()
call. You might want name
to be a dynamically allocated string, with size
being its size. In that case, you'd need to change the lines in the declaration of ALPHA
to be
int size;
char * name;
and the code might be be
ALPHA a;
fgets(line, 60, fp);
a.size = atoi(line);
a.name = calloc(a.size, sizeof(*a.name));
Once you've done that, after entering 10, you can refer to a.name[0]
through a.name[9]
, and that's your ten characters. a.name[10]
would be one past the end. Note that you can only put a nine-character string in a ten-character array, since you need to have room for the null terminator (the '\0'
which is the last character of any C-style string). If you want to be able to enter the specified number of characters, you'd want to add 1 to a.size
after putting the user-entered number in.