Getting to know the pointer
#define _CRT_SECURE_NO_WARNINGS 1
/************************************************
* @Title:> brief
* @Description:> brief
* @author:> HuaGe
* @date:> 2020/10/29 8:46
*************************************************
*笔记:
1、一般情况数组名就是首元素地址
( 特殊1:sizeof(数组名)--此时的数组名表示整个数组,计算整个数组大小,单位是字节;
特殊2:&数组名--表示取出整个数组的地址,但整个数组的地址和首元素地址值一样,但意义不一样。)
2、数组名只是一个地址(不可变),而指针是一个左值(可变)
3、指针数组:
char *p[] = { "I Love You!","meet you" };
for (int i = 0; i < 2 ; i++){
printf("%s\t", p[i]);//%s通过字符串的首地址来输出整个字符串
}
4、数组指针
int (*p2)[4] = &arr;//(*p2)[4]数组指针指向的是整个数组,*p2是数组的地址
************************************************/
#include <stdio.h>
int main()
{
int arr[] = { 1,5,3,7 };
int *p = arr;//p指向的是数组的第一个元素的地址,并不是指向整个数组
int (*p2)[4] = &arr;//(*p2)[4]数组指针指向的是整个数组
for (int i = 0; i < 4; i++) {
printf("%d\t", *(*p2+i));
}
return 0;
}
Operator
*Notes:
1. >> Move the sign bit to the right, the binary bit is shifted, the right is discarded, and the left is 0 (positive number) or 1 (negative number), which has the effect of dividing by 2.
2. <<Left shift operator, binary shift one bit to the left, and zero to the right. (Note: It does not support moving negative bits (such as a>>-1), and only works on integers)
//题1、编写代码,求一个整数存储在内存的二进制中1的个数
void num_1(int x)
{
int flag = 0;
int input = 0;
printf("请输入一个整数:");
scanf("%d", &input);
for (int i = 0; i < 32; i++) {
if (((input >> i) & 1) == 1) {
flag++;
}
}
printf("%d二进制中1的个数为:%d\n",input,flag);
}
int main()
{
int a = 8;
num_1(a);
return 0;
}