Embed presentation
Download to read offline



![Programming in C Unit-IV BCA Semester I4Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaPointer Arithmetic in CPointer arithmetic refers to performing operations on pointers to move across memory locations.It is mainly used when working with arrays, dynamic memory, and structures.Basic Pointer Arithmetic OperationsOperation Name OperationSymbolDescription Example Code Output /ResultPointerIncrementptr++ Moves the pointer tothe next elementint arr[3]={10,20,30};int *ptr=arr;ptr++;ptr → arr[1](value = 20)PointerDecrementptr-- Moves the pointer tothe previous elementint arr[3]={10,20,30};int *ptr=&arr[1];ptr--;ptr → arr[0](value = 10)Addition ofIntegerptr + n Moves pointerforward by nelementsint *ptr=arr;ptr = ptr + 2;ptr → arr[2](value = 30)Subtraction ofIntegerptr - n Moves pointerbackward by nelementsint *ptr=&arr[2];ptr = ptr - 1;ptr → arr[1](value = 20)PointerSubtractionptr1 - ptr2 Gives number ofelements between twopointers (same arrayonly)int *p1=&arr[2];int *p2=&arr[0];int diff = p1 - p2;diff = 2Pointer Addition(Invalid)ptr1 + ptr2 Adding two pointers isnot allowed— CompilationErrorPointerMultiplication(Invalid)ptr1 * ptr2 Multiplying pointers isnot allowed— CompilationErrorNotes:1. Pointer arithmetic works based on data type size, not bytes.Example:int *p; // int = 4 bytesp++; // moves 4 bytes ahead](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-4-2048.jpg&f=jpg&w=240)
![Programming in C Unit-IV BCA Semester I5Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca2. Adding an integer to a pointer is allowed; adding/multiplying two pointers is notallowed.Example:p = p + 2; // validp = p + p; // invalidp = p * p; // invalid3. Subtracting two pointers gives distance in elements.Example:int arr[5];int *p1 = &arr[3];int *p2 = &arr[1];int d = p1 - p2; // d = 24. Pointer multiplication is not allowed; multiply the integer before adding.Example:p = p + (3); // move 3 positions](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-5-2048.jpg&f=jpg&w=240)






![Programming in C Unit-IV BCA Semester I12Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaCategories of User-Defined FunctionsUser-defined functions in C can be classified into 4 types based on parameters and return type:1. Function with Arguments and Return Value(Takes parameters and returns a value) [int result = add (5, 10);]2.Function with No Arguments but Return Value(Takes parameters but does not return a value) [int result = add ();]3. Function with Arguments but No Return Value (Void Function)(Does not take parameters but returns a value) [ add (5, 10);]4. Function with No Arguments and No Return Value (Void Function)(Does not take parameters and does not return a value) [ add ();]1. Function with Arguments and Return Value• This type of user-defined function accepts arguments (inputs) from the calling functionand returns a value (output) back to the caller.• It is the most commonly used function type in C because it allows data to be passed intothe function and the result to be reused in different parts of the program.Example Program](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-12-2048.jpg&f=jpg&w=240)









Unit-4Pointers in C - Definition, Declaring and initialising pointers, accessing address and valueof variables using pointers,Pointer Arithmetic, Advantages and disadvantages of usingpointers.User Defined Functions - Need , structure of C user defined functions.Categories of userdefined functions - With and without parameters and return type. function call - call byvalue, call by reference.



![Programming in C Unit-IV BCA Semester I4Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaPointer Arithmetic in CPointer arithmetic refers to performing operations on pointers to move across memory locations.It is mainly used when working with arrays, dynamic memory, and structures.Basic Pointer Arithmetic OperationsOperation Name OperationSymbolDescription Example Code Output /ResultPointerIncrementptr++ Moves the pointer tothe next elementint arr[3]={10,20,30};int *ptr=arr;ptr++;ptr → arr[1](value = 20)PointerDecrementptr-- Moves the pointer tothe previous elementint arr[3]={10,20,30};int *ptr=&arr[1];ptr--;ptr → arr[0](value = 10)Addition ofIntegerptr + n Moves pointerforward by nelementsint *ptr=arr;ptr = ptr + 2;ptr → arr[2](value = 30)Subtraction ofIntegerptr - n Moves pointerbackward by nelementsint *ptr=&arr[2];ptr = ptr - 1;ptr → arr[1](value = 20)PointerSubtractionptr1 - ptr2 Gives number ofelements between twopointers (same arrayonly)int *p1=&arr[2];int *p2=&arr[0];int diff = p1 - p2;diff = 2Pointer Addition(Invalid)ptr1 + ptr2 Adding two pointers isnot allowed— CompilationErrorPointerMultiplication(Invalid)ptr1 * ptr2 Multiplying pointers isnot allowed— CompilationErrorNotes:1. Pointer arithmetic works based on data type size, not bytes.Example:int *p; // int = 4 bytesp++; // moves 4 bytes ahead](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-4-2048.jpg&f=jpg&w=240)
![Programming in C Unit-IV BCA Semester I5Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca2. Adding an integer to a pointer is allowed; adding/multiplying two pointers is notallowed.Example:p = p + 2; // validp = p + p; // invalidp = p * p; // invalid3. Subtracting two pointers gives distance in elements.Example:int arr[5];int *p1 = &arr[3];int *p2 = &arr[1];int d = p1 - p2; // d = 24. Pointer multiplication is not allowed; multiply the integer before adding.Example:p = p + (3); // move 3 positions](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-5-2048.jpg&f=jpg&w=240)






![Programming in C Unit-IV BCA Semester I12Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaCategories of User-Defined FunctionsUser-defined functions in C can be classified into 4 types based on parameters and return type:1. Function with Arguments and Return Value(Takes parameters and returns a value) [int result = add (5, 10);]2.Function with No Arguments but Return Value(Takes parameters but does not return a value) [int result = add ();]3. Function with Arguments but No Return Value (Void Function)(Does not take parameters but returns a value) [ add (5, 10);]4. Function with No Arguments and No Return Value (Void Function)(Does not take parameters and does not return a value) [ add ();]1. Function with Arguments and Return Value• This type of user-defined function accepts arguments (inputs) from the calling functionand returns a value (output) back to the caller.• It is the most commonly used function type in C because it allows data to be passed intothe function and the result to be reused in different parts of the program.Example Program](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-ivpointersanduserdefinedfunctions-251203072356-cdec4368%2f75%2fProgramming-in-C-UNIT-IV-Pointers-and-User-Defined-Functions-12-2048.jpg&f=jpg&w=240)







