Skip to main content

Pointers

Pointers:

A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value.

Syntax:

type * variable ;

Interpretation:

The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type.



Dynamic Memory Management:

C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type.
The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management.

Syntax:

int *num = (int *)malloc(sizeof (int)*numCount);
or
int *ptr = (int *)calloc(numCount, sizeof (int));
/* returns a pointer to a section of memory just large enough to hold the integers, whose quantity is being obtained through user input stored in the variable numCount. */
free(ptr);     //returns to the system whatever memory was pointed to by ptr.

POINTER TO POINTER(Double Pointer) DECLARATION:


Syntax:

type ** variable ;

Interpretation:

The value of the pointer variable ptr2ptr is a memory address of another pointer.


Implementation of Pointers in C:


#include <stdio.h>
#include<malloc.h>

int main()
{

int row,col,x,y;

printf(“Enter number of rows:”);

scanf(“%d”,&row);

printf(“Enter number of columns:”);

scanf(“%d”,&col); //allocate memory for xs

int **ptr = (int **)calloc(row, sizeof (int*));

//for each x allocate memory for yumns

for(x=0;x<row;x++)

*(ptr+x) = calloc(col,sizeof(int)); //Storing elements

for (x=0;x<row;x++)

for(y=0;y<col;y++)

{
printf(“Enter number: “);

scanf(“%d”,&ptr[x][y]);

}

//Displaying pointer to pointer to ptr

printf(“nYou have entered:n”);

for (x=0;x<row;x++)
{
for(y=0;y<col;y++)
{
printf(“%d “,ptr[x][y]);
}
printf(“n”);
}

free(ptr);
//returns to the system whatever memory was pointed to by ptr.

return 0;
}

Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...