<style type="text/css">
<!--
@page
{margin:0.79in}
p
{margin-bottom:0.08in}
h3
{margin-bottom:0.08in}
h3.western
{font-family:"Arial",sans-serif}
h3.cjk
{font-family:"AR PL UMing HK"}
h3.ctl
{font-family:"Lohit Hindi"}
-->
</style>
Pointers to pointers, pointer and string arrays, multidimensional arrays. Stacks and queues.
Pointer pointers
• What does this function do?
void swap (int ∗∗a , int ∗∗b){
int ∗temp = ∗a;
∗ a = ∗b;
∗b = temp;
}
• How does it compare to the familiar version of swap?
void swap ( int ∗a , int ∗b ){
int temp = ∗a;
∗a = ∗b;
∗b = temp;
}
第一个是交换了两个指针,内存中的数据并没有改变;第二个指针没变,但内存中的数据变了。
Pointer array
• Have an array int arr [100]; that contains some numbers
• Want to have a sorted version of the array, but not modify arr
• Can declare a pointer array int ∗ sorted_array[100];containing pointers to elements of arr and sort the pointers insteadof the numbers themselves
• Good approach for sorting arrays whose elements are very large(like strings)
在struct的排序中,pointer会发挥很大的作用。
String arrays
• An array of strings, each stored as a pointer to an array ofchars
• Each string may be of different length
char str1[] = "hello"; /∗length = 6 ∗/
char str2[] = "goodbye";/∗ length = 8 */
char str3[] = "ciao"; /∗length = 5 ∗/
char ∗ strArray[ ] = { str1,str2, str3};
• Note that strArray contains only pointers, not the charactersthemselves
数组存储的不是字符,而是指针,因此要注意自动变量的作用域,如果是用malloc分配的堆内存,则需要注意free的时机。
Multidimensional arrays
• C also permits multidimensional arrays specified using []brackets notation:
int world [20][30]; is a 20x30 2-Darray of int’s
• Higher dimensions possible:
char bigcharmatrix [15][7][35][4]; –what are the dimensions of this?
• Multidimensional arrays are rectangular; pointer arrays can bearbitrary shaped
二维char数组和char*数组的区别?我想二维char数组在内存中是一块连续的空间,通过指针代数来获取其中元素的地址,char*数组则是一维数组,每个元素都是指针,两者最大的不同在于二维char数组中是存储对象的,而char*数组则不存储任何对象。
分享到:
相关推荐
资料目录.bat Advice to next year student.doc lec1.ppt lec10.ppt lec11.ppt lec12.ppt lec13.ppt lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt lec19.ppt ...lec6.ppt lec7.ppt lec8.ppt lec9.ppt
In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...
programming in computing 10a lec2
EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf
Lec 00 Introduction and Course Overview Lec 01 Bezier Curves and Splines Assignment 0 Lec 02 Curves Properties and Conversion, Surface Representation Lec 03 Coordinates and Transformations Lec 04 ...
EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf
lec7采购的内部控制.pptx
6 PNNI: Private Network Node Interface ...................... 151 6.1 Introduction ................................... 151 6.1.1 Introduction to the PNNI Routing Protocol .............. 151 6.1.2...
lec4.pptx
可通过改程序进行危险源的LEC分值的客观化处理。
算法设计与分析:2-Lec6 .pdf
算法设计与分析:2-Lec6.pdf
麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。
Week1—4_Note_Lec1—6.pdf
2022-专业英语-Lec7-8.pptx
算法设计与分析:3-Lec7.pdf
Lec6-企业合并会计.pptx
demo_Lec.m
Lec1-Introduction.pdf.zip
study.c_plus_plus.lec.5