C语言_动态内存分配练习
发布日期:2021-05-14 23:42:47 浏览次数:9 分类:精选文章

本文共 3528 字,大约阅读时间需要 11 分钟。

������������������������������

���1���������������������������������������

���������������������������������

#include 
#include
#include
#define REGISTER_SIZE 32typedef int register_int;typedef struct{ unsigned char size_byte[4]; unsigned char addr_byte[4]; unsigned char pad_byte[4]; unsigned char type_byte[4];} memory_model_t;// ������������������void* applyspace(int* len, size_t size, char type){ memory_model_t memory = { { strlen(&type), 0, 0, 0 } }; return malloc(size);}

������������

#define OFFSET 0#define.LAZYALLOC_SIZE 10int main(){    int (*p = NULL);    size_t size = sizeof(int);    char type = 'm';        // ������������    p = applyspace(&size, size, type);        // ������������������    input(p, &size);        // ���������������    input_rand(p, &size);        // ������������    free(p);    return 0;}

���2������������������������10���������������������������������������������

������������

#include 
#include
// ������������������������int* array = (int*)malloc(10 * sizeof(int));if (!array){ perror("malloc failed"); return 1;}// ������������int len = 10;printf("���������10��������� ");for (int i = 0; i < len; ++i){ scanf("%d", &array[i]);}// ������������������������������int unique = -1;for (int i = 0; i < len; ++i){ int count = 0; for (int j = 0; j < len; ++j) { if (array[i] == array[j]) { count++; break; } } if (count == 1) { unique = array[i]; break; }}// ������������printf("��������������������������� %d\n", unique);free(array);return 0;

���3������������������������10������������������������������������

  • ���������������������
  • ���������������2������������������2������
  • ������������������������������

������������

#include 
#include
#include
// ������������������������int* array = (int**)malloc(sizeof(int*) * 10);if (!array){ perror("malloc failed"); return 1;}// ������������int len = 10;printf("���������10������������ ");for (int i = 0; i < len; ++i){ scanf("%d", array[i]);}// ���������������������������������������������// k��torsSort������void sort(int* array, int* len){ int i; for (i = 0; i < *len; ++i) { pos = i; for (j = i + 1; j < *len; ++j) { if (array[pos] > array[j]) { // ������ array[pos] ^= array[j]; array[j] ^= array[pos]; array[j] ^= array[pos]; } } }}sort(array, &len);printf("��������������������������������������������������� ");int sum = 0;for (int i = 2; i < len - 2; ++i){ sum += array[i];}// ���������������int average = sum / (len - 4);printf("���������������������������%.2f\n", average);free(array);return 0;

���4���������������

������������

  • ������������������������������������������
  • ������������������������������������
  • ������������������������������������

������������

#include 
#include
#include
// ������������������������void* applyspace(size_t* len, size_t size){ return (void**)malloc(size);}int main(){ // ��������������������� int* data = applyspace(&data_len, data_size); // ������������ printf("������������������ "); scanf("%d", data); // ������������ int pos = 5; alter_space(data, &pos, 10); // ������������������������ printf("��������������������� %d\n", data[pos]); // ������������ free(data); return 0;}
上一篇:C语言_指针及回调函数练习
下一篇:C语言_链表操作

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月14日 01时47分17秒