指针对于数组和字符串来说 是其内存的首地址。
- 首先发现字符型和整形的有点不一样呢
#define _CRT_SECURE_NO_WARNINGS #include
#include #include #define DER 100char * getStr1(){ char *p1 = "abcdefg2"; return p1;}char *getStr2(){ char *p2 = "abcdefg2"; return p2;}void main(){ char *p1 = NULL; char *p2 = NULL; int *b1; int *b2; int a=100; b1=&a; b2=&a; p1 = getStr1(); p2 = getStr2(); //打印p1 p2 所指向内存空间的数据 printf("p1:%s , p2:%s \n", p1, p2);//直接显示的是指针内部的值 //打印p1 p2 的值 printf("p1:%d , p2:%d \n", p1, p2);//这里显示的是两个指针指向值地址 printf("b1:%d ,b2:%d \n", b1, b2);//这里显示的是指针指向值的地址 printf("b1:%d ,b2:%d \n", &b1, &b2);//整个数据的地址,如100,12个字节 printf("b1:%d ,b2:%d \n", *b1, *b2);//指针内存放的值 printf("b1:%s ,b2:%s \n", b1, b2);//两个指针所代表的ascii表中的值 printf("hello...\n"); system("pause"); return ;} 结果为
-
:
#include "stdlib.h"#include "stdio.h"#include "string.h"// 数据类型的用途//数据类型的本质:固定大小内存块的别名// b &b 数组数据类型 (定义一个1 数组类型 2数组指针 3 数组类型和数组指针类型的关系) ====>压死初学者的三座大山 抛砖//void main31(){ int a; //告诉c编译器分配4个字节的内存 int b[10] ; //告诉c编译器分配40个自己内存 printf("b:%d, b+1:%d, &b:%d, &b+1:%d \n", b, b+1, &b, &b+1); printf("sizeof(b):%d \n", sizeof(b)); //40 printf("sizeof(a):%d \n ", sizeof(a)); //4 // b+1 &b+1 结果不一样 //b &b所代表的数据类型不一样 //b 代表的数组首元素的地址 //&b代表的是整个数组的地址 // printf("hello....\n"); system("pause");}struct Teacher{ char name[64]; int age;}Teacher;typedef struct Teacher2{ char name[64]; int age;}Teacher2;//数据别名 typedeftypedef int u32;void main33(){ int a; //告诉c编译器分配4个字节的内存 int b[10] ; //告诉c编译器分配40个自己内存 struct Teacher t1; Teacher2 t2; t1.age = 31; printf("u32:%d \n", sizeof(u32)); { char *p2 = NULL; void *p1 = NULL; p2 = (char *)malloc(100); p1 = &p2; } { //void a;//编译器不知道如何分配内存 } printf("hello....\n"); system("pause");}