I wrote like this but it isn't work
example is int. how can I do this??Code:(const *)example;
Printable View
I wrote like this but it isn't work
example is int. how can I do this??Code:(const *)example;
shoud be just
since its your not coverting into const integer pointer.Code:(const int) example;
ssharish
Const is not a type. You can only cast to a new type. A type can be constant, however, so applying const to the type will make it constant.
it isnt work! I have simple struct and I want to use like this
but "x[bla]" you now bla must be const int so I get error..Code:void example(int *vle)
{
.....
struct exam{
......
}x[(const)*vle]
}
sory my english.
Const is not a type.
Use (const int).
I used it (const int) but result didnt change...
I get this error
1)Constant expression required
2)Constant variable 'x' must be initialized
I suppose you are trying to create an array of structs with a non-constant expression. This is illegal.
Consider:
It is allowed to create a dynamic array on the stack in C99, but not C89.Code:int n = 10;
int myarray[n]; /* Illegal */
int myarray[10]; /* Legal */
But I must get struct size from another result.. Cant we do this?? :S
It's possible via dynamic memory.
That is malloc and free. Malloc to allocate a dynamic size of memory and free to free it later when you don't need it.
thanks for advice but I didnt it in struct.. if I use int so I can do like this:
is it true?? I think yes but I never do it for struct.. How can we do this??Code:int * number = (int *) malloc(*nos);
Should be
You need to specifgy the size to the malloc, otherwise the malloc will assume one byte for every element, which in your case should be int. Since you are trying to allocate integer vector. In the same way if you are allocating float then just replace sizeof(int) with sizof(float) and same applies for the other datatypes.Code:int * number = (int *) malloc( *nos * sizeof(int) );
and check for error
if( number == NULL )
report error;
ssharish
Yes :S but I want it for struct like this
I want to use malloc for x struct??Code:void example(int *vle)
{
.....
struct exam{
......
}x
}
and add one more question how can we do this for char array??
ssharishCode:void example(int *vle)
{
.....
struct exam{
int in;
float flo;
};
struct exam *container = malloc( sizeof( struct exam ) * *vle );
// Check for return value of malloc and report error on failure.
}
Another, more popular way:
And never cast the return of malloc.Code:struct exam* container = malloc(sizeof(*container) * *vle);