指针的概念:地址、指针变量的定义与使用

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers


Int i=1,j=2,k=3; // int 2 i 2000 j 2002 k ______

2004

int b=3; int *a=&b;, *a ______

3

: & ______

Signup and view all the answers

p______ *pp

Signup and view all the answers

int a, *p; p=&a;, *p ______

<p>a</p> Signup and view all the answers

void swap(int *p1, int *p2), ______

Signup and view all the answers

return ______

Signup and view all the answers

P p+i a+i a[i] ______ai

Signup and view all the answers

*(&a[0]) &a[0] ______

<p>a</p> Signup and view all the answers

,

Signup and view all the answers

Char"______"

<p>%s</p> Signup and view all the answers

p1 p2 a b a b *p2=*p1 '______''*string'

<p>ilovechina</p> Signup and view all the answers

Sub,global______

Signup and view all the answers

Malloc,, #include <______>

<p>stdlib.h</p> Signup and view all the answers

Malloc______

Signup and view all the answers

pointvoid int(int &)______

<p>point</p> Signup and view all the answers

Int argc, char *argv______ main argv______

Signup and view all the answers

Flashcards

地址的含义

在程序中定义了一个变量时,系统会分配内存单元。每个字节拥有编号,这个编号就是地址。

引用变量的值

在程序中通过变量名来引用变量的值。

定义指针变量

类型名 *指针变量名;

指针赋值

确保赋值的值是地址,且指针类型一致。

Signup and view all the flashcards

指针变量赋值

将变量的地址赋给指针变量。

Signup and view all the flashcards

指针访问数组元素

通过已知的首个元素的地址,访问数组中其他元素。

Signup and view all the flashcards

按地址传递的优点

向函数传递变量的地址 , 允许它修改变量的值。

Signup and view all the flashcards

指向函数的指针。

把函数的入口地址作为参数传递到其他函数。

Signup and view all the flashcards

动态内存分配函数 malloc

void *malloc(unsigned size)

Signup and view all the flashcards

动态内存分配函数 calloc

void *calloc (unsigned n, unsigned size)

Signup and view all the flashcards

释放内存空间的函数 free

void free (void *ptr)

Signup and view all the flashcards

命令行参数argc,*argv[]

(argc argument count 的缩写,意思是参数个数) (argv argument vector 缩写,意思是参数向量)

Signup and view all the flashcards

字符指针变量

char *string=\

Signup and view all the flashcards

Study Notes

指针的概念

  • 如果在程序中定义了一个变量,系统会分配内存单元
  • 编译系统根据变量的类型分配一定长度的空间
  • 内存区的每一个字节有一个编号,这个编号就是“地址”
  • 地址指向对应的变量单元的引用称为 “指针”

地址和指针

  • 变量 i,j,k 分别被分配地址 2000, 2002, 2004
  • 对应内容 (值) 分别为 1, 2, 3
  • Int类型的变量通常占 2 个字节

变量名的引用

  • 通常通过变量名来引用变量的值
  • 直接访问: 直接使用变量名访问
  • 间接访问: 将变量的地址存放在另一变量(指针变量)中,通过该指针变量找到对应变量的地址,从而访问变量

指针变量的定义

  • 定义格式: 类型名 * 指针变量名;
  • int *a, int b; 定义了int指针变量a, int 变量b;
  • int *a = &b; 是将int指针类型的变量a指向b的地址
  • 指针变量的类型需要和指针变量指向的类型保持一致
  • a = &b;将变量b的地址赋值给a,指针变量赋值必须是一个地址

指针变量的使用示例

  • 首先定义int变量a为100,b为10
  • 然后定义指向int的指针变量pointer_1pointer_2
  • 使用&取地址赋值给指针
    • pointer_1 = &a; // pointer_1 指向 a的地址
    • pointer_2 = &b; // pointer_2 指向 b的地址
  • 可以通过指针访问对应的值*pointer_1*pointer_2

指针变量的引用

  • 给指针变量赋值
  • 引用(访问)指针变量指向的变量
  • 引用指针变量的值

指针变量示例

  • 声明int变量 a 和指针 *p
  • p = &a;就是将a的地址赋值给指针变量p
  • 使用*p来访问p所指向的值,即访问变量a
  • 可以通过*p = 1修改指针指向的变量的值,这等价于 a = 1

注意

  • &是取地址运算符,&a就是变量a的地址
  • * 是指针运算符(或称“间接访问”运算符),*p 代表指针变量 p指向的对象的值

指针变量的使用要点

  • 解题思路: 不直接交换变量的值,而是交换两个指针变量的值 (地址)
  • 可以通过交换了的指针, 访问到对应的变量

指针变量的类型

  • 指针变量可以是 int *类型
  • 需要使用指针变量指向的地址p1 = &a
  • 如果需要调换指向需要注意
    • p = p1p2 的值互换
    • printf("a=%d,b=%d\n",a,b); 仍然打印原变量ab的值
    • printf("max=%d,min=%d\n",*p1,*p2); 打印交换后的指针对应的数据

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Puntatori in C++: Concetti Base
25 questions
Pointers and Addresses in C++
37 questions

Pointers and Addresses in C++

FavorableQuatrain7724 avatar
FavorableQuatrain7724
Pointers in C
10 questions

Pointers in C

LushLouvreMuseum6525 avatar
LushLouvreMuseum6525
Use Quizgecko on...
Browser
Browser