1. Definition of pointers. 2. Declaration of pointers (especially multiple point
ID: 3686306 • Letter: 1
Question
1. Definition of pointers.
2. Declaration of pointers (especially multiple pointers in a line)
3. Initialize a pointer from the address operator
4. Dynamic allocation of variables
5. Return multiple values in functions.
6. Figure out the values of pointers and their associated values they point to.
7. Define a pointer to a pointer.
8. Draw memory map.
9. Syntax to dynamically allocate memory for variables.
10. Read programs and figure out the contents of variables that pointers point to.
11. Read programs and figure out what they implement and the outputs
Explanation / Answer
Q.
1. Definition of pointers
A. A pointer is a variable which contains the address in memory of another variable.
We can have a pointer to any variable type.
The unary or monadic operator & gives the "address of a variable".
The indirection or difference operator * gives the contents of an object pointed to by a pointer.
2. Declaration of pointers
A. A "pointer declaration" names a pointer variable and specifies the type of the object to which the variable points.
A variable declared as a pointer holds a memory address.
Pointer vaiable declaration:
________________________________
[box] * can appears anywhere between Pointer_name and Data Type[/box]
int *p;
int * p;
int * p;
Multiple pinters in a line:
int *x, *y, *z;
3. Initialize a pointer from the address operator
A. 1. Declare a Pointer Variable and Note down the Data Type.
2. Declare another Variable with Same Data Type as that of Pointer Variable.
3. Initialize Ordinary Variable and assign some value to it.
4. Now Initialize pointer by assigning the address of ordinary variable to pointer variable.
Example:
#include<stdio.h>
int main()
{
int a; // Declaration of variable
int *ptr; //Declaration of pointer variable
a = 10; // Initialization of variable
ptr = &a; // Initialization of a pointer variable
return(0);
}
4. Dynamic allocation of variables
A. Memory allocated ön the fly"during run time
Dynamically allocated space usually placed in a program segment known as the heap or the free store
Exact amount of space or number of items does not have to be known by the compiler in advance.
For dynamic memory allocation,pointers are crucial
Dynamic allocation having 2 steps:
1. Ceating the dynamic space.
2. Storing its address in a pointer
5. Return multiple values in functions.
A.
Syntax:
_______
int foo(int arg1, int arg2);
7. Define a pointer to a pointer.
A. A pointer to a pointer is a form of multiple indirection,or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which point6s to the location that contains the actual value.
9. Syntax to dynamically allocate memory for variables.
A.
int * foo;
foo = new int [5];
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.