C problem with array s
Can I extend an array after it is full?
If so how?
Please explain in detail. My array shows full after 30 iteration s.
My array size is 30.
Can I extend an array after it is full?
If so how?
Please explain in detail. My array shows full after 30 iteration s.
My array size is 30.
First let me know whether you have used the malloc or simply an array.
int A[30];
or
int *A=(int*)malloc(30*sizeof(int));
If you have used the first one, then
Recreate an array
int B[40];
Copy A[30] to B[30];
for(i=0;i<30;i++){ B[i]=A[i];}
Now use B[30] to B[39]. Reanme B to A
int * temp=A;
A=B;
free temp[];
If you have used the second one, then,
use Realloc()
A = ( int * ) realloc ( A , sizeof ( int ) * 40 );
You get a new array of size 40 with the previous elements intact.
If your requirement is Increasing Array, then try a linked List
struct node{
int A;
struct node *next;
};
struct node *head,*temp;
head = ( struct node * ) malloc ( sizeof ( struct node ));
head > A=0;
head > next=NULL;
for(i=1;i<30;i++){
traverse=head;
while(traverse->next!=NULL)
       traverse=traverse->next;
temp = ( struct node * ) malloc ( sizeof ( struct node ));}
temp->A=i;
temp->next=NULL;
traverse->next=temp;
//Now you have a linked list with 30 data;
//Print It
traverse=head;
while(traverse->next!=NULL){
       printf("%d",traverse->A);
       traverse=traverse->next;
}