Reputation: 341
Is it possible to define a char
with a variable length?
I have a char
"name" (member of a struct
named "person") with a length of 25 but I want it to be a variable length between the values 1 and 25, because I want to generate random strings of that char
with different sizes and not always with the same length (25). One of the parameters of the method is sizeof(n.name)
.
Note: n
is a struct
(struct person n
).
The struct
"person" is defined this way:
struct person{
int c;
char name[25];
};
Anyone?
Upvotes: 4
Views: 16019
Reputation: 19864
struct person{
int c;
char name[]; /* Variable length array */
};
I think this should serve your purpose.
Else you can have dynamic memory allocation using
char *name;
name
is a pointer and memory should be allocated and it can be done using malloc()
Upvotes: 5
Reputation: 310950
You can use a flexible array. It must be the last data member of a structure.
struct person{
int c;
char name[];
};
The memory for a structure with a flexible array has to be allocated dynamically.
From the C Standard (6.7.2.1 Structure and union specifiers)
the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. Howev er, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.
And there is an example of its using
20 EXAMPLE 2 After the declaration:
struct s { int n; double d[]; };
the structure struct s has a flexible array member d. A typical way to use this is:
int m = /* some value */;
struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));
and assuming that the call to malloc succeeds, the object pointed to by p behaves, for most purposes, as if
p had been declared as:
struct { int n; double d[m]; } *p;
(there are circumstances in which this equivalence is broken; in particular, the offsets of member d might
not be the same).
Or you could declare a pointer to char and dynamically allocate only the array itself
struct person{
int c;
char *name;
};
Upvotes: 4
Reputation: 726509
char[25]
lets you store C strings of length between zero and 24, inclusive (one character is needed for '\0'
terminator).
You can use one of two solutions:
The first solution lets you keep the name
together with the rest of the struct
, but you would not be able to make arrays of these struct
s:
struct person{
int c;
char name[];
};
See this Q&A for more information on flexible array members. You need a compiler compatible with C99 to use flexible array members.
The second solution takes slightly more memory, but the size of your struct
does not change, making it possible to use it in an array:
struct person{
int c;
char *name;
};
Upvotes: 3
Reputation: 29
A better way to do this is to use the strings library and declare the variable 'name' of type string.
#include<string>
struct person{
int c;
string name;
};
Upvotes: 0