动态内存分配
来源:互联网 发布:windows arp老化时间 编辑:程序博客网 时间:2024/06/04 20:03
/*
动态分配内存演示
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
//int arr[5] = {1, 2, 3, 4, 5};
/*for (num = 0;num <= 4;num++) {
printf("%d ", arr[num]);
}
printf("\n");*/
int num = 0, size = 0;
printf("请输入存储区个数:");
scanf("%d", &size);
int *p_num = (int*)malloc(size * sizeof(int));
if (p_num) {
for (num = 0;num <= size - 1;num++) {
*(p_num + num) = num + 1;
}
for (num = 0;num <= size - 1;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *create(void) {
int num = 0;
int *p_num = (int*)malloc(7 * sizeof(int));
if (p_num) {
for (num = 0;num <= 6;num++) {
*(p_num + num) = rand() % 36 + 1;
}
}
return p_num;
}
int main() {
int *p_num = NULL, num = 0;
srand(time(0));
p_num = create();
if (p_num) {
for (num = 0;num <= 6;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row, col;
} pixel;
/*pixel *read(void) {
pixel *p_pt = (pixel *)malloc(sizeof(pixel));
if (p_pt) {
printf("请输入点的位置:");
scanf("%d%d", &(p_pt->row), &(p_pt->col));
}
return p_pt;
}*/
void read(pixel **pp_pt) {
*pp_pt = (pixel *)malloc(sizeof(pixel));
if (*pp_pt) {
printf("请输入点的位置:");
scanf("%d%d", &((*pp_pt)->row), &((*pp_pt)->col));
}
}
int main() {
//pixel *p_pt = read();
pixel *p_pt = NULL;
read(&p_pt);
if (p_pt) {
printf("点的位置是(%d, %d)\n", p_pt->row, p_pt->col);
free(p_pt);
p_pt = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
char *read(void) {
char *p_str = (char *)malloc(10 * sizeof(char));
if (p_str) {
printf("请输入字符串:");
fgets(p_str, 10, stdin);
}
return p_str;
}
void read1(char **pp_str) {
*pp_str = (char *)malloc(10 * sizeof(char));
if (*pp_str) {
printf("请输入字符串:");
fgets(*pp_str, 10, stdin);
}
}
int main() {
//char *p_str = read();
char *p_str = NULL;
read1(&p_str);
if (p_str) {
printf("字符串是%s\n", p_str);
free(p_str);
p_str = NULL;
}
return 0;
}
/*
动态分配内存作业
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row, co;
} pixe;
pixe *midpt(const pixe *p_pt1, const pixe *p_pt2) {
pixe *p_pt = (pixe *)malloc(sizeof(pixe));
if (p_pt) {
p_pt->row = (p_pt1->row + p_pt2->row) / 2;
p_pt->co = (p_pt1->co + p_pt2->co) / 2;
}
return p_pt;
}
void midpt1(const pixe *p_pt1, const pixe *p_pt2, pixe **pp_pt) {
*pp_pt = (pixe *)malloc(sizeof(pixe));
if (*pp_pt) {
(*pp_pt)->row = (p_pt1->row + p_pt2->row) / 2;
(*pp_pt)->co = (p_pt1->co + p_pt2->co) / 2;
}
}
int main() {
pixe pt1 = {}, pt2 = {}, *p_pt = NULL;
printf("请输入一个点的位置:");
scanf("%d%d", &(pt1.row), &(pt1.co));
printf("请输入另外一个点的位置:");
scanf("%d%d", &(pt2.row), &(pt2.co));
//p_pt = midpt(&pt1, &pt2);
midpt1(&pt1, &pt2, &p_pt);
if (p_pt) {
printf("中间点位置是(%d, %d)\n", p_pt->row, p_pt->co);
free(p_pt);
p_pt = NULL;
}
return 0;
}
/*
calloc演示
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p_num = (int *)calloc(5, sizeof(int));
int num = 0;
if (p_num) {
for (num = 0;num <= 4;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
动态分配内存演示
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
//int arr[5] = {1, 2, 3, 4, 5};
/*for (num = 0;num <= 4;num++) {
printf("%d ", arr[num]);
}
printf("\n");*/
int num = 0, size = 0;
printf("请输入存储区个数:");
scanf("%d", &size);
int *p_num = (int*)malloc(size * sizeof(int));
if (p_num) {
for (num = 0;num <= size - 1;num++) {
*(p_num + num) = num + 1;
}
for (num = 0;num <= size - 1;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *create(void) {
int num = 0;
int *p_num = (int*)malloc(7 * sizeof(int));
if (p_num) {
for (num = 0;num <= 6;num++) {
*(p_num + num) = rand() % 36 + 1;
}
}
return p_num;
}
int main() {
int *p_num = NULL, num = 0;
srand(time(0));
p_num = create();
if (p_num) {
for (num = 0;num <= 6;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row, col;
} pixel;
/*pixel *read(void) {
pixel *p_pt = (pixel *)malloc(sizeof(pixel));
if (p_pt) {
printf("请输入点的位置:");
scanf("%d%d", &(p_pt->row), &(p_pt->col));
}
return p_pt;
}*/
void read(pixel **pp_pt) {
*pp_pt = (pixel *)malloc(sizeof(pixel));
if (*pp_pt) {
printf("请输入点的位置:");
scanf("%d%d", &((*pp_pt)->row), &((*pp_pt)->col));
}
}
int main() {
//pixel *p_pt = read();
pixel *p_pt = NULL;
read(&p_pt);
if (p_pt) {
printf("点的位置是(%d, %d)\n", p_pt->row, p_pt->col);
free(p_pt);
p_pt = NULL;
}
return 0;
}
/*
动态分配内存练习
*/
#include <stdio.h>
#include <stdlib.h>
char *read(void) {
char *p_str = (char *)malloc(10 * sizeof(char));
if (p_str) {
printf("请输入字符串:");
fgets(p_str, 10, stdin);
}
return p_str;
}
void read1(char **pp_str) {
*pp_str = (char *)malloc(10 * sizeof(char));
if (*pp_str) {
printf("请输入字符串:");
fgets(*pp_str, 10, stdin);
}
}
int main() {
//char *p_str = read();
char *p_str = NULL;
read1(&p_str);
if (p_str) {
printf("字符串是%s\n", p_str);
free(p_str);
p_str = NULL;
}
return 0;
}
/*
动态分配内存作业
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row, co;
} pixe;
pixe *midpt(const pixe *p_pt1, const pixe *p_pt2) {
pixe *p_pt = (pixe *)malloc(sizeof(pixe));
if (p_pt) {
p_pt->row = (p_pt1->row + p_pt2->row) / 2;
p_pt->co = (p_pt1->co + p_pt2->co) / 2;
}
return p_pt;
}
void midpt1(const pixe *p_pt1, const pixe *p_pt2, pixe **pp_pt) {
*pp_pt = (pixe *)malloc(sizeof(pixe));
if (*pp_pt) {
(*pp_pt)->row = (p_pt1->row + p_pt2->row) / 2;
(*pp_pt)->co = (p_pt1->co + p_pt2->co) / 2;
}
}
int main() {
pixe pt1 = {}, pt2 = {}, *p_pt = NULL;
printf("请输入一个点的位置:");
scanf("%d%d", &(pt1.row), &(pt1.co));
printf("请输入另外一个点的位置:");
scanf("%d%d", &(pt2.row), &(pt2.co));
//p_pt = midpt(&pt1, &pt2);
midpt1(&pt1, &pt2, &p_pt);
if (p_pt) {
printf("中间点位置是(%d, %d)\n", p_pt->row, p_pt->co);
free(p_pt);
p_pt = NULL;
}
return 0;
}
/*
calloc演示
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p_num = (int *)calloc(5, sizeof(int));
int num = 0;
if (p_num) {
for (num = 0;num <= 4;num++) {
printf("%d ", *(p_num + num));
}
printf("\n");
free(p_num);
p_num = NULL;
}
return 0;
}
0 0
- 动态内存分配
- 关于动态内存分配
- 关于动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- C++ 动态内存分配
- VC动态内存分配
- 动态数组分配内存
- C++动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配
- 动态内存分配+链表
- 动态内存分配函数
- php 抽奖类的
- WebUtil
- php中在一个php文件调用另一个php的函数,保存登录时保存的用户id
- Android--RecyclerView
- 异步加载(AsyncTask异步任务、Handler、Json解析、Lrucache缓存、ListView滑动优化等来实现ListView图文混排)
- 动态内存分配
- linux中的link
- Android开发问题集锦
- C语言——缓冲区
- struts 2 实现文件下载 中文乱码问题
- [置顶] xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)
- Django1.9 Python3.4出现Error loading MySQLdb module:No module named ‘MySQLdb’问题
- 全屏点击,开始游戏
- iOS中的Runloop