Do me a favor Write programs in C

Updated on technology 2024-05-29
1 answers
  1. Anonymous users2024-02-11

    It's all at once.

    #include

    #include

    main()

    int count,*array;Count is a counter, and array is an integer pointer, which can also be understood as pointing to the first address of an integer array

    if((array(int *)malloc(10*sizeof(int)))==null)

    printf (Storage space could not be allocated successfully. );

    exit(1);

    for (count=0;count〈10;count++) *to assign a value to the array*

    array[count]=count;

    for(count=0;count〈10;count++) *print array elements*

    printf(%2d,array[count]);

    In the example above, 10 integer storage areas are dynamically allocated, which are then assigned and printed. In this example, the if((array(int *)malloc(10*sizeof(int)))==null) statement can be divided into the following steps:

    1) Allocate 10 contiguous storage spaces for integers and return an integer pointer to their starting address.

    2) Assign this integer pointer address to the array

    3) Check whether the return value is null

    2. Free function.

    Since the memory area is always finite, it cannot be allocated without restrictions, and a program should try to save resources, so when the allocated memory area is not used, it should be released so that other variables or programs can use it. This is where we come into use the free function.

    The function prototype is: void free(void *p). The effect is to release the memory area pointed by the pointer p.

    Its argument p must be a pointer returned when a previous call to the malloc function or the calloc function (another function that dynamically allocates a storage area). Passing other values to the free function can cause panic or other disastrous consequences.

    Note: The important thing here is the value of the pointer, not the pointer itself used to apply for dynamic memory. Example:

    int *p1,*p2;

    p1=malloc(10*sizeof(int));

    p2=p1;

    free(p2) or free(p2)*

    Malloc returns a value to P1, and the value of P1 is assigned to P2, so both P1 and P2 can be used as arguments to the free function.

    The malloc function allocates the storage area.

    The free function frees up areas of memory that are no longer in use.

    Therefore, these two functions can be used to dynamically allocate memory areas and manage them simply.

Related questions
14 answers2024-05-29

#include

int plu(int n) returns the sum of the digits on each digit of n. >>>More

5 answers2024-05-29

#include

#define m 5 >>>More

12 answers2024-05-29

A full set of programs in five, when has C been so worthless.

3 answers2024-05-29

Heada and headb are both singly linked lists with leading nodes. In this algorithm, we delete the common elements from the ith element in the heada linked list, and then insert the single-linked list heada before the jth element of the headb. >>>More