“allocation de mémoire dynamique pour la chaîne en c” Réponses codées

allocation de mémoire dynamique pour la chaîne en c

string* str_arr = nullptr;
str_arr = new string[10];

//initialize
str_arr[0] = "Hello";
str_arr[1] = " World!";
Magnificent Millipede

allocation de mémoire dynamique c

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
    // This pointer will hold the
    // base address of the block created
    int* ptr;
    int n, i;
 
    // Get the number of elements for the array
    printf("Enter number of elements:");
    scanf("%d",&n);
    printf("Entered number of elements: %d\n", n);
 
    // Dynamically allocate memory using malloc()
    ptr = (int*)malloc(n * sizeof(int));
 
    // Check if the memory has been successfully
    // allocated by malloc or not
    if (ptr == NULL) {
        printf("Memory not allocated.\n");
        exit(0);
    }
    else {
 
        // Memory has been successfully allocated
        printf("Memory successfully allocated using malloc.\n");
 
        // Get the elements of the array
        for (i = 0; i < n; ++i) {
            ptr[i] = i + 1;
        }
 
        // Print the elements of the array
        printf("The elements of the array are: ");
        for (i = 0; i < n; ++i) {
            printf("%d, ", ptr[i]);
        }
    }
 
    return 0;
}
Dead Dunlin

Réponses similaires à “allocation de mémoire dynamique pour la chaîne en c”

Questions similaires à “allocation de mémoire dynamique pour la chaîne en c”

Plus de réponses similaires à “allocation de mémoire dynamique pour la chaîne en c” dans C++

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code