-
Notifications
You must be signed in to change notification settings - Fork 15
/
declaring-pointers.c
63 lines (47 loc) · 2.14 KB
/
declaring-pointers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
------------------------------------------------------------------------------------
Tutorial: This tutorial explains the concepts of pointers in C programming.
Pointers (pointer variables) are special variables that are used to store addresses rather than values.
Pointer Syntax
Pointers can be declared in the following three ways:
dataType* pointerVariableName;
dataType *pointerVariableName;
dataType * pointerVariableName;
e.g., int *p; // declares pointer variable p of type int
NOTE:
1. * is called the dereference operator (when working with pointers)
2. The pointer variable should be of the same datatype as that of the variable whose address is to be stored in it.
Assigning addresses to Pointers:
pointerVariabaleName = &var;
// & is the address of operator
// here var refers to the variable whose address is to be stored
e.g., int *p, c;
p=&c; // assigning of c's address to p
------------------------------------------------------------------------------------
*/
// Code here explaining concept with comments to guide
#include<stdio.h>
int main()
{
int *pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c);
pc = &c; // assigning value of c's address to pc
printf("Address of c: %p\n", pc);
printf("Content of c: %d\n", *pc); // 22
printf("Address of pc: %p\n", &pc);
printf("Content of pc: %p\n\n", pc); // same as the address of c
c = 11; // assigning new value to c
printf("Address of c: %p\n", pc);
printf("Content of c: %d\n\n", *pc); // 11
*pc = 2; // assigning new value to c using it's pointer variable pc
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
/*
----------------------------------------------------------------------------------------------------------------------------------
Challenge: Write a C program declaring a float variable and display the memory address of the variable using a pointer variable.
----------------------------------------------------------------------------------------------------------------------------------
*/