Answers for "C malloc"

C
4

malloc in c

#include <stdlib.h>

void *malloc(size_t size);

void exemple(void)
{
  char *string;
  
  string = malloc(sizeof(char) * 5);
  if (string == NULL)
    return;
  string[0] = 'H';
  string[1] = 'e';
  string[2] = 'y';
  string[3] = '!';
  string[4] = '\0';
  printf("%s\n", string);
  free(string);
}

/// output : "Hey!"
Posted by: Guest on March-03-2020
4

malloc c

int main(int argc, char *argv[])
{
    int* memoireAllouee = NULL;

    memoireAllouee = malloc(sizeof(int));
    if (memoireAllouee == NULL) // Si l'allocation a échoué
    {
        exit(0); // On arrête immédiatement le programme
    }

    // On peut continuer le programme normalement sinon

    return 0;
}
Posted by: Guest on November-13-2020
1

malloc in c

// Let's allocate enough space on the heap for an array storing 4 ints

intArray = (int *) malloc(4 * sizeof(int)); // A pointer to an array of ints

intArray[0] = 10;
intArray[1] = 20;
intArray[2] = 25;
intArray[3] = 35;
Posted by: Guest on June-15-2021
0

C malloc

#include <stdlib.h>
int main(){
int *ptr;
ptr = malloc(15 * sizeof(*ptr)); /* a block of 15 integers */
    if (ptr != NULL) {
      *(ptr + 5) = 480; /* assign 480 to sixth integer */
      printf("Value of the 6th integer is %d",*(ptr + 5));
    }
}
Posted by: Guest on August-28-2021
0

C malloc

Example: ptr = (int *) malloc (50)
Posted by: Guest on August-28-2021
0

C malloc

ptr = (cast_type *) malloc (byte_size);
Posted by: Guest on August-28-2021

Code answers related to "C"

Browse Popular Code Answers by Language