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

Array & Pointer & String

The document provides an overview of arrays, pointers, and strings in C programming. It explains the declaration, initialization, and usage of arrays, as well as the concept of pointers and their operations. Additionally, it covers string manipulation and standard library functions related to strings.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Array & Pointer & String

The document provides an overview of arrays, pointers, and strings in C programming. It explains the declaration, initialization, and usage of arrays, as well as the concept of pointers and their operations. Additionally, it covers string manipulation and standard library functions related to strings.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Objectives

 Concept of Arrays
 Concept of Pointers
 Concept of String
Arrays

 Array is a set of similar data types.


 It is a group of similar quantities.
 Similar elements could be all ints,or all floats,
or all chars etc.
 Example,
int marks={ 48,88,34,23,89}
Declaration of Arrays
 When declaring arrays, specify
 Name
 Type of array
 Number of elements
arrayType arrayName[ numberOfElements ];
 Examples:
int c[ 10 ];
float myArray[ 3284 ];
 Declaring multiple arrays of same type
 Format similar to regular variables
 Example:
int b[ 100 ], x[ 27 ];
Array Initialization
If size is defined
int num[6]={2,4,12,5,45,5}
If size omitted, initializes determine it
int n[]={ 2,4,12,5,45,5}
float press[]={ 12.3,34.2,-23.4,-11.3}
-mentioning array size is optional
C arrays have no bounds checking

# include<stdio.h>
Void main()
{
int num[40],i;
for(i=0;i<=100;i++)
num[i]=i;

}
A Simple Program using
Array
/*Array Initialization*/
# include<stdio.h>
Void main()
{
int avg,sum=0;
int i;
int marks[30];
/*Entering Data into an Array*/
for(i=0;i<29;i++)
{
printf("\nenter marks")'
scanf("%d",&marks[i]);
}
/*Read Data from an Array*/
for(i=0;i<29;i++)
{
sum=sum+marks[i];
avg=sum/30;
printf("\n average marks=%d",avg);
}
Representation of Array elements in
memory Name of array (Note that all elements of
To refer to an element, specify
this array have the same name, c)
 Array name
 Position number c[0] -45
 Format: c[1] 6
arrayname[ position number ] c[2] 0
 First element at position 0 c[3] 72
 n element array named c: c[4] 1543
 c[ 0 ], c[ 1 ]...c[ n – 1 ] c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78

Position within array c


number of the element
More Different Types of Array
 2-D array
Two Dimensional array
int ttt[3][3]={
{12, 44}
{23, 65}
{87,55}
{65,12}
}
Multidimensional Array
int arr[2][3][4]=
{
{6,8,7,8}
{3,6,9,6}
{2,7,1,3}
}
{
{2,8,5,8}
{1,2,3,4}
{4,5,6,9}
}
What is pointer??

Pointer is a user defined data type which creates special


types of variables which can hold the address of data types
like char, int, float, double etc.
A pointer variable must be declared before it can be
used.
A pointer indirectly references a value. Referencing a
value through a pointer is called indirection.

int a=5;
int * ptr;
ptr=&a;
Use of & and *
 When is & used?
 When is * used?

 & -- "address operator"


which gives or produces the
memory address of a data
variable
 * -- "dereferencing operator"
which provides the contents
in the memory location
specified by a pointer
Concept of Address and
Pointers
ADDR1 Contents1
ADDR2
ADDR3
Memory can be
ADDR4
conceptualized as a ADDR5
linear set of data locations. ADDR6
*
Variables reference the contents of *
a locations *
Pointers have a value of the ADDR11 Contents11
address of a given location
*
*
ADDR16 Contents16
Pointer example…

int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&ptr1;

int *ptr1 // declaring a pointer of integer type//


Int **ptr2//declaring a pointer to a pointer//
printf ("\n pointer's value: %d", ptr1);
/* Output the value (an address) */
Printf ("pointer's address: %d", &ptr1); /* Output the address */
Printf ("\n value pointed to: %d \n", *ptr1); /* Value at the address */
Arithmetic and Logical
Operations on Pointers

A pointer may be incremented or decremented

An integer may be added to or subtracted from a pointer.

Pointer variables may be subtracted from one another.

Pointer variables can be used in comparisons,


but usually only in a comparison to NULL.
Pointers and Arrays
{
int i=3,*x;
OUTPUT:
float j=1.5,*y;
char k='c',*z;
printf("\nValue of i=%d",i); Value of i=3
printf("\nValue of j=%f",j); Value of j=1.500000
printf("\nValue of k=%c",k); Value of k=c
x=&i; Original address in x=65524
y=&j; Original address in y=65520
z=&k; Original address in z=65519
printf("\nOriginal address in x=%u",x); New address in x=65526
printf("\nOriginal address in y=%u",y); New address in y=65524
printf("\nOriginal address in z=%u",z); New address in z=65520
x++;
y++;
z++;
printf("\n New address in x=%u",x);
printf("\n New address in y=%u",y);
printf("\n New address in z=%u",z);
}
Pictorial representation
String
 What is String?
The term string is used in C to describe a
contiguous sequence of characters
terminated by a null (‘\0’).

 String is also called Character array .


Memory allocation
Ex: Char name[]= “HELLO”;
The way string character stored in memory

H E L L O \0

4001 4002 4003 4004 4005 4006


Example
 Program Ex:
#include<stdio.h>
#include<String.h>
#include<conio.h>

void main()
{
char name[]= "Hello"; //initialize String
for(int i=0;i<=4;i++)
printf("%c",name[i]); // print the charecter
}

Output: Hello
String function
 Some standard library string function
Strlen(): Find the length of the string.
Strcat(): Appends one string at the end of
another.
Strcpy(): Copies a string into another.
Strcmp(): Compares two strings.
Strrev(): reverse a string.
Function example
 Example of strlen():
#include<String.h>
void main()
{
char name[]= "Hello"; // initialize String
int len1,len2; // initialize two veriables
len1= strlen(name); // calculate the lenth of charecter name and store it in veriable len1
len2=strlen("Hello World"); // calculate the lenth of that charecter and store it in veriable len2
printf("\nstring= %s length =%d",name,len1); // Print the length
printf("\nstring= %s length =%d","Hello World",len2); // Print the length

}
Output:
string= Hello length =5
string= Hello World length =11
Function example (cont.)
 Example of strrev():
#include<stdio.h>
#include<string.h>

void main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is ::%s\n",arr);
}
Output: Hello
Reverse of entered string is ::olleH

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy