L1--指针

介绍

指针是内存单元的 编号,地址就是指针。一句话通过指针和指针变量 可以在不同函数间接 对数据进行操作

快速入门  

int * p:p是变量名,p变量的数据类型是int * 类型,所谓int * 类型实际就是存放int变量地址的类型,不表示定义了一个名字叫*p的变量

&i: & 取地址运算符,这里指取i 的地址

*p:* 取地址变量所指向的实际地址里存放的数据

#include <stdio.h>

int main(void)
{
    int * p;  //p是变量的名字,int * 表示p变量存放的是int类型变量的地址
              //int * p:不表示定义了一个名字叫做*p的变量
              //int * p:p是变量名,p变量的数据类型是int *类型,即int * 类型实际就是存放int变量地址的类型
    int i = 3;
    p = &i;
    /*
        1.p保存了i的地址,因此p指向i;;
        2.p不是i,i也不是p,更准确的说:修改p的值不影响i的值,修改i的值也不会影响p的值
            *+指针变量 就完全等同于 普通变量
    */
    printf("%d\n", *p);
    return 0;
}
/*
输出结果:
3
*/

内存图:

指针的作用

  1. 表示一些复杂的数据结构;
  2. 快速的传递数据;
  3. 使函数返回一个以上的返回值;
  4. 能直接访问硬件;
  5. 能够方便的处理字符串;
  6. 是理解面向对象语言的基础。

总结:指针是c语言的灵魂。

指针的定义

  • 指针:指针就是地址,地址就是指针;指针变量就是存放地址的变量  
  • 地址:内存单元的编号
    • 从零开始的非负整数
    • 范围:4G  [0--(4G-1)](下面两图解释为什么地址范围是4G)

cpu与内存条交互过程:

cpu与内存条通过地址总线的交互过程图:

指针的分类

  1. 1.基本类型的指针;
  2. 2.指针和数组;
  3. 3.指针和函数;
  4. 4.指针和结构体;
  5. 5.多级指针。
时间: 2025-01-04 00:13:01

L1--指针的相关文章

快慢指针原理--快速找到未知长度单链表的中间节点

package com.java.dataStruct; //节点类 public class Node<E> { E item; Node next; public Node(){ } public Node(E element){ this.item = element; } public Node(E element, Node next){ this.item = element; this.next = next; } } Node p1,r1; Node L1 = new Node

reorder-list——链表、快慢指针、逆转链表、链表合并

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 由于链表尾端不干净,导致fast->next!=NULL&&fast->next-&

【Leetcode解题报告】快慢指针

快慢指针中的快慢指的是移动的步长,即每次向前移动速度的快慢.例如可以让快指针每次沿链表向前移动2,慢指针每次向前移动1次. Leetcode 141 Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 分析与解法 大家可以想一下上体育课长跑的情景.当同学们绕着操场跑步的时候,速度快的

HDU 5510---Bazinga(指针模拟)

题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, please sit up straight.Don't tilt your head. I'm serious.For n given strings S1,S2,?,Sn, labelled from 1 to n, you should find the largest i (1≤i≤n) su

将两个排好序的序列合并成一个(指针和数组分别实现)

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 12 List

程序设计基石与实践系列之类型提升、内存分配,数组转指针、打桩和矢量变换

英文出处:Peter Fa?ka: Guide to Advanced Programming in C C语言可用于系统编程.嵌入式系统中,同时也是其他应用程序可能的实现工具之一. 当你对计算机编程怀有强烈兴趣的时候,却对C语言不感冒,这种可能性不大.想全方位地理解C语言是一件极具挑战性的事. Peter Fa?ka 在2014年1月份写下了这篇长文,内容包括:类型提升.内存分配,数组转指针.显式内联.打桩(interpositioning)和矢量变换. 整型溢出和类型提升 多数C程序员以为,

21.Merge Two Sorted Lists(法1头部附加节点法2二级指针)

Merge two sorted linked lists and return it as a new list. The new listshould be made by splicing together the nodes of the first two lists. HideTags Linked List #pragma once #include<iostream> using namespace std; struct ListNode { int val; ListNod

[模板]洛谷T3391 文艺平衡树 链表&amp;递归版、无父指针版Splay

指针大法好 无父指针Splay大法好 大佬们的"改变旋转方向"萌新表示不懂,于是就自己乱搞出了下面的搞法... 代码如下,萌新写的丑,诸位大佬见谅QwQ~ 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<ctime> 5 #include<cstdlib> 6 #include<ctime> 7 8 #include<

LeetCode OJ 143. Reorder List(两种方法,快慢指针,堆栈)

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. Subscribe to see which companies asked this quest

【C/C++学院】0823-静态联合编译与动态联合编译/父类指针子类指针释放/虚函数/纯虚函数概念以及虚析构函数/抽象类与纯虚函数以及应用/虚函数原理/虚函数分层以及异质链表/类模板的概念以及应用

静态联合编译与动态联合编译 #include <iostream> #include <stdlib.h> //散列 void go(int num) { } void go(char *str) { } //class //::在一个类中 class A { public: void go(int num) { } void go(char *str) { } }; void main() { ///auto p = go;编译的阶段,静态联编 void(*p1)(char *s