I want to create a structure with field color, that can consider n characters. So, how can i create it without certain size:
struct A {
int width;
int height;
int length;
char name[50];
**char color[];**
};
struct A a = { 10, 20, 30, "Hello", "Red" };
I tried test it without structure and all is working, why ?
char p[] = "Hallo";
CodePudding user response:
The most straight forward way to do this on a modern system with an operating system is to use malloc/free:
struct A {
int width;
int height;
int length;
char name[50];
char *color;
};
Here color
is just a pointer type to a memory location that is of type char
.
How to use it?
Use malloc
to dynamically allocate memory and save the returned address in color
like this:
struct A var;
var.color = malloc(sizeof(char) * n);
Where n
is the number of char
acters you want to allocate.
sizeof(char)
here is superfluous since it's guaranteed to be 1
.
However, I included it because if you wanted to, say, allocate an array of int
egers you would use:
malloc(sizeof(int) * n)
sizeof(int)
is not always the same value, it depends on your architecture.
Also, in all cases you need to keep track of the size of your allocated memory yourself. There isn't a standard function that will tell you how big a buffer is by simply passing a pointer.
Note that malloc
can return NULL
(a pointer that you should never dereference) in case something went wrong, so you need to check the return value of malloc
before using it).
When you're done with the memory, you must free
the allocated memory like this:
free(var.color);
// do not use the value of var.color after this
Don't use value of var.color
after passing it to free
, otherwise the behaviour is undefined (use-after-free).
If you don't free
your memory you may risk a memory leak.
CodePudding user response:
Following up on my comment to the OP, and assuming the number and names of the acceptable colors are known in advance, here's an example where I used enum
in conjunction with typedef
to define a type that lists all the possible colors allowed by the program, so that the user can refer to each color by its name in the rest of the code.
#include <stdio.h>
typedef enum{
RED = 0,
GREEN = 1,
BLUE = 2
} Colors;
struct A {
int width;
int height;
int length;
char name[50];
Colors color;
};
int main(void){
struct A a = { 10, 20, 30, "Hello", RED };
printf("%d\n",BLUE GREEN);
}
Needless to say, other colors can be added to the enum
definition.
It's also worth noting that enum
associates an integer to each item; arithmetics becomes possible, as in BLUE GREEN
, which yields 3
in the code sample above.
CodePudding user response:
You can have color
field as a pointer to char
and create a enum
whose elements indicate the colours and use them as designated initialiser in an array of strings which will have the color names. The enum
elements can be used as index in color name array to get the appropriate color name and which can be assigned to structure color
field, like this:
#include <stdio.h>
enum colors {
WHITE = 0,
BLACK,
RED,
GREEN,
BLUE,
MAX_COLOR
};
char * color_names[MAX_COLOR 1] = {
[WHITE] = "White",
[BLACK] = "Black",
[RED] = "Red",
[GREEN] = "Green",
[BLUE] = "Blue",
[MAX_COLOR] = NULL
};
struct A {
int width;
int height;
int length;
char name[50];
char * color;
};
int main (void) {
struct A a = { 10, 20, 30, "Hello", color_names[RED] };
printf ("a.color : %s\n", a.color);
return 0;
}
Output:
# ./a.out
a.color : Red