Reputation: 45
I have an array 0 1 2 4 5 6 7 8 9. I want to insert 3 between 2 and 4.
Length stays the same after calling the function, even though I have added an value, why?
printf("%d,", feld[9]);
gives me the right value, code works - but I get a warning.
How can I insert a value even though I initialize with int feld[9] = {0,1,2,4,5,6,7,8,9};
or int feld[] = {0,1,2,4,5,6,7,8,9};
?
nt insertArray(int* array, int length, int value, int pos)
{
int i;
if (pos < length)
{
for (i = length; i > pos; i--)
{
array[i] = array[i - 1];
}
array[i] = value;
length++;
}
else if (pos == length)
{
array[pos] = value;
length++;
}
return length;
}
int main()
{
int feld[9] = {0,1,2,4,5,6,7,8,9};
size_t length = sizeof(feld) / sizeof(int);
insertArray(feld, length, 3, 3);
length = sizeof(feld) / sizeof(int);
for (int i = 0; i < length; i++)
{
printf("%d,", feld[i]);
}
printf("\n");
printf("%d,", feld[9]);
return 0;
}
Upvotes: 0
Views: 1009
Reputation: 15042
Per C syntax, It isn´t allowed to modify the length of an static allocated array after its definition. Any attempt to do so invokes undefined behavior.
Instead, Allocate dynamic memory with malloc()
, use pointer offsets to access certain pseudo-elements and use realloc()
to resize the memory.
Copy the content of the elements 4
to 9
to the elements 5
to 10
. Now you can store 3
in the 4th
element.
One demonstrative example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 9
#define ELEM_TO_CH 4
int main(void)
{
int* feld_ptr = malloc(sizeof(int) * SIZE);
if(!feld_ptr)
{
fprintf(stderr,"Memory could not be allocated for feld_ptr!");
return 1;
}
for(int i = 0; i < SIZE; i++)
{
if (i > (ELEM_TO_CH - 2))
feld_ptr[i] = i + 1;
else
feld_ptr[i] = i;
}
printf("Before:\n\n");
for(int i = 0; i < SIZE; i++)
{
printf("feld_ptr[%d] = %d\n", i, feld_ptr[i]);
}
printf("\n\n");
feld_ptr = realloc(feld_ptr, SIZE + 1);
if(!feld_ptr)
{
fprintf(stderr,"Error at resizing memory pointed by feld_ptr!");
return 1;
}
memcpy(&feld_ptr[ELEM_TO_CH], &feld_ptr[ELEM_TO_CH-1], sizeof(int) * ((SIZE + 1) - ELEM_TO_CH));
feld_ptr[ELEM_TO_CH-1] = 3;
printf("After:\n\n");
for(int i = 0; i < (SIZE + 1); i++)
{
printf("feld_ptr[%d] = %d\n", i, feld_ptr[i]);
}
free(feld_ptr);
return 0;
}
Output:
Before:
feld_ptr[0] = 0
feld_ptr[1] = 1
feld_ptr[2] = 2
feld_ptr[3] = 4
feld_ptr[4] = 5
feld_ptr[5] = 6
feld_ptr[6] = 7
feld_ptr[7] = 8
feld_ptr[8] = 9
After:
feld_ptr[0] = 0
feld_ptr[1] = 1
feld_ptr[2] = 2
feld_ptr[3] = 3
feld_ptr[4] = 4
feld_ptr[5] = 5
feld_ptr[6] = 6
feld_ptr[7] = 7
feld_ptr[8] = 8
feld_ptr[9] = 9
Upvotes: 1
Reputation: 3699
As the comments above, you can not extend the size of array. In this case, i propose two solutions:
size = length of old array + 1
. The insert function becomes as:int * insertArray(int* arr, int length, int value, int pos)
{
int * array = malloc(sizeof(int) * (length + 1));
if(!array) {
return NULL;
}
for(int i = 0; i < length; i++) {
array[i] = arr[i];
}
int i;
if (pos < length)
{
for (i = length; i > pos; i--)
{
array[i] = array[i - 1];
}
array[i] = value;
length++;
}
else if (pos == length)
{
array[pos] = value;
length++;
}
return array;
}
Then in main function:
int main()
{
int feld[9] = {0,1,2,4,5,6,7,8,9};
size_t length = sizeof(feld) / sizeof(int);
int * array = insertArray(feld, length, 3, 3);
for (int i = 0; i <= length; i++)
{
printf("%d,", array[i]);
}
free(array);
return 0;
}
void insertArray(int* array, int length, int value, int pos)
{
array = realloc(array, sizeof(int) * (length + 1));
if (!array)
return;
...
}
In the main function:
int main()
{
int *feld = malloc(sizeof(int) * 9);
if(!feld)
return -1;
for(int i = 0; i < 9; i++) {
if(i<3)
feld[i] = i;
else
feld[i] = i+1;
}
insertArray(feld, 9, 3, 3);
for (int i = 0; i <= 9; i++)
{
printf("%d,", feld[i]);
}
free(feld);
return 0;
}
Upvotes: 0
Reputation: 1
You need to use the heap instead of the stack for your task.
As said in the comments below your post, in case you declare the array it will have a fixed size. Of course, you can edit it and add another value but remember you are allocating some additional space in the memory that does not correspond to your array, thus destroying some of the other variables in the stack (maybe) which is not safe.
You can use malloc()
to reserve a required amount of memory in heap then realloc()
function to change the initial size.
Remember to free()
the memory at the end of your program.
Upvotes: 0