Introduction, The & and * operator, Declaration of pointer, Pointer to pointer, Pointer arithmetic, Pointer and array, Pointer with multidimensional array, Pointer and strings, Array of pointer with string, Dynamic memory allocation.
In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
A pointer in C is a variable that stores the address of another variable. It allows a program to indirectly access the memory location that a variable is stored in. Pointers are declared with a * before the variable name. The & operator returns the address of its operand and is used to initialize a pointer variable. The * operator dereferences a pointer to access the value at the address it contains.
Pointers in C allow variables to hold the memory addresses of other variables and data types. Pointers use the asterisk (*) and ampersand (&) operators - * accesses the value at a memory address, while & returns the memory address of a variable. Pointers are useful for passing arguments to functions, returning multiple values from functions, and accessing arrays through a single pointer variable. Pointer arithmetic increments or decrements a pointer by the size of its data type. Pointer-to-pointers allow pointers to hold the addresses of other pointer variables. Proper initialization and boundary checking is important to avoid crashes with pointers.
Pointer is a variable that stores the address of another variable. The '&' operator returns the address of a variable and '*' operator accesses the value of the variable being pointed to. Pointers must be declared with a data type that matches the variable it is pointing to (e.g. int* points to an int). Pointers can point to the address of other pointers, forming a chain. To access the value of a variable through a pointer, the pointer is dereferenced with the '*' operator.
The document provides an overview of pointers in C programming. It defines pointers as variables that contain the address of another variable in memory. The document outlines various pointer operations such as dereferencing with *, address of with &, arithmetic, comparisons, NULL pointers, function pointers, pointers to arrays, arrays of pointers, and pointers to pointers. It provides examples to illustrate how to declare, initialize, and manipulate pointers in C code.
Pointers in C store the address of another variable. They allow dynamic memory allocation at runtime and can refer to variables of any data type. Pointers help save memory space and improve performance. A pointer variable contains the address of another variable. Common pointer types include null pointers, void pointers, and wild pointers. Pointers are useful for accessing memory locations and forming complex data structures like linked lists. However, pointers also present risks like memory corruption if misused.
This document discusses pointers in C programming. It defines pointers as variables that store the address of another variable. It explains that pointers use the & operator to get the address of a variable and the * operator to dereference a pointer and access the value of the variable being pointed to. The document also discusses pointer arithmetic, where incrementing or decrementing a pointer moves it to the next or previous memory location. It provides an example using a float array to demonstrate pointer arithmetic and updating array elements through a pointer.
Pointers in C are variables that store memory addresses. They allow accessing and modifying the value stored at a specific memory location. Pointers contain the address of another variable as their value. To use pointers, they must first be declared along with the data type of the variable being pointed to. The address of the variable is then assigned to the pointer using the & operator. The value at the address can then be accessed using the * operator in front of the pointer variable name. The NULL pointer is a constant with a value of 0 that indicates an unassigned pointer. When a pointer is incremented, its value increases by the scale factor, which is the length of the data type being pointed to.
In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
A pointer in C is a variable that stores the address of another variable. It allows a program to indirectly access the memory location that a variable is stored in. Pointers are declared with a * before the variable name. The & operator returns the address of its operand and is used to initialize a pointer variable. The * operator dereferences a pointer to access the value at the address it contains.
Pointers in C allow variables to hold the memory addresses of other variables and data types. Pointers use the asterisk (*) and ampersand (&) operators - * accesses the value at a memory address, while & returns the memory address of a variable. Pointers are useful for passing arguments to functions, returning multiple values from functions, and accessing arrays through a single pointer variable. Pointer arithmetic increments or decrements a pointer by the size of its data type. Pointer-to-pointers allow pointers to hold the addresses of other pointer variables. Proper initialization and boundary checking is important to avoid crashes with pointers.
Pointer is a variable that stores the address of another variable. The '&' operator returns the address of a variable and '*' operator accesses the value of the variable being pointed to. Pointers must be declared with a data type that matches the variable it is pointing to (e.g. int* points to an int). Pointers can point to the address of other pointers, forming a chain. To access the value of a variable through a pointer, the pointer is dereferenced with the '*' operator.
The document provides an overview of pointers in C programming. It defines pointers as variables that contain the address of another variable in memory. The document outlines various pointer operations such as dereferencing with *, address of with &, arithmetic, comparisons, NULL pointers, function pointers, pointers to arrays, arrays of pointers, and pointers to pointers. It provides examples to illustrate how to declare, initialize, and manipulate pointers in C code.
Pointers in C store the address of another variable. They allow dynamic memory allocation at runtime and can refer to variables of any data type. Pointers help save memory space and improve performance. A pointer variable contains the address of another variable. Common pointer types include null pointers, void pointers, and wild pointers. Pointers are useful for accessing memory locations and forming complex data structures like linked lists. However, pointers also present risks like memory corruption if misused.
This document discusses pointers in C programming. It defines pointers as variables that store the address of another variable. It explains that pointers use the & operator to get the address of a variable and the * operator to dereference a pointer and access the value of the variable being pointed to. The document also discusses pointer arithmetic, where incrementing or decrementing a pointer moves it to the next or previous memory location. It provides an example using a float array to demonstrate pointer arithmetic and updating array elements through a pointer.
Pointers in C are variables that store memory addresses. They allow accessing and modifying the value stored at a specific memory location. Pointers contain the address of another variable as their value. To use pointers, they must first be declared along with the data type of the variable being pointed to. The address of the variable is then assigned to the pointer using the & operator. The value at the address can then be accessed using the * operator in front of the pointer variable name. The NULL pointer is a constant with a value of 0 that indicates an unassigned pointer. When a pointer is incremented, its value increases by the scale factor, which is the length of the data type being pointed to.
Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.
Pointers are among C’s most powerful, yet most difficult concepts to master. Some tasks like dynamic memory allocation done only by using pointers. So it is essential to learn pointers.
Pointers are a type of variable, just like int, double, etc., except instead of storing a value, they store a memory address of another variable.
This presentation focus on the basic concept of pointers. so that students can easily understand. it also contains basic operations performed by pointer variables and implementation using c language.
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
Pointers are a data type in C that contain memory addresses as their values. They allow programs to indirectly access and manipulate the data stored at those addresses. Pointers can be used to pass arguments by reference, return values from functions, access array elements, and link data structures like linked lists. Proper initialization of pointers is important to avoid issues like accessing protected memory or going out of array bounds.
The document discusses structures and unions in C programming. It defines structures as collections of related data types under a single name, and unions as structures that allocate memory for the largest data type so all fields share the same memory location. The key points covered include declaring and defining structures with tags, accessing structure members, nested structures, arrays of structures, structures as function parameters and return values, and the differences between structures and unions in memory allocation and variable storage.
The document discusses structures in C programming. It explains that a structure defines a template to group together different data types under a single name. It demonstrates how to define a structure, create structure variables, and access members of a structure using the dot and arrow operators.
The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and can point to data of various types, such as integers, characters, arrays, functions, and other pointers. It explains how to declare pointers, dereference pointers to access data, use pointers as function arguments, perform arithmetic on pointers, and use pointers to structures. Pointers allow accessing data indirectly through memory addresses, provide flexibility in passing arguments to functions, and are fundamental to working with arrays and structures in C.
The document discusses the different types of operators in C programming language including arithmetic, assignment, relational, logical, bitwise, conditional (ternary), and increment/decrement operators. It provides examples of how each operator is used in C code and what operation they perform on variables and values.
C Programming/Strings. A string in C is merely an array of characters. The length of a string is determined by a terminating null character: '-' . So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null character.
Pointer is a variable that stores the address of another variable. Pointers in C are used to allocate memory dynamically at runtime and can point to data of any type such as int, float, char, etc. Pointers are declared with a * before the variable name and are initialized using the address operator &. Pointers can be used to pass arguments to functions by reference and can also point to elements within structures.
1. A string is a one-dimensional array of characters terminated by a null character. Strings can be initialized during compilation or at runtime.
2. Common string functions like scanf(), gets(), getchar() are used to input strings while printf(), puts(), putchar() are used to output strings.
3. Library functions like strcpy(), strcat(), strcmp(), strlen() allow manipulation of strings like copying, concatenation, comparison and finding length.
A pointer is a variable that holds the memory address of another variable. Pointers allow access to the value of the variable located at the pointer's address. Pointers can be incremented or decremented to move through sequential memory addresses based on the data type size. Multiple levels of pointers can be declared, with each additional pointer level holding the address of the previous pointer variable.
The document discusses unions in C programming. A union is like a structure that allocates space for its members equal to the largest member. Only one member can be accessed at a time. The document provides an example union for employee data and explains memory allocation. Unions allow different variable types to share the same memory space. Structures are used for database management and other applications while unions are useful when variables need to share the same memory location.
This document discusses input and output streams in C++. It explains that streams are sequences of characters that move from a source to a destination, and covers input streams from devices to a computer and output streams from the computer to devices. It also details the standard input stream cin and standard output stream cout, and how to use various manipulators to format output, such as setprecision, fixed, showpoint, setw, setfill, left, and right.
Pointer arithmetic allows limited operations on pointers like incrementing, decrementing, addition and subtraction. When a pointer is incremented or decremented, its value changes by the size of the data type. Pointers store addresses, so adding two addresses is illegal as there is no meaning to the result. Subtracting pointers yields the offset between the two addresses. Operations like addition, subtraction on a pointer changes its value based on the data type size. Certain operations like addition of two addresses are illegal for pointers.
The document discusses pointers in C++. It defines pointers as variables that hold the memory addresses of other variables. It describes how to declare and initialize pointers, use pointer arithmetic and operators like & and *, pass pointers as function parameters, and dynamically allocate and free memory using pointers and operators like new and delete. Pointers allow programs to write efficiently, utilize memory properly, and dynamically allocate memory as needed.
- A structure is a user-defined data type that groups logically related data items of different data types into a single unit. Structures allow related data to be accessed and managed together.
- Structures can contain nested structures as members. Nested structure members are accessed using two period operators (e.g. e1.doj.day).
- Structures can be passed to functions as parameters and returned from functions. Pointers to structures are declared and accessed using arrow (->) operator instead of period operator.
- A union shares the same memory space for multiple data types, allocating only enough space for its largest member. Unions allow different types to share the same memory location.
Constants Variables Datatypes by Mrs. Sowmya JyothiSowmyaJyothi3
C provides various data types to store different types of data. The main data types are integer, float, double, and char. Variables are used to store and manipulate data in a program. Variables must be declared before use, specifying the data type. Constants are fixed values that don't change, and can be numeric, character, or string values. Symbolic constants can be defined to represent constant values used throughout a program. Input and output of data can be done using functions like scanf and printf.
The document discusses files and file operations in C/C++. It defines a file as a collection of bytes stored on a secondary storage device. There are different types of files like text files, data files, program files, and directory files. It describes opening, reading, writing, appending, and closing files using functions like fopen(), fread(), fwrite(), fclose(), etc. It also discusses random and sequential file access and modifying file contents using functions like fseek(), fread(), fwrite().
C Tokens, Escape sequence, Delimiters, Variables, Data types, Constants/ Literals, Expressions, Statements and Comments
https://github.com/ashim888/csit-c
Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.
Pointers are among C’s most powerful, yet most difficult concepts to master. Some tasks like dynamic memory allocation done only by using pointers. So it is essential to learn pointers.
Pointers are a type of variable, just like int, double, etc., except instead of storing a value, they store a memory address of another variable.
This presentation focus on the basic concept of pointers. so that students can easily understand. it also contains basic operations performed by pointer variables and implementation using c language.
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
Pointers are a data type in C that contain memory addresses as their values. They allow programs to indirectly access and manipulate the data stored at those addresses. Pointers can be used to pass arguments by reference, return values from functions, access array elements, and link data structures like linked lists. Proper initialization of pointers is important to avoid issues like accessing protected memory or going out of array bounds.
The document discusses structures and unions in C programming. It defines structures as collections of related data types under a single name, and unions as structures that allocate memory for the largest data type so all fields share the same memory location. The key points covered include declaring and defining structures with tags, accessing structure members, nested structures, arrays of structures, structures as function parameters and return values, and the differences between structures and unions in memory allocation and variable storage.
The document discusses structures in C programming. It explains that a structure defines a template to group together different data types under a single name. It demonstrates how to define a structure, create structure variables, and access members of a structure using the dot and arrow operators.
The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and can point to data of various types, such as integers, characters, arrays, functions, and other pointers. It explains how to declare pointers, dereference pointers to access data, use pointers as function arguments, perform arithmetic on pointers, and use pointers to structures. Pointers allow accessing data indirectly through memory addresses, provide flexibility in passing arguments to functions, and are fundamental to working with arrays and structures in C.
The document discusses the different types of operators in C programming language including arithmetic, assignment, relational, logical, bitwise, conditional (ternary), and increment/decrement operators. It provides examples of how each operator is used in C code and what operation they perform on variables and values.
C Programming/Strings. A string in C is merely an array of characters. The length of a string is determined by a terminating null character: '-' . So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null character.
Pointer is a variable that stores the address of another variable. Pointers in C are used to allocate memory dynamically at runtime and can point to data of any type such as int, float, char, etc. Pointers are declared with a * before the variable name and are initialized using the address operator &. Pointers can be used to pass arguments to functions by reference and can also point to elements within structures.
1. A string is a one-dimensional array of characters terminated by a null character. Strings can be initialized during compilation or at runtime.
2. Common string functions like scanf(), gets(), getchar() are used to input strings while printf(), puts(), putchar() are used to output strings.
3. Library functions like strcpy(), strcat(), strcmp(), strlen() allow manipulation of strings like copying, concatenation, comparison and finding length.
A pointer is a variable that holds the memory address of another variable. Pointers allow access to the value of the variable located at the pointer's address. Pointers can be incremented or decremented to move through sequential memory addresses based on the data type size. Multiple levels of pointers can be declared, with each additional pointer level holding the address of the previous pointer variable.
The document discusses unions in C programming. A union is like a structure that allocates space for its members equal to the largest member. Only one member can be accessed at a time. The document provides an example union for employee data and explains memory allocation. Unions allow different variable types to share the same memory space. Structures are used for database management and other applications while unions are useful when variables need to share the same memory location.
This document discusses input and output streams in C++. It explains that streams are sequences of characters that move from a source to a destination, and covers input streams from devices to a computer and output streams from the computer to devices. It also details the standard input stream cin and standard output stream cout, and how to use various manipulators to format output, such as setprecision, fixed, showpoint, setw, setfill, left, and right.
Pointer arithmetic allows limited operations on pointers like incrementing, decrementing, addition and subtraction. When a pointer is incremented or decremented, its value changes by the size of the data type. Pointers store addresses, so adding two addresses is illegal as there is no meaning to the result. Subtracting pointers yields the offset between the two addresses. Operations like addition, subtraction on a pointer changes its value based on the data type size. Certain operations like addition of two addresses are illegal for pointers.
The document discusses pointers in C++. It defines pointers as variables that hold the memory addresses of other variables. It describes how to declare and initialize pointers, use pointer arithmetic and operators like & and *, pass pointers as function parameters, and dynamically allocate and free memory using pointers and operators like new and delete. Pointers allow programs to write efficiently, utilize memory properly, and dynamically allocate memory as needed.
- A structure is a user-defined data type that groups logically related data items of different data types into a single unit. Structures allow related data to be accessed and managed together.
- Structures can contain nested structures as members. Nested structure members are accessed using two period operators (e.g. e1.doj.day).
- Structures can be passed to functions as parameters and returned from functions. Pointers to structures are declared and accessed using arrow (->) operator instead of period operator.
- A union shares the same memory space for multiple data types, allocating only enough space for its largest member. Unions allow different types to share the same memory location.
Constants Variables Datatypes by Mrs. Sowmya JyothiSowmyaJyothi3
C provides various data types to store different types of data. The main data types are integer, float, double, and char. Variables are used to store and manipulate data in a program. Variables must be declared before use, specifying the data type. Constants are fixed values that don't change, and can be numeric, character, or string values. Symbolic constants can be defined to represent constant values used throughout a program. Input and output of data can be done using functions like scanf and printf.
The document discusses files and file operations in C/C++. It defines a file as a collection of bytes stored on a secondary storage device. There are different types of files like text files, data files, program files, and directory files. It describes opening, reading, writing, appending, and closing files using functions like fopen(), fread(), fwrite(), fclose(), etc. It also discusses random and sequential file access and modifying file contents using functions like fseek(), fread(), fwrite().
C Tokens, Escape sequence, Delimiters, Variables, Data types, Constants/ Literals, Expressions, Statements and Comments
https://github.com/ashim888/csit-c
This document discusses file handling in C. It defines a file, outlines the basic steps to process a file which are to open, read/write, and close it. The basic file operations like fopen, fclose, fread, fwrite are introduced. It also covers file open modes, additional modes like r+, w+, a+, and functions to work with files like fprintf, fscanf, getc, putc. Examples are provided to demonstrate reading, writing, seeking within a file using functions like fread, fwrite, fseek, ftell.
- Functions allow programmers to split code into separate reusable segments that each perform a specific task. The document discusses different types of functions including user-defined and library functions.
- Key aspects of functions like function definitions, declarations, parameters, return values, and calling functions are explained. Different ways of passing data to functions like passing arrays and strings are also covered.
- The document provides examples to illustrate concepts like passing arguments by value versus reference and categorizing functions based on their use of arguments and return values.
For all who wish to learn c graphics programming, no knowledge of graphics concepts is required. C Graphics programming is very easy and interesting. You can use graphics programming for developing your own games, in making projects, for animation etc. It's not like traditional C programming in which you have to apply complex logic in your program and then you end up with a lot of errors and warnings in your program. In C graphics programming you have to use standard library functions ( need not worry if you don't know functions ) to get your task done. Just you pass arguments to the functions and it's done. On this website you will find almost all functions with detailed explanation and a sample program showing the usage of a function. To make things easy you are provided with executable files which you can download and execute. Firstly you should know the function initgraph which is used to initialize the graphics mode . To initialize graphics mode we use initgraph function in our program. initgraph function is present in "graphics.h" header file, so your every graphics program should include "graphics.h" header file. We will discuss initgraph withe help of following sample program:-
Unit 10 discusses files and file handling in C. It introduces the concept of data files, which allow data to be stored on disks and accessed whenever needed. There are two main types of data files: standard/high-level files and system/low-level files. Standard files are further divided into text and binary files.
To read from or write to files, a program must first open the file. This establishes a link between the program and operating system. Various library functions allow reading, writing, and processing file contents, such as fopen() to open a file, fread() and fwrite() for record input/output, and fseek() to move the file pointer to different positions for direct access of
Files allow data to be permanently stored and accessed by programs. Basic file operations include opening, reading, writing, and closing files. To open a file, its name and access mode are passed to the fopen function, which returns a file pointer used for subsequent read/write operations. Characters can be read from and written to files using functions like getc and putc. Command line arguments passed when a program launches are accessible through the argc and argv parameters of the main function.
Introduction of arrays, Declaration of array, Initialization of array, Sorting, Multidimensional array. Some code examples that will make you clear about the concept of arrays.
https://github.com/ashim888/csit-c
Branching, Looping, Conditional Statement, Exit function, Difference between break and exit. Some of the codes and some of the branching techniques are covered in this unit. Check conditional statement looping and exit functions too
https://github.com/ashim888/csit-c
Introduction, Array of structure, Passing structure to function, Passing array of structure to function, Structure within structure ( Nested Structure), Union, Pointer to structure
This document discusses input and output operations in C programming. It explains that input/output functions provide the link between the user and terminal. Standard input functions like scanf() are used to read data from keyboard while standard output functions like printf() display results on screen. Formatted functions like scanf() and printf() allow input/output to be formatted according to requirements. Unformatted functions like getchar() and putchar() deal with single characters. The standard library stdio.h provides predefined functions for input and output in C.
This document discusses data files in C programming. It defines a data file as a computer file that stores data for use by an application or system. It describes two types of data files: stream-oriented (text files and unformatted files) and system-oriented (low-level files). It explains how to open, read from, write to, and close data files using functions like fopen(), fclose(), getc(), putc(), fprintf(), and fscanf(). It provides examples of programs to create a data file by writing user input to a file and to read from an existing data file and display the contents.
This document discusses file management in C. It explains that files are used to store large amounts of data systematically so it can be accessed easily later. Files allow flexible storage and retrieval of data that is too large for memory. The key points covered include opening, reading, writing and closing files; using functions like fopen(), fclose(), fprintf(), fscanf(); handling errors; and dynamic memory allocation functions like malloc() and calloc().
The document discusses file handling in C programming. It explains that console I/O functions use keyboard and monitor for input and output but the data is lost when the program terminates. Files provide a permanent way to store and access data. The document then describes different file handling functions like fopen(), fclose(), fgetc(), fputc(), fprintf(), fscanf() for reading from and writing to files. It also discusses opening files in different modes, reading and writing characters and strings to files, and using formatted I/O functions for files.
Pointer is a variable that stores the address of another variable. The document discusses the syntax and declaration of pointers, initialization of pointers, pointer operators like * and &, void pointers, NULL pointers, pointers to pointers, and the relationship between arrays and pointers. Pointers allow accessing and modifying the value of the variable to which they point. The address of operator & returns the address of a variable, and the indirection operator * accesses the value of the variable at the address stored in the pointer.
Pointer variables allow programmers to indirectly access and manipulate the memory addresses where variables are stored. Pointers must be declared with a data type and initialized by assigning the address of an existing variable using the address-of operator (&). Pointer variables can then be used to read from and write to the memory location of the variable being pointed to using indirection (*). Pointers enable operations like traversing arrays, passing arguments by reference, and dynamically allocating memory. Key pointer concepts covered include declaration, initialization, dereferencing, arithmetic, comparisons, NULL pointers, and their usage with arrays.
Pointers in C programming store the address of other variables or memory locations. Pointers allow accessing and manipulating the data stored at those memory addresses. Pointers are useful for accessing arrays, dynamically allocating memory, and passing arguments by reference. Pointer variables must be declared with a data type and dereferenced using the * operator. Pointers can be initialized by assigning the address of another variable using the & operator. Pointer arithmetic allows incrementing or decrementing pointers to access successive memory locations.
- A pointer is a variable that stores the address of another variable. Pointers allow dynamic memory allocation and access to the value of the variable being pointed to using the indirection operator (*).
- Pointer variables are declared with a data type followed by an asterisk, such as int *ptr. They can be initialized by using the address-of operator (&) to store the address of another variable.
- Pointers can be used to access elements in an array by using pointer arithmetic. An array name itself is a constant pointer to the first element of the array.
The document discusses pointers in C programming. It begins by defining a pointer as a variable that holds the memory address of another variable. Pointers allow indirect access to variables using their addresses. Pointers are declared with a * before the variable name and initialized using the address operator &. The indirection operator * is used to dereference a pointer and access the value at the addressed memory location. Pointers can be used to pass arguments to functions by address, access arrays indirectly, and dynamically allocate memory on the heap. Operations on pointers include assignment, addition/subtraction with integers to offset the address, subtracting/comparing pointers, and incrementing/decrementing.
Pointer is a variable that stores the address of another variable. It can be declared by specifying the pointer type followed by an asterisk. Common pointer operations include assigning the address of a variable to a pointer, accessing the value at a pointer's address using unary operator *, and pointer arithmetic. Pointers allow accessing array elements by incrementing/decrementing the pointer. They can also be compared using relational operators and passed as arguments to functions.
Pointer variables store the memory addresses of other variables. They can be used to access and modify the values stored at those addresses. Pointers allow values to be passed by reference rather than by value, enabling changes made within functions to be reflected back in the calling function. Common pointer operations include dereferencing a pointer to access the value at an address, pointer arithmetic to increment or decrement a pointer to other memory locations, and passing pointers as function arguments to allow modification of variable values.
The document provides an introduction to pointers in C programming. Some key points:
1. Pointers store the address of another variable of the same data type. They allow accessing and modifying values using their memory location rather than their name.
2. Pointers are declared with a data type followed by an asterisk, like int *ptr. They must be initialized with the address of a variable using the & operator before being accessed.
3. The dereference operator * accesses the value at the address stored in the pointer. Pointer arithmetic and increment/decrement allow iterating through arrays using pointers.
4. Pointers enable passing by reference in functions and dynamic memory allocation. They are useful for handling arrays
A pointer is a variable that stores the memory address of another variable. A double pointer stores the address of a pointer variable. In the example code, a double pointer p2 stores the address of a single pointer p1, which stores the address of an integer variable v. The code then prints the value of v using the single and double pointers to demonstrate this. Pointers allow arithmetic operations like incrementing and decrementing to move the pointer to the next memory location without changing the actual variable value. A function pointer is a pointer that points to executable code in memory, allowing a function to be indirectly called through the pointer variable.
This document provides an overview of pointers in C programming. It defines pointers as variables that store memory addresses rather than values. Pointers have several useful applications like accessing variables outside functions, passing information between functions, and more efficiently handling data tables. The document explains how to declare pointer variables, assign the address of a variable to a pointer, dereference a pointer to access the value at an address, and pass pointers to functions. It also discusses pointers and arrays, structures, and arrays of structures.
- Pointers in C++ provide direct access to memory locations by storing the address of a variable. A pointer variable contains the address of another variable.
- The & (address of) operator returns the address of its operand. The * (value at) operator accesses the value at the address stored in a pointer variable.
- Memory can be dynamically allocated during runtime using pointers and the new operator. The delete operator frees up dynamically allocated memory.
This power point presentation explains briefly about the possible arithmetic operations in pointers. It also explains implicit and explicit typecasting of pointers
A pointer is a variable that stores the memory address of another variable. Pointers allow accessing and modifying the data stored at the referenced memory location. Pointers can be declared by specifying the data type followed by an asterisk, and are initialized by assigning the address of a variable to the pointer variable. Pointer variables can be used in expressions and arithmetic and can be passed to functions to modify the referenced data. Arrays can also be accessed and traversed using pointers by treating the array name as a pointer to its first element. Pointers to functions allow functions to be passed as arguments and enables polymorphism.
Pointers are variables that store memory addresses. A pointer must be declared with a data type and is initialized by using the address-of operator (&) to point to another variable. Pointer variables can be used to access and modify the value of the variable being pointed to using the indirection operator (*). Arrays can also be accessed using pointers by treating array names as pointers to the first element. Memory for dynamic data can be allocated at runtime using functions like malloc() and calloc() and pointers are used to access this memory.
The document discusses pointers in C++. It defines a pointer as a variable that stores the memory address of another variable of the same data type. Pointers allow a program to indirectly access the memory location of another variable. The key points covered include:
- Declaring pointer variables using a data type followed by an asterisk (*)
- Initializing pointers using the address-of (&) operator to store the address of another variable
- Accessing the value at a pointer's address using the dereference (*) operator
- Pointer arithmetic, where pointers can be incremented or decremented to access subsequent memory addresses
- Dynamic memory allocation using new/delete to allocate memory at runtime through a pointer.
This document discusses pointers in C programming. It defines pointers as memory variables that store memory addresses. It describes how pointers are declared using an asterisk and how they can be initialized to point to other variables by using the address-of operator. The document also discusses how pointers can be dereferenced using the indirection operator to access the value of the variable being pointed to. It provides examples of using pointers to pass values between functions and to access array elements.
String is an array of characters that can be manipulated using functions like strlen(), strcat(), strcmp(), and strcpy(). A NULL character (\0) is automatically appended to the end of a string.
Pointers in C store the address of a variable rather than the variable's value. Pointer variables must be declared with a data type followed by an asterisk, such as int *ptr. The ampersand (&) operator returns the address of its operand, and the asterisk (*) operator accesses the value stored at the address a pointer refers to. Pointers can dynamically allocate memory at runtime and are often used to pass arguments by reference.
1) Pointers store the address of a variable in memory. Pointer variables allow access and manipulation of other variables' memory addresses.
2) Pointer arithmetic can increment, decrement, add, and subtract from pointers to navigate through arrays and other data structures. Incrementing and decrementing a pointer moves it by the size of the data type, such as 4 bytes for a 64-bit integer.
3) Pointer arithmetic performs numeric operations on the memory addresses stored in pointers. Addition and subtraction adjust the pointer's address by the product of the data type size and the added/subtracted value.
Pointer is a variable that stores the address of another variable. Pointers allow indirect referencing of values and are useful for returning multiple values from functions, dynamic memory allocation, and building complex data structures like linked lists. Pointers are declared with a data type followed by an asterisk and are initialized with the address of another variable. The address-of operator & returns the address of its operand, while the indirection operator * accesses the value at the address stored in the pointer. Pointers can reference values indirectly, be passed by reference to functions to modify caller's variables, and implement call by reference parameter passing in C.
The document discusses sorting algorithms. It begins by defining sorting as arranging data in logical order based on a key. It then discusses internal and external sorting methods. For internal sorting, all data fits in memory, while external sorting handles data too large for memory. The document covers stability, efficiency, and time complexity of various sorting algorithms like bubble sort, selection sort, insertion sort, and merge sort. Merge sort uses a divide-and-conquer approach to sort arrays with a time complexity of O(n log n).
This document provides an overview of trees as a non-linear data structure. It begins by discussing how trees are used to represent hierarchical relationships and defines some key tree terminology like root, parent, child, leaf, and subtree. It then explains that a tree consists of nodes connected in a parent-child relationship, with one root node and nodes that may have any number of children. The document also covers tree traversal methods like preorder, inorder, and postorder traversal. It introduces binary trees and binary search trees, and discusses operations on BSTs like search, insert, and delete. Finally, it provides a brief overview of the Huffman algorithm for data compression.
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
https://github.com/ashim888/dataStructureAndAlgorithm
Stack
Concept and Definition
• Primitive Operations
• Stack as an ADT
• Implementing PUSH and POP operation
• Testing for overflow and underflow conditions
Recursion
• Concept and Definition
• Implementation of:
¬ Multiplication of Natural Numbers
¬ Factorial
¬ Fibonacci Sequences
The Tower of Hanoi
Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.
For further information
https://github.com/ashim888/dataStructureAndAlgorithm
References:
https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation
http://web.mit.edu/16.070/www/lecture/big_o.pdf
https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
https://justin.abrah.ms/computer-science/big-o-notation-explained.html
Concept and Definition of Data Structures
Introduction to Data Structures: Information and its meaning, Array in C++: The array as an ADT, Using one dimensional array, Two dimensional array, Multi dimensional array, Structure , Union, Classes in C++.
https://github.com/ashim888/dataStructureAndAlgorithm
This document provides an overview of problem solving using computers. It discusses the 7 stages of problem solving: 1) problem analysis, 2) algorithm development, 3) flowcharting, 4) coding, 5) compilation and execution, 6) debugging and testing, and 7) documentation. It also covers computer programs, programming languages, and the basic structure of a C program, which typically includes documentation, include libraries, definitions, global declarations, the main function, and subprograms.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPRAHUL
This Dissertation explores the particular circumstances of Mirzapur, a region located in the
core of India. Mirzapur, with its varied terrains and abundant biodiversity, offers an optimal
environment for investigating the changes in vegetation cover dynamics. Our study utilizes
advanced technologies such as GIS (Geographic Information Systems) and Remote sensing to
analyze the transformations that have taken place over the course of a decade.
The complex relationship between human activities and the environment has been the focus
of extensive research and worry. As the global community grapples with swift urbanization,
population expansion, and economic progress, the effects on natural ecosystems are becoming
more evident. A crucial element of this impact is the alteration of vegetation cover, which plays a
significant role in maintaining the ecological equilibrium of our planet.Land serves as the foundation for all human activities and provides the necessary materials for
these activities. As the most crucial natural resource, its utilization by humans results in different
'Land uses,' which are determined by both human activities and the physical characteristics of the
land.
The utilization of land is impacted by human needs and environmental factors. In countries
like India, rapid population growth and the emphasis on extensive resource exploitation can lead
to significant land degradation, adversely affecting the region's land cover.
Therefore, human intervention has significantly influenced land use patterns over many
centuries, evolving its structure over time and space. In the present era, these changes have
accelerated due to factors such as agriculture and urbanization. Information regarding land use and
cover is essential for various planning and management tasks related to the Earth's surface,
providing crucial environmental data for scientific, resource management, policy purposes, and
diverse human activities.
Accurate understanding of land use and cover is imperative for the development planning
of any area. Consequently, a wide range of professionals, including earth system scientists, land
and water managers, and urban planners, are interested in obtaining data on land use and cover
changes, conversion trends, and other related patterns. The spatial dimensions of land use and
cover support policymakers and scientists in making well-informed decisions, as alterations in
these patterns indicate shifts in economic and social conditions. Monitoring such changes with the
help of Advanced technologies like Remote Sensing and Geographic Information Systems is
crucial for coordinated efforts across different administrative levels. Advanced technologies like
Remote Sensing and Geographic Information Systems
9
Changes in vegetation cover refer to variations in the distribution, composition, and overall
structure of plant communities across different temporal and spatial scales. These changes can
occur natural.
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxEduSkills OECD
Iván Bornacelly, Policy Analyst at the OECD Centre for Skills, OECD, presents at the webinar 'Tackling job market gaps with a skills-first approach' on 12 June 2024
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
How to Make a Field Mandatory in Odoo 17Celine George
In Odoo, making a field required can be done through both Python code and XML views. When you set the required attribute to True in Python code, it makes the field required across all views where it's used. Conversely, when you set the required attribute in XML views, it makes the field required only in the context of that particular view.
हिंदी वर्णमाला पीपीटी, hindi alphabet PPT presentation, hindi varnamala PPT, Hindi Varnamala pdf, हिंदी स्वर, हिंदी व्यंजन, sikhiye hindi varnmala, dr. mulla adam ali, hindi language and literature, hindi alphabet with drawing, hindi alphabet pdf, hindi varnamala for childrens, hindi language, hindi varnamala practice for kids, https://www.drmullaadamali.com
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
বাংলাদেশের অর্থনৈতিক সমীক্ষা ২০২৪ [Bangladesh Economic Review 2024 Bangla.pdf] কম্পিউটার , ট্যাব ও স্মার্ট ফোন ভার্সন সহ সম্পূর্ণ বাংলা ই-বুক বা pdf বই " সুচিপত্র ...বুকমার্ক মেনু 🔖 ও হাইপার লিংক মেনু 📝👆 যুক্ত ..
আমাদের সবার জন্য খুব খুব গুরুত্বপূর্ণ একটি বই ..বিসিএস, ব্যাংক, ইউনিভার্সিটি ভর্তি ও যে কোন প্রতিযোগিতা মূলক পরীক্ষার জন্য এর খুব ইম্পরট্যান্ট একটি বিষয় ...তাছাড়া বাংলাদেশের সাম্প্রতিক যে কোন ডাটা বা তথ্য এই বইতে পাবেন ...
তাই একজন নাগরিক হিসাবে এই তথ্য গুলো আপনার জানা প্রয়োজন ...।
বিসিএস ও ব্যাংক এর লিখিত পরীক্ষা ...+এছাড়া মাধ্যমিক ও উচ্চমাধ্যমিকের স্টুডেন্টদের জন্য অনেক কাজে আসবে ...
2. • All computer have primary memory, also known as RAM( Random
Access Memory)
• RAM holds the programs that the computer is currently running along
with the data(i.e. variables) they are currently manipulating.
• All the variables used in a program reside in the memory when the
program is executed.
• RAM is divided into a number of small units or locations and each
location is represented by some unique number known as memory
address.
3/23/2016 Ashim Lamichhane 2
So before knowing what pointer is let us know about memories
3. So on…
• Each memory location is capable of storing small number, which is
known as byte.
• A char data is one byte is size and hence needs one memory location
of the memory.
• Similarly, integer data is two byte in size and hence needs two memory
locations of the memory.
3/23/2016 Ashim Lamichhane 3
4. Key concept
8 bits 1 byte ex. 1001 1111 is one byte
1024 bytes 1 kilo byte (KB)
1024 KB 1 Mega Bytes (MB)
1024 MB 1 Giga Bytes (GB)
3/23/2016 Ashim Lamichhane 4
• A computer having 1 GB RAM has 1024*1024*1024 i.e. 1073741824 bytes and these 1073741824 bytes are
represented by 1073741824 different address.
• For ex. The memory address 65524 represents a byte in memory and it can store data of one byte.
5. • Every variable in C program is assigned a space in memory.
• When a variable is declared, it tells computer the type of variable and
name of the variable.
• According to the type of variable declared, the required memory
locations are reserved.
• For ex. int requires two bytes, float requires four bytes and char
requires one byte.
3/23/2016 Ashim Lamichhane 5
7. A program to display memory location reserved by a variable
#include <stdio.h>
int main(){
int a=20;
printf("The address of a is: %un", &a);
printf("The value of a is: %dn", a);
return 0;
}
OUTPUT:
The address of a is: 65524
The value of a is: 20
3/23/2016 Ashim Lamichhane 7
8. A program to illustrate address reserved by different data types
#include <stdio.h>
int main()
{
/* code */
int a=20, b=50;
float c=50.4;
char d='A';
printf("The Base Address of a is: %un", &a);
printf("The Base Address of b is: %un", &b);
printf("The Base Address of c is: %un", &c);
printf("The Base Address of d is: %un", &d);
return 0;
}
3/23/2016 Ashim Lamichhane 8
9. Pointer
• A pointer is a variable that contains a memory address of variable.
• A pointer variable is declared to some type, like any other variable.
• Each pointer variable can point only to one specific type
• Pointer is declared in the same fashion like other variables but is
always preceded by ’*’ (asterisk operator.)
3/23/2016 Ashim Lamichhane 9
10. • integer variable declared as:
oint a;
oHere a is considered as integer variable.
• Similarly,
oint * a;
oNow variable a is a pointer variable, it now can store
address of integer variable.
oThe address of float variable can not be stored in it.
3/23/2016 Ashim Lamichhane 10
11. • Valid Example
int *p;
int num;
p=#
• Invalid Example
int *p;
float num;
p=#
3/23/2016 Ashim Lamichhane 11
12. Pointer Declaration
• Pointer variable can be declared as follows:
SYNTAX:
data_type * variable_name;
• Example:
int *x; //x integer pointer, holds address of any int variable
float *y; //y integer pointer, holds address of any float variable
char *z; //z integer pointer, holds address of any char variable
3/23/2016 Ashim Lamichhane 12
13. #include <stdio.h>
int main()
{
/* code */
int v=10, *p;
p=&v;
printf("address of v=%u n",&v );
printf("address of v=%u n",p );
printf("value of v=%d n",v );
printf("value of v=%d n",*p );
printf("address of p=%u n",&p );
}
3/23/2016 Ashim Lamichhane 13
Explanation
• v is an integer variable with 10.
• p is a pointer variable
• p=&v assigns address of v to p variable.
i.e ‘p’ is the pointer to variable ‘v’
• To access the address and value of v,
pointer p can be used.
• The value of p is nothing but address of
the variable v.
• *p can be used to display value stored
at a location pointed by pointer variable
p.
14. Indirection or Dereference Operator
• The operator *, used in front of a variable, is called pointer or indirection or
dereference operator.
• Normal variable provides direct access to their own values whereas a
pointer provides indirect access to the values of the variable whose address
it stores.
• When the pointer is declared, the star indicates that it is a pointer, not a
normal variable.
• The indirection operator indicates “the value at the memory
location stored in the pointer” or “the content of the location
pointed by pointer variable”
3/23/2016 Ashim Lamichhane 14
15. Address Operator
• The operator & is known as address operator.
• &a denotes the address of variable a.
• Ex.
int a=10, *p;
p=&a;
3/23/2016 Ashim Lamichhane 15
16. Initializing Pointer
• Address of some variable can be assigned to a pointer variable at the
time of declaration of the pointer variable.
• For ex:
int num; int num;
int *ptr=# int *p;
p=#
• These two statements above are equivalent to following statements.
3/23/2016 Ashim Lamichhane 16
17. So what actually is bad pointer?
• When a pointer is first declared, it doesn't have a valid address.
• Each pointer must be assigned a valid address before it can support
dereference operators. Before that, the pointer is bad and must not be
used.
• Every pointer contains garbage value before assignment of some valid
address.
• Correct code overwrites the garbage value with a correct reference to an
address and thereafter the pointer works fine.
• We have to program carefully.
3/23/2016 Ashim Lamichhane 17
18. Void Pointer
• Void pointer is a special type of pointer.
• It can point to any data type, from an integer value to a float to a string
of characters.
• Using void pointer, the pointed data can not be referenced directly (i.e.
* operator can not be used on them)
• Type casting or assignment must be used to change the void pointer to
a concrete data type to which we can refer.
3/23/2016 Ashim Lamichhane 18
19. Void Pointers in C : Definition
• Suppose we have to declare integer pointer, character pointer and float
pointer then we need to declare 3 pointer variables.
• Instead of declaring different types of pointer variable it is feasible to
declare single pointer variable which can act as integer pointer, character
pointer.
• Declaration of Void Pointer :
void * pointer_name;
3/23/2016 Ashim Lamichhane 19
20. Void Pointer Basics
• In C General Purpose Pointer is called as void Pointer.
• It does not have any data type associated with it
• It can store address of any type of variable
• A void pointer is a C convention for a raw address.
• The compiler has no idea what type of object a void Pointer really
points to
3/23/2016 Ashim Lamichhane 20
21. example
void *ptr; // ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum; // ptr has address of float data
3/23/2016 Ashim Lamichhane 21
Explanation
• Void pointer declaration is shown above.
• We have declared 3 variables of integer,
character and float type.
• When we assign address of integer to the
void pointer, pointer will become Integer
Pointer.
• When we assign address of Character Data
type to void pointer it will become Character
Pointer.
• Similarly we can assign address of any data
type to the void pointer.
• It is capable of storing address of any data
type
22. Summary : Void Pointer
Scenario Behavior
When We assign address of integer variable to void
pointer
Void Pointer Becomes Integer Pointer
When We assign address of character variable to void
pointer
Void Pointer Becomes Character Pointer
When We assign address of floating variable to void
pointer
Void Pointer Becomes Floating Pointer
3/23/2016 Ashim Lamichhane 22
23. example
#include <stdio.h>
int main(void){
int a=10;
double b=4.5;
void *vptr;
vptr=&a;
printf("a=%dn", *((int *)vptr)); /* not just simply *vptr */
vptr=&b;
printf("nb=%lfn", *((double *)vptr));
}
3/23/2016 Ashim Lamichhane 23
24. NULL POINTER
• A null pointer is a special pointer value that points nowhere or
nothing. i.e. no other valid pointer to any variable or array cell or
anything else will ever be equal to a null pointer.
• We can define a null pointer using predefined constant NULL which is
defined in header files such as stdio.h, stdlib.h,string.h
• Ex:
int *ptr=NULL;
3/23/2016 Ashim Lamichhane 24
25. POINTER TO POINTER (Double Pointer)
• C allows the use of pointers that point to other pointers and these in
turn, point to data.
• For pointers to do that, we only need to add asterisk (*) for each level
of reference. For example:
3/23/2016 Ashim Lamichhane 25
int a=20;
int *p;
int **q; pointer to “a pointer to an integer”
p=&a;
q=&p;
• To refer to variable ‘a’ using pointer ‘q’,
dereference it once i.e. *p
• To refer to variable ‘a’ using pointer ‘q’,
dereference it twice because there are two
levels of indirection involved.
• Both *p and **q displays 20 if they are
printed with a printf statement.
27. Double Pointer
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
/* take the address of ptr using address of
operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %dn", var );
printf("Value available at *ptr = %dn", *ptr );
printf("Value available at **pptr = %dn", **pptr);
return 0;
}
3/23/2016 Ashim Lamichhane 27
OUTPUT
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
28. Array Of Pointers
• An array of pointers can be declared as
data_type *pointer_name[size];
• For ex:
int *p[10];
• This declares an array of 10 pointers, each of which points to an integer. The first
pointer is called p[0], the second is p[1] and so on up to p[9].
• Initially, these pointers are uninitialized and they can be used as below.
int a=10, b=100, c=1000;
p[0]=&a;
p[1]=&b;
p[2]=&c; and so on.
3/23/2016 Ashim Lamichhane 28
29. Relationship between 1-D array and pointer
• Array name by itself is an address or pointer.
• It points to the address of the first element(0th element of an array)
• If x is 1D array, the address of the first element can be expressed as
&x[0] or as x.
• Similarly address of second array element can be written as &x[1]or
x+1.
• In general, address on an array element i can be expressed as &x[i]
or x+i
3/23/2016 Ashim Lamichhane 29
30. 3/23/2016 Ashim Lamichhane 30
• In general address of array element i can be expressed as &x[i] or x+i
• x[i] and *(x+i) both represents represents the content of the address.
Array x
Address of first
array element
can be expressed as
&x[0] or simply x
x[0] x[1] x[2] x[3]
Array x
Address of second
array element
can be expressed as
&x[1] or simply x+1
x[0] x[1] x[2] x[3]
31. #include <stdio.h>
int main(){
int x[5]={20,40,60,80,100},k;
printf("narray element ttelements value ttaddressn");
for(k=0;k<5;k++){
printf("x[%d]ttt%dttt%pn",k,*(x+k),x+k );
}
}
array element elements value address
x[0] 20 0x7fff5bb0bbb0
x[1] 40 0x7fff5bb0bbb4
x[2] 60 0x7fff5bb0bbb8
x[3] 80 0x7fff5bb0bbbc
x[4] 100 0x7fff5bb0bbc0
3/23/2016 Ashim Lamichhane 31
To display array element with their address using array name as a pointer
OUTPUT
• Element k acts as the element number( 0,1,2,3,4)
• x acting array is added with k i.e k is added with the address of first element, it points to the consecutive
memory location.
• Thus &x[k] is same as *(x+k)
32. /* WAP to calculate average marks of 10 students in a subject using pointer*/
#include <stdio.h>
int main(void){
float marks[10],sum=0;
int i;
float avg;
printf("Enter marks of 10 students: ");
for(i=0;i<10;i++){
scanf("%f",marks+i);
sum+=*(marks+i);
}
avg=sum/10;
printf("nThe average is=%fn", avg);
}
3/23/2016 Ashim Lamichhane 32
marks+0
marks+1
marks+2
marks+3
marks+4
33. Pointers and 2-D Arrays
• A two dimensional array is actually a collection of one dimensional arrays, each
indicating a row (i.e. 2-D array can be thought as one dimensional array of rows).
• It is stored in memory in the row form. For ex.
a=
3/23/2016 Ashim Lamichhane 33
1 2 3
4 5 6
7 8 9
• Is stored in the row major order in memory as illustrated below
1 2 3 4 5 6 7 8 9
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
65500 65502 65504 65506 65508 65510 65512 65514 65516
34. Syntax for declaration of 2-D array
• data_type (*ptr_var)[size2];
• Instead of data_type array[size1][size2];
• Ex: Suppose x is a two dimensional integer array having 4 rows and 5 columns. We declare
x as
int (*x)[5];
rather than
int x[4][5];
• x points to the first 5 element array, which is actually first row of the two dimensional
array.
• Similarly x+1 points to the second 5 element array, which is the second row of the two
dimensional array
3/23/2016 Ashim Lamichhane 34
35. 3/23/2016 Ashim Lamichhane 35
x
*x *x +1
1st 1-D array or first row
2nd 1-D array or first row
x +1
*(x+1) *(x +1)+1 *(x +1)+4
3rd 1-D array or first row
x +2
*(x+2) *(x +2)+1 *(x +2)+4
It can be concluded that
x pointer to 1st row
x+i pointer to ith row
*(x+i) Pointer to first
element in the ith
row
*(x+i)+j Pointer to jth
element in the ith
row
*(*(x+i)+j) Value stored in
the cell i,j
36. and
Thus in 2-D array,
&x[0][0] Is same as *x or *(x+0)+0
&x[0][1] Is same as *x +1 or *(x+0)+1
&x[2][0] Is same as *x +2 or *(x+2)+0
&x[2][4] Is same as *(x+2)+4
3/23/2016 Ashim Lamichhane 36
Thus in 2-D array,
&x[0][0] Is same as **x or *(*(x+0)+0)
&x[0][1] Is same as *(*x +1) or*( *(x+0)+1)
&x[2][0] Is same as *(*x +2) or *(*(x+2)+0)
&x[2][4] Is same as *(*(x+2)+4)
37. OUTPUT
/* Illustration of 2D array representation in Memory */
#include <stdio.h>
int main(void){
int p[2][3]={{1,2,3},{4,5,6}};
printf("p=%p p+1=%p",p,p+1);
printf("*p=%p *(p+1)=%p ",*p,*(p+1));
printf("*(p+0)+1=%p *(p+1)+1=%p",*(p+0)+1,*(p+1)+1);
printf("*(*(p+0)+1)=%d *(*(p+1)+1)=%d",*(*(p+0)+1),
*(*(p+1)+1));
}
p=0x7fff51c9dbd0
*p=0x7fff51c9dbd0
*(p+0)+1=0x7fff51c9dbd4
*(*(p+0)+1)=2
p+1=0x7fff51c9dbdc
*(p+1)=0x7fff51c9dbdc
*(p+1)+1=0x7fff51c9dbe0
*(*(p+1)+1)=5
3/23/2016 Ashim Lamichhane 37
39. Pointer Operations
• To illustrate the pointer operations, let us consider following
declaration of ordinary variables and pointer variables:
int a,b; float c; int *p1,*p2, float *f;
• A pointer variable can be assigned the address of an ordinary variable.
For ex: p1=&a; p2=&b; f=&c
• Content of one Pointer can be assigned to other pointer provided they
point to same data type. For ex.
p1=p2; /* valid */
f=p1; /* invalid */
3/23/2016 Ashim Lamichhane 39
40. • Integer data can be added to or subtracted from pointer variables. Eg.
p1+2 /* specifies an address which is two memory blocks
beyond the address pointed by p1 */
f+1 /* address which is one memory block beyond
address pointed by f */
3/23/2016 Ashim Lamichhane 40
p1 p1 +1 p1 +2 p1 +3 p1 +4
65516 65518 65520 65522 65524
f f+1 f+2 f+3 f+4
65506 65510 65514 65518 65522
If p1 points or stores address 65516, then p1+1
means address pointed by p1+ size in bytes of
data type of pointer.
i.e 65516+2 =65518. thus p1+1 represent address
65518 instead of 65517
Here f if float type. If f points or stores address
65506, then f+1 means address pointed by f+ size
in bytes of data type of pointer.
i.e 65506+1 =65510. thus f+1 represent address
65510 instead of 65507
41. • One pointer can be subtracted from other pointer provided they point
to elements of same array. For ex:
int main(void){
int a[]={45,89,54,29},*pf,*p1;
p1=a;
pf=a+2;
printf("So pf-p1=%ldn",pf-p1 );
}
OUTPUT: ???
3/23/2016 Ashim Lamichhane 41
42. • Here p1, points address of a[0] i.e. 65518 and pf points address to next
second block i.e. 65522.
• The difference between pf and p1 means number of memory blocks
for their type between addresses pointed by them i.e. 2 in this case
3/23/2016 Ashim Lamichhane 42
a[0] a[1] a[2] a[3]
65518 65520 65522 65524
45 89 54 29
43. • Two pointer variables can be compared provided both pointers point
to objects of the same data type:
if(p1>pf){
/* is a valid comparison */
}
• There is no sense in assigning an integer to a pointer variable.
p1=100; /* non sense */
p2=65516; /* invalid because cannot be directly assigned
like integer*/
3/23/2016 Ashim Lamichhane 43
44. • Two pointer variables cannot be multiplied and added together.
p1+p2; /* invalid */
• A pointer variable cannot be multiplied by a constant
p1*2; /* invalid */
• Null value can be assigned to pointer variable.
p1=NULL;
3/23/2016 Ashim Lamichhane 44
45. Passing Pointer to a Function
• A pointer can be passed to a function as an argument.
• Passing a pointer means passing address of a variable instead of value
of the variable.
• As address is passed in this case, this mechanism is also known as call
by address or call by reference.
• As address of variable is passed in this mechanism, if value in the
passed address is changed within function definition, the value of
actual variable is also changed.
3/23/2016 Ashim Lamichhane 45
46. #include <stdio.h>
void addGraceMarks(int *m){
*m=*m+10;
}
int main(void){
int marks;
printf("Enter Actual Marksn");
scanf("%d",&marks);
addGraceMarks(&marks);
printf("nThe grace marks is : %dn",marks);
}
3/23/2016 Ashim Lamichhane 46
47. #include <stdio.h>
void conversion(char *);
int main(){
char input;
printf("Enter character of your choice: ");
scanf("%c",&input);
conversion(&input);
printf("The corresponding character is: %cn",input );
}
void conversion(char *c){
if (*c>=97 && *c<=122)
{
*c=*c-32;
}
else if(*c >=65 && *c<=90){
*c=*c+32;
}
}
3/23/2016 Ashim Lamichhane 47
Output
Enter Character of Your Choice: a
The corresponding Char is: A
Enter Character of Your Choice: b
The corresponding Char is: B
48. String And Pointer
• As strings are arrays and arrays are closely connected with pointers, we can
say that string and pointers are closely related.
char name[5]=“shyam”;
• As the string variable name is an array of characters, it is a pointer to the
first character of the string and can be used to access and manipulate the
characters of the string.
• When a pointer to char is printed in the format of a string, it will start to
print the pointer character and then successive characters until the end of
string is reached.
• Thus name prints “shyam”, name+1 prints “hyam”, name+2 prints “yam”
and so on.
3/23/2016 Ashim Lamichhane 48
49. Dynamic memory allocation (DMA)
• The process of allocating and freeing memory at run time is known as
dynamic memory allocation.
• This reserves the memory required by the program and returns valuable
resources to the system once the use of reserved space is utilized.
• Though arrays can be used for data storage, they are of fixed size.
#define m 5
int main(void){
int x[m];
printf("%lun",sizeof(x));
}
3/23/2016 Ashim Lamichhane 49
50. • Consider an array size of 100 to store marks of 100 student.
• If the number of students is less than 100 say 10, only 10 memory
locations will be used and rest 90 locations are reserved but will not be
used i.e. wastage of memory will occur.
• In such situation DMA will be useful.
3/23/2016 Ashim Lamichhane 50
51. • Since an array name is actually a pointer to the first element within the
array, it is possible to define the array as a pointer variable rather than
as a conventional array.
• While defining conventional array, system reserves fixed block of
memory at the beginning of program execution which is inefficient but
this does not occur if the array is represented in terms of a pointer
variable.
• The use of pointer variable to represent an array requires some type of
initial memory assignment before the array elements are processed.
• This is known as Dynamic Memory Allocation (DMA)
3/23/2016 Ashim Lamichhane 51
52. • At execution time, a program can request more memory from a
free memory pool and frees if not required using DMA.
• Thus, DMA refers allocating and freeing memory at execution time or
run time.
• There are four library functions for memory management.
• malloc()
• calloc()
• free()
• realloc()
• These functions are defined within header file stdlib.h and alloc.h
3/23/2016 Ashim Lamichhane 52
53. 1. malloc()
oIt allocates requested size of bytes and returns a pointer to the first byte of the
allocated space.
ptr=(data_type*) malloc(size_of_block);
oHere, ptr is a pointer of type data_type. The malloc() returns a pointer to an
area of memory with size size_of_block.
x = (int *) malloc(100*sizeof(int));
oA memory space equivalent to 100 times the size of an integer is reserved and
the address of the first byte of the memory allocated is assigned to the pointer
x of type int. (i.e. x refers to the first address of allocated memory)
3/23/2016 Ashim Lamichhane 53
54. 2. calloc()
• The function provides access to the C memory heap, which is available
for dynamic allocation of variable-sized blocks of memory.
• Unlike malloc(), it accepts two arguments: no_of_blocks and
size_of_each_block specifies the size of each item.
• The function calloc allocates multiple blocks of storage, each of the
same size and then sets all bytes to zero.
• calloc initializes all bytes in the allocated block to zero.
3/23/2016 Ashim Lamichhane 54
55. ptr=(data_type*)calloc(no_of_blocks,size_of_each_block);
• For Ex:
x=(int*) calloc(5,10*sizeof(int));
Or
x=(int*) calloc(5,20);
• The above statement allocates contiguous space for 5 blocks, each of
size 20bytes. i.e. We can store 5 arrays, each of 10 elements of integer
types.
3/23/2016 Ashim Lamichhane 55
56. 3. free()
• Frees previously allocated space by calloc, malloc or realloc functions.
• The memory dynamically allocated is not returned to the system until
the programmer returns the memory explicitly. This can be done using
free() function.
• This function is used to release the space when not required.
• Syntax: free(ptr);
3/23/2016 Ashim Lamichhane 56
57. 4. realloc()
• This function is used to modify the size of previously allocated space.
• Sometimes the previously allocated memory is not sufficient; we need
additional space and sometime the allocated memory is much larger
than necessary.
• We can change memory size already allocated with the help of
function realloc().
3/23/2016 Ashim Lamichhane 57
58. • If the original allocation is done by the statement
ptr=malloc(size);
• Then, reallocation of space may be done by the statement
ptr=realloc(ptr,newsize);
• This function allocates a new memory space of size newsize to the
pointer variable ptr and returns a pointer to the first byte of the new
memory block and on failure the function return NULL
3/23/2016 Ashim Lamichhane 58
59. Applications of pointer
• Pointer is widely used in Dynamic Memory Allocation.
• Pointer can be used to pass information back and forth between a function
and its reference point.
• Pointers provide an alternative way to access individual array elements.
• Pointers increase the execution speed as they refer address.
• Pointers are used to create complex data structures such as linked list, trees,
graphs and so on.
3/23/2016 Ashim Lamichhane 59