Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to insert an element in a dynamic array in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void)
{
    int *p, *tmp, i, size, pos, n;
     
    printf("Enter array size: ");
    scanf("%d", &size);
     
    p = malloc(size * sizeof(int)); 
    if (p == NULL) {
        printf("Error allocating memory\n"); 
        return 1;
    }
     
    srand(time(NULL));  
    for (i = 0; i < size; i++) {
         p[i] = rand() % 10 + 1;
         printf("arr[%d] = %d ", i, p[i]);
    }
    
    printf("\n");
     
    pos = 2; // insert element in this position
    n = 888; // value to be inserted
     
    tmp = realloc(p, (size + 1) * sizeof(int));
    if (tmp != NULL)  {
        p = tmp;
        size++;
    }
    else {
        free(tmp);
        printf("Error allocating memory!\n");
        return 1;
    }  
 
    for (i = size - 1; i > pos; i--)
         p[i] = p[i - 1];
    p[pos] = n;
     
    printf("after insert\n");
    for (i = 0; i < size; i++) {
         printf("arr[%d] = %d ", i, p[i]);
    }
     
    free(p);
  
    return 0;
}


/*
run:

Enter array size: 5
arr[0] = 4 arr[1] = 5 arr[2] = 2 arr[3] = 6 arr[4] = 5 
after insert
arr[0] = 4 arr[1] = 5 arr[2] = 888 arr[3] = 2 arr[4] = 6 arr[5] = 5 

*/


answered Sep 13, 2014 by avibootz
edited May 3, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to insert an element into an array at a given position
int *insertElement(int *arr, int *size, int pos, int value) {
    int *tmp;
    
    // Allocate more memory to store the new element
    tmp = realloc(arr, (*size + 1) * sizeof(int));
    if (tmp == NULL) {
        printf("Error allocating memory!\n");
        return arr; // Return original array if memory allocation fails
    }
    
    arr = tmp; // Update pointer to newly allocated memory
    (*size)++; // Increase size of the array

    // Shift elements to the right to make space for the new element
    for (int i = *size - 1; i > pos; i--) {
        arr[i] = arr[i - 1];
    }

    // Insert the new element at the given position
    arr[pos] = value;

    return arr; // Return updated array
}

int main(void) {
    int *p, i, size, pos, n;
    
    // Prompt user for the array size
    printf("Enter array size: ");
    scanf("%d", &size);
    
    // Allocate memory dynamically for the array
    p = malloc(size * sizeof(int)); 
    if (p == NULL) {
        printf("Error allocating memory\n"); 
        return 1; // Exit if memory allocation fails
    }
    
    // Seed random number generator and initialize array with random values
    srand(time(NULL));  
    for (i = 0; i < size; i++) {
        p[i] = rand() % 10 + 1; // Generate random numbers between 1 and 10
        printf("arr[%d] = %d ", i, p[i]); // Print initial array
    }
    
    printf("\n");
    
    pos = 2; // Define position for insertion
    n = 888; // Value to be inserted

    // Call the function to insert an element at position `pos`
    p = insertElement(p, &size, pos, n);

    // Print the updated array after insertion
    printf("After insert:\n");
    for (i = 0; i < size; i++) {
        printf("arr[%d] = %d ", i, p[i]);
    }
    
    // Free allocated memory
    free(p);
    
    return 0;
}




/*
run:

Enter array size: 5
arr[0] = 9 arr[1] = 5 arr[2] = 1 arr[3] = 5 arr[4] = 3 
After insert:
arr[0] = 9 arr[1] = 5 arr[2] = 888 arr[3] = 1 arr[4] = 5 arr[5] = 3 

*/

 



answered May 3, 2025 by avibootz

Related questions

2 answers 318 views
2 answers 316 views
1 answer 139 views
1 answer 292 views
292 views asked May 19, 2025 by avibootz
1 answer 143 views
2 answers 191 views
3 answers 284 views
...