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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to pop the first element of an array in C

3 Answers

0 votes
#include <stdio.h>

void pop_first_element(int arr[], int *size) {
    if (*size == 0) {
        printf("Array is already empty.\n");
        return;
    }

    for (int i = 1; i < *size; i++) {
        arr[i - 1] = arr[i];
    }

    (*size)--;
}

int main() {
    int arr[] = {39, 5, 3, 8, 24, 849, 7};
    int size = sizeof(arr) / sizeof(arr[0]);

    pop_first_element(arr, &size);

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

  
  
/*
run:

5 3 8 24 849 7 
  
*/

 



answered May 1, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

void pop_first_element(int **arr, int *size) {
    if (*size == 0) {
        printf("Array is already empty.\n");
        return;
    }

    // Shift elements to the left
    for (int i = 1; i < *size; i++) {
        (*arr)[i - 1] = (*arr)[i];
    }

    // Decrease the size of the array
    (*size)--;

    // Reallocate memory
    *arr = realloc(*arr, (*size) * sizeof(int));
    if (*arr == NULL && *size > 0) {
        printf("Memory reallocation failed.\n");
        exit(1);
    }
}

int main() {
    int size = 5;
    int *arr = malloc(size * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Initialize array
    for (int i = 0; i < size; i++) {
        arr[i] = i + 1;
    }

    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Pop the first element
    pop_first_element(&arr, &size);

    printf("Array after popping first element: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Free the allocated memory
    free(arr);

    return 0;
}



/*
run:

Original array: 1 2 3 4 5 
Array after popping first element: 2 3 4 5 
  
*/

 



answered May 1, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

// Function to allocate memory for an array
int* malloc_array(int size) {
    int *arr = malloc(size * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    return arr;
}

// Function to print the array
void print_array(int *arr, int size, const char *message) {
    printf("%s: ", message);
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Function to pop the first element
void pop_first_element(int **arr, int *size) {
    if (*size == 0) {
        printf("Array is already empty.\n");
        return;
    }

    // Shift elements to the left
    for (int i = 1; i < *size; i++) {
        (*arr)[i - 1] = (*arr)[i];
    }

    // Decrease the size of the array
    (*size)--;

    // Reallocate memory
    int *new_arr = realloc(*arr, (*size) * sizeof(int));
    if (new_arr == NULL && *size > 0) {
        printf("Memory reallocation failed.\n");
        exit(1);
    }
    *arr = new_arr;
}

int main() {
    int size = 5;
    int *arr = malloc_array(size);

    // Initialize array
    for (int i = 0; i < size; i++) {
        arr[i] = i + 1;
    }

    print_array(arr, size, "Original array");

    // Pop the first element
    pop_first_element(&arr, &size);
    
    print_array(arr, size, "Array after popping first element");

    // Free the allocated memory
    free(arr);

    return 0;
}



/*
run:

Original array: 1 2 3 4 5 
Array after popping first element: 2 3 4 5 
  
*/

 



answered May 1, 2025 by avibootz
...