100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Study Guide on Pointers

Rating
-
Sold
-
Pages
20
Uploaded on
01-09-2022
Written in
2022/2023

This is a study guide to the subject of pointers, especially in terms of C.

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
September 1, 2022
Number of pages
20
Written in
2022/2023
Type
Other
Person
Unknown

Subjects

Content preview


Pointers For Notes
Tags
Things to learn before learning about pointers
What are pointers?
Manipulation with Pointers
Usage of Pointers
Character Array and Pointers
Multi-dimensional Array
Dynamic Memory
Pointers and Functions


Things to learn before learning about pointers
In a typical memory architecture, consider the entire main memory to be divided into
segments of 1 byte. Each byte of memory will have an address.

What happens when we declare a variable?
Let’s say you have the following declaration of variable.


int a;




The compiler will allocate some memory to the variable ‘a’, where the size of the memory
depends on the type of the variable. Here is the list of typical size of memory of different
types:

int — 4 bytes

char — 1 bytes

float — 4 bytes

A internal lookup table will keep track of the memory allocation situation.

What happens when we initialise/modify a variable?
Let’s say we assign a value to the variable when just declare.



Pointers For Notes 1

, a = 5;




The compiler will look into the lookup table. Find the corresponding address. Then write
the value to that memory in binary.


What are pointers?
What are pointers?
Pointers are variables, which they store the address of another variable as values.

In other words, the pointer points to some value of another variable



💡 Bear in mind that since pointers are variables, they will have addresses as well.




How to declare a pointer in C/C++?

int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
printf("the value of a: %d \n", a);
printf("the address of a: %p \n", &a); // &a should have the same value as p
printf("the value of p: %p \n", p);
printf("the address of a: %p \n", &p);




Explanation:

A pointer is declared by adding * in front of the variable name, behind the data type.
The asterisk sign indicates the variable name will be a pointer.

The data type can be any data type: int, float, char, or user-defined classes etc.

& followed by a variable name will return the address of the variable.

It is worth noting that &p will actually return the address to the pointer p itself, not
the content of p, which just happens to be an address.

Try running the above code to see the results.




Pointers For Notes 2

, 💡 Make sure you always initialise the pointer before using it, or else there will be
an runtime error




How to get the value correspond to the address in a pointer?
Up till this point, the pointer is merely another normal variable that stores another
variable’s address. What makes pointer special is that we can get the normal variables’
values through the pointer.


int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;

printf("The value of a: %d \n", a); // a should be equal to *p
printf("Let's try to get the value of a through p: %d", *p);




Explanation:

The process of getting values of the address that pointers points to are called “de-
referencing”.

We do this by adding an asterisk * in front of the variable name:

This is not to be confused with the pointer declaration, despite having similar
syntax.

Try running the code to see the results


How to modify the value correspond to the address in a pointer?
We can also modify the value through a pointer.


int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;

// Now I will change the value of a to 11
*p = 11;
printf("The value of a: %d \n", a); // a still equals to *p, but now instead of 5, it is 11
printf("Let's try to get the value of a through p: %d", *p);




Pointers For Notes 3
$3.39
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
arnoldli

Get to know the seller

Seller avatar
arnoldli HKUST
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions