Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Pointers:

Sure! Let's talk about pointers in C using simple language

What is a Pointer?

A pointer in C is a special type of variable that stores the memory address of another variable. Think of it like a label that tells you where something is stored in memory.

How to Declare a Pointer?

To declare a pointer, you use the '*'symbol before the variable name. For example:

int *ptr;

This declares a pointer variable ptr that can store the memory address of an integer.

How to Assign a Value to a Pointer?

You can assign the memory address of a variable to a pointer using the address-of operator &. For example:

int x = 10;
int *ptr = &x;

This assigns the memory address of variable x to the pointer ptr.

How to Access the Value at a Memory Address?

You can access the value stored at a memory address using the dereference operator *. For example:

int x = 10;
int *ptr = &x;

printf("The value of x is: %d\n", *ptr);

This will print the value of x, which is 10, because ptr stores the memory address of x, and *ptr accesses the value stored at that memory address.

Example:

Let's say we have a program that swaps the values of two variables using pointers:

#include <stdio.h>

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;

printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}

In this example



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java