Pointers, Pass by Reference, Passing Arrays and C-Strings, Pointer Arithmetic

Pointer

 

Firstly, let’s review how to print the hexadecimal(16 base)

 

Remember: Every time the program is run, the address of data will likely be different.

 

 

 

 

When declare pointers for different types of variables:

Notice : The type of the right-hand side must match the type of the left-hand side.

 

Dereferencing:

It is possible to take a memory address and get the data that this address points to

Dereferencing is also known as indirection... as we can access data in memory indirectly, via pointer

 

 

 

 

When run it, compile will crash because the null pointer cannot be found.

 

The Dangling Pointer

A dangling pointer is a pointer that once stored a valid address, but the address has since become invalid

 

Remember, local variables, and their contents, no longer valid when a function return.

 

 

Pass by Reference

Pointers can be used as parameters to functions

Allowing the calling function to pass in the address of a piece of memory

This allows the function to refer to the caller’s memory

 

Swap data:

In this sample, a and b are only accessible in the bad_swap function, When the function returns, a and b disappear

Also, the call to bad_swapdoes not change the data stored in x and y inside the main function

The swap function is using x and y to calculate actually….(not the a and b)

 

Returning Multiple Things from Functions

 

 

 

A Pointer to a Structure

Declaring a pointer named: p_lecturer

 

 

The ->Operator

The ->operator allows access to the members of a structure via pointer

Because the for the .operator, cannot use the pointer directly.

 

Passing Structures to function via pointer

 

 

Arrays and Pointers

As the name of an array is actually a pointer to the first element in the array.

Passing Arrays as Parameters

(Arrays are passed to functions by reference)

The callee can alter the caller’s data

 

Passing Arrays using Pointers

Arrays can be passed into functions via pointer.

The name of the array is actually a pointer to the first element

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值