Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

basic notes on Pointers, Study notes of Computer Engineering and Programming

exam study notes on Pointers ! covers all the important topics ! smart option to study !

Typology: Study notes

2016/2017

Uploaded on 01/14/2017

vaishnavi_saxena
vaishnavi_saxena 🇮🇳

2 documents

1 / 7

Related documents


Partial preview of the text

Download basic notes on Pointers and more Study notes Computer Engineering and Programming in PDF only on Docsity! POINTERS A pointers is a derived data type in C . pointers contain memory address as their values since these memory address are the locations in the computer memory where program instructions and data are stored pointers can be used to access and manipulate the data stored in memory. Pointers are used frequently in c as they offer a number of benefits to the programmers .they include: a. Pointers are more efficient in handling array and data tables b. Pointers can be used to return the multiple values from a function via function arguments c. Use of pointers arrays to character strings result in saving of the data storage space in the memory. d. Pointers allow c in support dynamic memory management e. Pointers reduces length and complexity of programs ADVANTAGES:- a. To point to different data structures b. Manipulation of data at different memory locations is easier c. To achieve clarity and simplicity d. More compact and efficient coding e. To return multiple value via functions f. Dynamic memory allocations OPERATORS USED WITH POINTERS There are two basic operators used with pointers 1. The address operator : & (ampersand) 2. The indirection operator : * (asterisk) The address operator gives the address of a variable while the indirection operator gives the value of the variable that the pointer is pointing to. THE ADDRESS OPERATOR (&):- When a variable “a” is declared in a program a strong location in main memory is made available by the compiler as shown below int a;=a Thus “a” is the name associated by the compiler in the compiler in the location in the memory of the computer . Explanation of Example With reference to above program – We have following associated points – Point Variable 'a' Variable 'ptr' Variable 'pptr' Name of Variable A ptr Pptr Type of Value that it holds Integer Address of 'a' Address of 'ptr' Value Stored 3 2001 4001 Address of Variable 2001 4001 6001 & Operator – “Address of” Operator As you can see that in above example, I have used & operator to find out the address of variable name. & operator is also known as “Address of” Operator. printf("Address of var is: %u", &num); Point to note: %u is used for formatting variable’s address. You can also use %p in place of %u. Now you know how to get the address of a variable but do you know how can you store that address in some other variable? That’s where pointer takes place. } Can you guess the output of following statements – #include <stdio.h> #include<conio.h> void main() { int var =10; int *p; p= &var; printf ( "\n Address of var is: %u", &var); printf ( "\n Address of var is: %u", p); printf ( "\n Address of pointer p is: %u", &p); /* Note I have used %u for p's value as it should be an address*/ printf( "\n Value of pointer p is: %u", p); printf ( "\n Value of var is: %d", var); printf ( "\n Value of var is: %d", *p); printf ( "\n Value of var is: %d", *( &var)); getch(); } Output: Address of var is: 00XBBA77 Address of var is: 00XBBA77 Address of pointer p is: 77221111 Value of pointer p is: 00XBBA77 Value of var is: 10 Value of var is: 10 Value of var is: 10 POINTERS AND FUNCTIONS:- Pointers are mainly used allow with functions for passing the address of the arguments . generally an argument can be passed onto the functions in two ways: 1. Call by Value 2. Call by Reference CALL BY VALUE Call by value refers to sending value or arguments onto the functions .in this method the value of actual arguments is copied onto the corresponding formal arguments whenever a function is called. The various oprations performed on their value of actual arguments on their main functions. EXAMPLE : Write a program to swap two integers number using call by value method through the keyboard .find out the number before during and after the function calling (Swap). SOLUTION : #include<stdio.h.> #include<conio.h > void main() { clrscr(); int x,y; void input (int x,int y) x=30; y=40; printf(“\n value of x before function call=%d”,x); printf(“\n value of ybefore function call=%d”,y); swap(x,y); printf(“\n value of x after function call=%d”,x); printf(“\n value of y after function call=%d”,y); getch(); } void swap (int a,int b) { int temp; temp=a; a=b; b=temp; printf (“\n value of x during function=%d”,a); printf(“\n value of y during function=%d”,b); } OUTPUT value of x before function call 30 value of y before function call 40 value of x during function 40 value of y during function 30 value of x after function call 30 value of y after function call 40 CALL BY REFRENCE Call by reference is the method used to access any function through the main program using pointers . Using this method is contrast to call by value method the operations performed on the formal arguments directly effect the value of the actual arguments so any change on that address effect the value .the following example illustrate the call by reference method . EXAMPLE : Write a program to swap two integers numbers using call by reference method find out the number before during and after the function calling Solution : #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b; void swap(int*x,int*y); a=30; b=40; printf(“\n value of a before function=%d”,a); printf(“\nvalue of before function=%d”,b); swap(&a,&b); printf(“\n value of a after function =%d”,a); printf(“\n value of a after function=%d”,b); getch(); } void swap(int*x,int*y) { int temp; temp=*x; *x=*y; *y=temp; printf(“n\value of a during function=%d”,*x); printf(“\n value of b during function=%d”,*y); } Output value of a before function call=30 value of b before function call=40 value of a during function =40 value of b during function =30 value of a after function call=30 value of b after function call=40
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved