Module -3
Module -3
Function Prototype
void main()
{
int result = add(5, 3);
// Function call
printf("Sum: %d\n", result);
return 0;
}
// Function definitionin
add(int a, int b)
{
return a + b;
}
String 1D array initialization
• Assigning a String Literal without Size
– char str[] = “jnnce”
• Assigning a String Literal with Size
– Char str[30]=“JNNCESHIMOGA”
• Assigning Character by Character with Size
– Char str[10] = {‘j’,’n’,’n’,’c’,’e’}
• Assigning Character by Character with out
Size
• Char str[] = {‘j’,’n’,’n’,’c’,’e’}
2D array initialization
#include <stdio.h>
int main() {
char arr[3][10] = {
“hjnnce",
“shimoga",
“nes"
};
return 0;
}
C string functions
strlen(const char *str)
: Returns the length of the string
Excluding the null character
strcpy
• strcpy(char *dest, const char *src)
• Copies the string src to dest
strcat
• strcat(char *dest, const char *src)
• Appends the string src to the end of dest
strcmp
• strcmp(const char *str1, const char *str2)
• Compares two strings
• Return 0 if equal
• Return >=1 if str1>str2
• Returns <=-1 if str2>str1
strchr
Strchr: Finds the first occurrence of character c in str
Syntax : strchr(const char *str, int c)
Example:
const char str[] = "Hello, jnnce";
char ch = ‘n';
char *ptr = strchr(str, ch);
}
strstr
strstr(const char *text, const char *patt)
Finds the first occurrence of the substring patt in text.
Example
char str[] = “welcome to jnnce";
char substr[] = “come’; / char substr[]=“sunitha”
char *result;
result = strstr(str, substr);
if (result != NULL)
{
printf("Substring found: '%s'\n", result);
}
else
{
printf("Substring not found.\n");
}
strtok()
Increment/Decrement of a
Pointer
Addition of integer to a
pointer
Subtraction of integer to a
pointer
Subtracting two pointers of
the same type
Comparison of pointers
Increment / decrement
Addition /subtraction of integer to a pointer
Pointer comparison
Pointers and arrays
• The memory address of the first element is
the same as the name of the array
Pointers and arrays
Array of pointers
• a pointer array is a homogeneous collection of
indexed pointer variables that are references
to a memory location.
• pointer_type *array_name [array_size];
• pointer_type: Type of data the pointer is
pointing to.
• array_name: Name of the array of pointers.
• array_size: Size of the array of pointers.
example
pointer
• A pointer is a variable that stores
the memory address of another
variable.
• 2 important operators used while
handling pointers are
– Dereferencing operator(*) : Used to
access the value
– Address operator(&) : used to access
the address of a variable
Pointer description
pointer
Advantages Dis-advantages
1. Pointers are used for dynamic 1. Uninitialized pointers
memory allocation and
might cause a
deallocation
segmentation fault.
2. Pointers are useful for
accessing memory 2. Memory corruption can
locations occur
3. Pointers are used to form 3. Pointers are a little bit
complex data structures such
complex to understand.
as linked lists, graphs, trees,
etc.
4. Pointers reduce the length of
the program and its execution
time as well
Declaring and initializing pointers
Example call by value
Call by Reference
Finding the sum and avg of array elements
using pointer
Programs
1. finding sum upto n
2. Factorial of a number
3. Checking number is odd or even
4. Finding the area ( 3.14*r*r) or circumference of a circle
(2 * 3.14 * r)
5. Adding two numbers
6. Checking given number is prime or not
7. Finding sum and average of array elements using
pointers
8. Swapping of two numbers using pointers