Double pointers shadowing

Hello, and welcome back.

Welcome to your first challenge for this section on advanced pointers.

This challenge will be all about double pointers.

So we’re gona to have a couple challenges your first challenge is

going to test your understanding of double pointers, basically the syntax.

How to declare a pointer to a pointer, how to initialize the double pointer

and how to access a double pointer.

Again, I want to give you familiar with the syntax understanding

when to apply the double asterix, when to de-reference.

And then you can do things in many different ways syntactically

with double pointers.

So you need to write a program that creates, assigns and

accesses some double pointers.

I want you to create a normal integer variable, a non-pointer and I want

you to assign it a random value.

Then I want you to create a single integer pointer

variable, single pointer.

Then create a double integer pointer variable, and then assign the address

of the normal integer variable in step one to the single pointer in step two.

Then assign the address of the single pointer step two to the double

pointer variable in step three.

And so that step three sounds a little confusing because

it says double integer.

What that means is a pointer to a pointer that’s an integer.

Okay, not a double data type, a double pointer, so a double pointer variable.

So again, first step just a normal integer, second step single

pointer, third step double pointer.

And then this is just for setting up.

So then you’re going to set up your single pointer

and your double pointer.

And so you can display a lot of the information using

a lot of different syntax.

So first I want you to display all the possible ways to find the value

of the normal integer variable because using through those three variables.

I want you to then display all the possible ways to find the

address of the normal energy variable, again, using the three

variables that I mentioned, the integer, the double, the single

pointer and the double pointer.

And then you have to find all the possible ways to find the value

of the single pointer variable and then all the possible ways

to find the address of the single pointer variable and lastly all the

possible ways to print the double pointer value and the address.

And again, you can do this in many, many different ways.

Here’s some example output for charge number one.

So first you’re going to print out all the possible ways to find the

value of the normal integer variable.

And there’s going to be three possible ways.

One using a regular number, one using single pointer and

one using the double pointer.

Then you need to find all the possible ways to find the address

of the normal integer variable.

And again, there’s three ways, using the num, single

pointer and double pointer.

Next one would be to find all the possible ways to find the value

of the single pointer variable.

And here, there are actually just two ways using the single pointer

and double pointer and then all the possible ways to find the address

of the single pointer variable.

Again, there’s two ways using the single pointer and the double pointer.

And then lastly, all the possible ways to print the double

pointer value and its address.

Now obviously, you can only use the double pointer so we print out

the value of the double pointer and the address obviously the value

of the double pointer is going to be the single pointer address.

And so this is some example output, gives you a little hint on how

many different ways syntactically, you can show the same information.

Challenge number two.

Again, we’re going to test in your understanding of double pointers

used as an arguments to functions.

So you need to write a program that includes a function that modifies

a pointer’s value, the pointer’s value not what the point is

pointing to, but the pointers value.

Understand the difference there.

The actual value of the pointer which is usually an address.

And whether it’s a double pointer it’s a address to another pointer

that’s pointing to something.

Essentially this program will be simulating pass by

reference in the c language.

You want to change the value of the pointer pass to a function

as the function argument.

So first, create a function named allocate memory that takes a single

integer pointer as a function parameter, something like void

allocate memory in star pointer.

The function should allocate memory for this pointer.

Now create a main function that does the following, creates an

integer pointer and initializes the null, invokes the allocate

memory function passing it the integer pointer just created.

Then assign a value to the integer pointer that it’s pointing to

de-reference it, then print the value of what the pointer

is pointing to de-reference it, and then free the pointer.

So the idea here is that in the main you’re passing a pointer

into a function and the function’s going to allocate memory.

What you’re going to notice is something very important here when

you pass a pointer to a function that accepts a single pointer argument,

it’s not going to be allocating memory for what you think it is.

So that’s your first part of the program.

What is the output of the program, why is this the output, make sure

you understand why that’s the output.

The next part of the program is to modify your program

and use a double pointer.

So modify the function named, allocate memory to take a double pointer of

type int as a function parameter, something like void allocate

memory into star star pointer.

And what this function should do is it should call malloc and

allocate memory for this pointer.

You have to use the correct syntax.

Now modify your main function that does the following, creates an

integer pointer initializes the null, invokes to allocate memory function

passing the address of the inner pointer just created because that’s

how you can pass in a double pointer if you have an existing pointer

and you take the address of it.

Then assign a value to the integer pointer that is pointing

to, de-reference it, print the value of the pointer it’s

pointing to and free the pointer.

And then understand what the output of the program is and understand why

this isn’t the output, two separate parts to channels number two.

And just a couple of reminders if you pass a single pointer in as

an argument, you’ll be modifying local copies of the pointer, not the

original pointer in the calling scope.

With a pointer to a pointer, you modify the original pointer passed in.

Use the double pointer as an argument to a function when you want to

preserve the memory allocation or assignment value of the pointer

even outside of the function.

And so on the next lecture, I’ll provide a solution and a demonstration

for further understanding please.

Let me know if you have any questions.

Thank you.

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *