Based on the comments:
But the size of the new arrangement differs from the first ??? - Dariel Ramos Díaz de Villegas
yes, it can be {2,2,2,2, '\ 0'}; the new value as {24,22,23,54,43,23 '\ 0'}; - Alberto Figueroa
I already notice that you are careful to exceed the initial size.
Before this statement:
unsigned char cadena[] = {154,162,162,145,'unsigned char *ptr = 0;
size_t max_size;
max_size = 10;
unsigned char* ptr2 = (unsigned char*)realloc(ptrmax_size*sizeof(unsigned char));
if( 0 == ptr2 )
{
/* Error al solicitar memoria */
}
ptr = ptr2;
'};
The program reserves 5 bytes for cadena
. The rest of the memory will be available to other variables, then if you exceed that limit of 5 bytes you will end up stepping on the value of other variables and the program may behave erratically.
If your idea is to have an array of variable size you have to resort to dynamic memory. To request memory you can use malloc
, but to modify the size of this assignment you have to resort to realloc
. realloc
can also be used to make the initial memory reservation, so it is up to you to choose the mechanism that best suits your needs:
unsigned char cadena[] = {154,162,162,145,'unsigned char *ptr = 0;
size_t max_size;
max_size = 10;
unsigned char* ptr2 = (unsigned char*)realloc(ptrmax_size*sizeof(unsigned char));
if( 0 == ptr2 )
{
/* Error al solicitar memoria */
}
ptr = ptr2;
'};
Note that the memory remapping may fail so it does not hurt to check the pointer that returns realloc
.
After this you can copy the data into the memory as you have indicated in your answer.