C语言指针问题,我这样想利用pointpointer改变pointer,使pointer指向整形变量number2,可是最终运行不对
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言指针问题,我这样想利用pointpointer改变pointer,使pointer指向整形变量number2,可是最终运行不对相关的知识,希望对你有一定的参考价值。
我是指针这块不怎么理解,我这样写最终没有让point指向number2,哪里错了呢?
这是我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
int number1=100,number2=200;
int *pointer,**pointpointer;
pointer=&number1;
pointpointer=&pointer;
printf("Before changed by pointpointer:\n");
printf("variable pointer's address:%p\n",&pointer);
printf("variable pointer's value:%d\n",pointer);
printf("variable pointed to by pointer:%d\n\n",*pointer);
printf("Pointpointer's attribute:\n");
printf("variable pointpointer's address:%p\n",&pointpointer);
printf("variable pointpointer's size:%d\n",sizeof(pointpointer));
printf("variable pointpointer's value:%p\n",pointpointer);
printf("value pointed to by pointpointer:%d\n\n",*pointpointer);
pointpointer=&number2;
pointer=&pointpointer;
printf("After changed by pointpointer:\n");
printf("variable pointer's address:%p\n",&pointer);
printf("variable pointer's value:%d\n",pointer);
printf("value pointed to by pointer:%d\n",*pointer);
return 0;
#include<stdlib.h>
#include<string.h>
int main()
int number1=100,number2=200;
int *pointer,**pointpointer;
pointer=&number1;
pointpointer=&pointer;
printf("Before changed by pointpointer:\\n");
printf("variable pointer's address:%p\\n",&pointer);
printf("variable pointer's value:%d\\n",pointer);
printf("variable pointed to by pointer:%d\\n\\n",*pointer);
printf("Pointpointer's attribute:\\n");
printf("variable pointpointer's address:%p\\n",&pointpointer);
printf("variable pointpointer's size:%d\\n",sizeof(pointpointer));
printf("variable pointpointer's value:%p\\n",pointpointer);
printf("value pointed to by pointpointer:%d\\n\\n",*pointpointer);
pointer=&number2;
pointpointer=&pointer;
printf("After changed by pointpointer:\\n");
printf("variable pointer's address:%p\\n",&pointer);
printf("variable pointer's value:%d\\n",pointer);
printf("value pointed to by pointer:%d\\n",*pointer);
return 0;
第19、20行错了。
这样就对了
oc中的指针
谈到c语言,不得不提指针,有多少英雄好汉栽在指真上。利用指计可以很方便的使用数组和字符串(C#中的String类型就是引用类型,也就是指针类型)。指针支持动态分配内存,可以让程序更简介,提高效率。
“要想精确打击,就要人工引导”---军旅剧中导弹兵经常会这样说,指针类似于人工引导的坐标。灵活,易用,同时也很复杂,引导错误就是毁灭性的打击。举个栗子:移动大厦一层有10个方间,编号依次是1-10.其中2号给A公司租用,5-8给公司租用。以每个房间的编号第一个为公司的地址。如果我们要给A公司送快递,那就是给地址为2的公司送快递。如果给B公司送快递就是给地址为5的公司送快递;
同样,在编程中系统为了管理内存,给内存中的每一个字节都设一个编号。所有变量在内存中要战胜一个或几个连续的字节。其中第一个字节就是该变量的地址。若有INT 类型的A,编译时系统会为A分配两个字节的存储单元。假设这两个字节的编号分别为:001002那边A的地址就是001;
类型* 变量名;---定义指针
&取地址运算符,后面会紧跟一个变量,用来读取变量的保存地址;
*:取变量的运算符。用于读取该指针变的值;
// // main.m // ZhiZhen // // Created by 程英暾 on 2017/3/21. // Copyright 2017年 程英暾. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { int a=10;//变量 int* b;//指针类型的b b=&a;//将A的地址给B,也可以说B代表A的地址 NSLog(@"打印指针B所指向的地址中的值:%d",*b);//打印指针中所指向a的值 *b=20;//给指针赋值,也就是通过B给变理A赋值 NSLog(@"a的值已经改变,其值为:%d",a);//a的值改变了 }//@autoreOVER return 0; }
--------------------result--------------------
2017-03-21 22:20:29.835850 ZhiZhen[8931:355806] 打印指针B所指向的地址中的值:10
2017-03-21 22:20:29.836091 ZhiZhen[8931:355806] a的值已经改变,其值为:20
以上是关于C语言指针问题,我这样想利用pointpointer改变pointer,使pointer指向整形变量number2,可是最终运行不对的主要内容,如果未能解决你的问题,请参考以下文章