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

unit 3 (1) (1)

The document provides an overview of arrays in C, including their declaration, initialization, and differences between arrays and pointers. It explains multidimensional arrays, string manipulation functions, and the conversion of strings to numerical values using atoi, atol, and atof. Additionally, it discusses the memory model of arrays and how indexing works.

Uploaded by

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

unit 3 (1) (1)

The document provides an overview of arrays in C, including their declaration, initialization, and differences between arrays and pointers. It explains multidimensional arrays, string manipulation functions, and the conversion of strings to numerical values using atoi, atol, and atof. Additionally, it discusses the memory model of arrays and how indexing works.

Uploaded by

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

Unit 3

1.Describe how arrays are declared and initialized


in C with examples.
Array is a collection of elements stored in
contiguous(side by side) memory locations
We can collect elements of similar datatyp
in an array.
Each element is separated by comma ( , )

Syntax for array declaration:


data_type array_name[size] ;//declarati
Example:
//declaration and initialization of different
types
int primes [ 4 ] = {2, 3, 5, 7 };
char college [ 3 ] = {‘Q’, ‘I’, ‘S’};
float time[3] = {9.00, 10.50, 1.30};
double roots[3] = { 2.546 , 3.450,
6.000};

2. Explain the concept of multidimensional array.


How do you declare and initialize a 2D array and
3D array.
A multidimensional array is an array that
has more than one dimension.
We can collect elements in different
directions,
i.e., row-wise, column-wise, height-wise…
Syntax for a 2d array declaration:
data_type array_name[rows][columns];

Syntax for a 3d array declaration:


data_type array_name[rows][columns]
[height];
Example:
// declaration and initialization
int over_score[6]={4, 6, 2, 1, 0, 6} //1D
int matrix[2][2] = { 1 , 2 , 3, 4}; //2D
array

int cube[2][2][2]={1,2,3,4,5,6,7,8}; //3

3.What is the difference between a pointer and


an array in c. Explain with examples.
Array is a contiguous collection of element
of same data-type.
In array,the array name points to the first
element. i.e,it stores address of first eleme
Example of marks array:
float marks[3]= { 89.0 , 78.5, 98.0};
Pointer is a variable that stores address of
another variable.
We can get address of an element using (&)
address of operator.
Example :
int num = 7 ; //integer variable num
int *ptr = # //pointer variable ptr
//storing address of
num

4.Write a program to find transpose of a


2D
array in c.
#include<stdio.h>
int main( ){
int mat_A[2][2]={1,2,3,4};
int trans_A[2][2];
//getting the transpose of a matrix
for(int i = 0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
trans_A[j][i]=mat_A[i][j];
}
}

// printing the transpose


for(int i = 0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
printf(“ %d\t”, trans_A[ i][ j
}
printf(“\n”);
}
return 0;
}

5.What is a string ? Explain the working of the strlen( )


strcpy( ) , strncpy( ) functions.

A string is a sequence of characters


terminated
by a null character (\0).
Strings are used to store ,manipulate text
data.
Example:
char name[7]= “Johnny”;
strlen( )
To find length of string.
char name [100]= “ sri ” ;
printf(“length is %d”,strlen(name));
Output:
length is 3
strcpy( )
Copies one string to another
char name[100]= “Titanic”;
char movie[100];
strcpy(movie,name);//copies name to mov

strncpy( )
The strncpy( ) is similar to the strcpy( )
It copies the given n numbers of character
from
another string
Example:
char symbols[100]= “abcdef@#&+-!” ;
char alpha[100];
strncpy(alpha , symbols, 6 ) ;

6. Discuss the concept of string manipulation in


C and demonstrate how you can manipulate
strings using strchr(),strrchr(), and strstr()

strchr( )
The strchr() function is used to find the fir
occurrence of a specified character in a
string.
It returns pointer to the first occurrence of
the specified character.
It returns NULL if the character is not
found.
Example:

char *ptr = strchr(name , ‘G’);


The address of character ‘G’ (x6) will be
stored
in ptr .

strrchr( )
The strchr() function is used to find the las
occurrence of a specified character in a
string.
It returns pointer to the last occurrence of
the specified character.
 It returns NULL if the character is not
found.
Example:

char *ptr = strrchr(name , ‘E’);


The address of character ‘E’ (x7) will be
stored
in ptr .

strstr( )
strstr ( ) searches for the first occurrence o
a
specified substring within a string.
It returns pointer to the first occurrence of
the specified substring.
 It returns NULL if the substring is not
found.

Example:
char *ptr =strstr(“coldplay”, “play”);
The address of character ‘p’ (xf5) will be
stored
in ptr

7. Explain the difference between atoi,atol,atof.


Provide examples of usage.
These functions are used to convert string
to
numerical values.
atoi(ASCII to Integer)
atoi converts a string to an integer value
Example:
atoi(“123”);
Output: 123

atol(ASCII to Long)
atol converts a string to an long integer value
Example:
atol(“9876543210”);
Output: 9876543210

atof(ASCII to Float)
atof converts a string to an floating value
Example:
atol(“3.1415”);
Output: 3.1415
8. Explain the memory model of arrays in C.
How are arrays stored in memory ,how does
indexing work.
Arrays in memory are stored as contiguou
blocks of memory. The Elements are stor
one after the other in sequence.

Example :
// 1D (linear array)
char twinkle[ 5]={‘S’, ‘t’, ‘a’, ‘r’, ‘s’};

//2 D array
char mat_A[2][2]= {1 , 2 , 3 , 4};
Indexing in Arrays:
Indexing refers to process of accessing eleme
using its position/index.
Array index starts from zero.

You might also like