Sample


[br]9 [br]<strong>Every variable has got its own address</strong> in the computer's memory. You can get that address and store it in another variable called <strong>pointer</strong>. A pointer is created putting an <strong>asterisk</strong> before its name during its definition: [crayon-5df243b5452d0544641251/] <strong>A pointer can access the value of the variable it points to</strong>, but modifying the value of the first doesn't affect the second one's However, since the value of a pointer is an address and you can use that to access a variable, you can change its value indirectly. So if <code>p</code> points to <code>var</code>, <strong>this will change p's value but not var's</strong>: [crayon-5df243b5452dd355255040/] this instead accesses the variable and <strong>chages its value, but not p's</strong>: [crayon-5df243b5452e1131142898/] Let's see how to <strong>get the address</strong> of a variable - <em>so that we can assign it to a pointer </em>- and how to <strong>get the value</strong> of the variable pointed by a pointer - <em>so that we can use it indirectly</em>. The <code>&</code> symbol <strong>gets the address</strong> form a variable. [crayon-5df243b5452e3221729152/] The <code>*</code> symbol <strong>gets the value</strong> of the pointed variable. [crayon-5df243b5452e6117076420/] <strong>What is the address of an array - <em>and so of a string too</em>?</strong> <strong>The array itself is a pointer to its first element.</strong> Therefore, the piece of code below: [crayon-5df243b5452e8728634183/] is equal to: [crayon-5df243b5452eb985063202/] When we get an element from an array using <code>arr[i]</code>, we're just writing a short version of <code>*(arr+i)</code>. When you <strong>add a whole number to a pointer</strong>, you make it point to a different address. The new address is calculated as follows: <code>new=old+(number*dimension_of_its_data_type)</code>. This is why <strong>the data type of a pointer is important</strong>, even if all of them store addresses. Adding 1 to an int pointer, we'll move its address of 4 bytes, while adding the same number to a char pointer, we'll move its address of 1 byte only. [br]https://itszephyro.com/blog/learn-c/pointers/[br]#include <stdio.h> int main() { int *pointer; int value1 = 10; int value2 = 20; int asw; printf("Address of value1: %p\nAddress of value2", &value1, &value2); printf("Which variable do you want the pointer to point to? (1 or 2): "); scanf("%d", asw); if(asw==1) { pointer = &value1; }else{ pointer = &value2; } printf("The value found at %p is %d", pointer, *pointer); } [br]

[br]9
[br]Every variable has got its own address in the computer’s memory.

You can get that address and store it in another variable called pointer.

A pointer is created putting an asterisk before its name during its definition:

A pointer can access the value of the variable it points to, but modifying the value of the first doesn’t affect the second one’s

However, since the value of a pointer is an address and you can use that to access a variable, you can change its value indirectly.

So if p points to var, this will change p’s value but not var’s:

this instead accesses the variable and chages its value, but not p’s:

Let’s see how to get the address of a variable – so that we can assign it to a pointer – and how to get the value of the variable pointed by a pointer – so that we can use it indirectly.

The & symbol gets the address form a variable.

The * symbol gets the value of the pointed variable.

What is the address of an array – and so of a string too?

The array itself is a pointer to its first element.

Therefore, the piece of code below:

is equal to:

When we get an element from an array using arr[i], we’re just writing a short version of *(arr+i).

When you add a whole number to a pointer, you make it point to a different address.

The new address is calculated as follows: new=old+(number*dimension_of_its_data_type).

This is why the data type of a pointer is important, even if all of them store addresses.

Adding 1 to an int pointer, we’ll move its address of 4 bytes, while adding the same number to a char pointer, we’ll move its address of 1 byte only.

[br]https://itszephyro.com/blog/learn-c/pointers/[br]#include

int main() {
int *pointer;
int value1 = 10;
int value2 = 20;
int asw;
printf(“Address of value1: %p\nAddress of value2”, &value1, &value2);
printf(“Which variable do you want the pointer to point to? (1 or 2): “);
scanf(“%d”, asw);
if(asw==1) {
pointer = &value1;
}else{
pointer = &value2;
}
printf(“The value found at %p is %d”, pointer, *pointer);
}
[br]

To learn more, visit: