2024年大数据最新C语言高级教程-C语言数组(二)_c语言中高维数组的作用 (1),一线互联网企业高级大数据开发工程师面试题大全

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

A variable of type long occupies 4 bytes.
Here are the addresses of some variables of type long:
The address of a is: 00EFFC60  The address of b is: 00EFFC54
The address of c is: 00EFFC48

A variable of type double occupies 8 bytes.
Here are the addresses of some variables of type double:
The address of d is: 00EFFC38  The address of e is: 00EFFC28
The address of f is: 00EFFC18
请按任意键继续. . .

在这里插入图片描述

声明3个long类型的变量和3个double类型的变量

// Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;

// Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;

2.2 输出变量地址的格式

下来输出long变量占用的字节数,跟着输出这3个变量的地址

printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);

  • 使用%u显示sizeof 生成的值,因为它是无符号的整数。
  • 使用一个新的格式说明符%p,来输出变量的地址。
  • 这个格式说明符指定输出一个内存地址,其值为十六进制。内存地址一般是32位或64位,地址的大小决定了可以引用的最大内存量。

输出double变量占用的字节数,接着输出这3个变量的地址

printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
printf("\nHere are the addresses of some variables of type double:");
printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
printf("\nThe address of f is: %p\n", &f);

  • 事实上,程序本身不如输出那么有趣。
  • 看看显示出来的地址,地址值逐渐变小,是成“等差排列”,如下图所示。
    在这里插入图片描述
  • 在本例中,地址b比a低4,c比b低4。这是因为每个long类型的变量占用4个字节。
  • 变量d、e、f也是如此,但它们的差是8。这是因为类型double的值用8个字节来存储。

三、输出数组里面变量的地址

3.1 输入数组里面存储的数据变量的地址

下面的程序将输出数组里面的10变量的地址

	int arrays[10];

    srand(time(NULL));
    for (int i = 0; i < 10; i++)
    {
        arrays[i] = rand() % 20 + 1;
    }

    for (int i = 0; i < 10; i++)
    {
        printf("%d of the address--> %p\n", arrays[i], &arrays[i]);
    }
    printf("\n");

按F5进行编译,运行结果如下所示

18 of the address--> 010FFE58
17 of the address--> 010FFE5C
4 of the address--> 010FFE60
15 of the address--> 010FFE64
6 of the address--> 010FFE68
12 of the address--> 010FFE6C
9 of the address--> 010FFE70
3 of the address--> 010FFE74
7 of the address--> 010FFE78
13 of the address--> 010FFE7C

请按任意键继续. . .


在这里插入图片描述

  • 有地址的变化规律可以看出int类型的地址占四个字节。
  • 数组中存储的数据地址是呈现递增关系。

3.2 “&数组名”:代表数组首地址

&数组名:是取出数组的首个(第一个)数据的地址。
增加代码如下所示

printf("The array of the address is %p\n", &arrays);

按F5进行编译,运行结果如下所示

13 of the address--> 006FFAC8
18 of the address--> 006FFACC
9 of the address--> 006FFAD0
18 of the address--> 006FFAD4
19 of the address--> 006FFAD8
16 of the address--> 006FFADC
17 of the address--> 006FFAE0
7 of the address--> 006FFAE4
17 of the address--> 006FFAE8
17 of the address--> 006FFAEC

The array of the address is 006FFAC8
请按任意键继续. . .


在这里插入图片描述

  • 可以看出"&数组名"就是将数组中的第一个变量的地址取出来。

四、完整程序

本文的完整程序如下所示

4.1 Main.h 文件程序

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


4.2 Main.c 文件程序

#define \_CRT\_SECURE\_NO\_WARNINGS

#include "Main.h"

int main()
{
    system("color 3E");

   /\* Define some integer variables
 long a = 1L;
 long b = 2L;
 long c = 3L;

 Define some floating-point variables
 double d = 4.0;
 double e = 5.0;
 double f = 6.0;

 int arrays[10];

 printf("A variable of type long occupies %u bytes.", sizeof(long));
 printf("\nHere are the addresses of some variables of type long:");
 printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
 printf("\nThe address of c is: %p", &c);
 printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
 printf("\nHere are the addresses of some variables of type double:");
 printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
 printf("\nThe address of f is: %p\n", &f);\*/

    int arrays[10];

    srand(time(NULL));
    for (int i = 0; i < 10; i++)
    {
        arrays[i] = rand() % 20 + 1;
    }

    for (int i = 0; i < 10; i++)
    {
        printf("%d of the address--> %p\n", arrays[i], &arrays[i]);
    }
    printf("\n");
    printf("The array of the address is %p\n", &arrays);
    

    system("pause");
    return 0;
}



五、总结

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

转存中…(img-TykYwLkT-1715608945646)]
[外链图片转存中…(img-dgDqiYuu-1715608945648)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值