0% found this document useful (0 votes)
3 views

Arrays and pointers

The document explains the concepts of arrays and pointers in C programming. It covers how to declare, initialize, and use arrays, as well as how to work with pointers to store memory addresses and manipulate variable values. Examples of code snippets demonstrate the practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Arrays and pointers

The document explains the concepts of arrays and pointers in C programming. It covers how to declare, initialize, and use arrays, as well as how to work with pointers to store memory addresses and manipulate variable values. Examples of code snippets demonstrate the practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Arrays and pointers

An array is a variable that can store multiple values. For example, if you want
to store 100 integers, you can create an array for it

int data[100];

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

Here, we declared an array, mark , of floating-point type. And its size is 5.


Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed
once it is declared.

How to initialize an array?


It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this.


int mark[] = {19, 10, 8, 17, 9};

Here, we haven't specified the size. However, the compiler knows its size is 5
as we are initializing it with 5 elements.

Here,

mark[0] is equal to 19

mark[1] is equal to 10

mark[2] is equal to 8

mark[3] is equal to 17

mark[4] is equal to 9

This program declares an integer array numbers of size 5. It then uses a loop to input five
numbers from the user and stores them in the array. Finally, it displays the entered numbers
using another loop.
#include <stdio.h>

int main() {
// Declare an array to store numbers
int numbers[5];

// Input numbers into the array


printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; ++i) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}

// Display the numbers


printf("\nEntered numbers are:\n");
for (int i = 0; i < 5; ++i) {
printf("%d\n", numbers[i]);
}

return 0;
}

Pointers

Address in C
If you have a variable var in your program, &var will give you its address in the
memory.
We have used address numerous times while using the scanf() function.

scanf("%d", &var);

Here, the value entered by the user is stored in the address of var variable.
Let's take a working example.

#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);

// Notice the use of & before var


printf("address of var: %p", &var);
return 0;
}

Output

var: 5
address of var: 2686778
C Pointers
Pointers (pointer variables) are special variables that are used to store
addresses rather than values.

Pointer Syntax

int* p;
int *p1;
int * p2;

Example: C program that uses pointers to swap the values of two variables:

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

// Display the original values


printf("Original values: num1 = %d, num2 = %d\n", num1, num2);

// Swap values using pointers


swap(&num1, &num2);

// Display the swapped values


printf("Swapped values: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

You might also like