目录
指针相同点
1.定义
通常我们把指针变量简称指针,简单直白点,指针变量就是用来存储内存地址的变量即为指针变量。
2. 用法
//C版本
#include<stdio.h>
int main(int argc, char *argv[]){
int * p1;
int a = 5;
p1 =&a;
printf("p1 value:%d",*p1);
}
//Golang版本
func main() {
var p1* int
a:=5
p1=&a
fmt.Println("p1 value is:",*p1)
}
// C版本
int main(int argc, char *argv[]) {
int **outer;
int *inter;
int a = 3;
inter = &a;
outer = &inter;
// memory address
printf("%p\n", inter);
printf("%p\n", *outer);
// value
printf("%d\n", *inter);
printf("%d\n", **outer);
}
//Golang版本
func main() {
var outer **int
var inter *int
a := 3
inter = &a
outer = &inter
// memory address
fmt.Println(inter)
fmt.Println(*outer)
// value
fmt.Println(*inter)
fmt.Println(**outer)
}
指针异同点
#include<stdio.h>
#include<malloc.h>
int main(int argc, char *argv[]){
int *p1;
p1 = (int*) malloc(sizeof(int));
*p1 = 3;
printf("p1 value:%d",*p1);
}
//Golang版本
func main() {
var p1 *int
p1 = new(int)
*p1 = 3
fmt.Println(*p1)
}
#include<stdio.h>
int main(int argc, char *argv[]){
int i, *pa, a[] = {3,4,5,6,7,3,7,4,4,6};
pa = a;
printf("this is test:%d\n",*pa);
for (i = 0; i <= 9; i++)
{
printf("%d\n", *pa);
pa++; /*注意这里,指针值被修改*/
}
}
func main() {
var test *int
a := [7]int{5, 2, 3, 6, 7, 8, 9}
test=a //这种操作是非法的,会报错,不支持
fmt.Println(*test)
}
#include<stdio.h>
int main(int argc, char *argv[]){
int a = 111;
double * f;
f=(double *)&a;
}
package main
func main() {
var a int = 111
var f *float64
f = *float64(&a)
}