Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers...
Transcript of Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers...
![Page 1: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/1.jpg)
Data Variable and Pointer VariablePass by ReferencePointer Arithmetic
Passing Array Using PointersDynamic Allocation
![Page 2: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/2.jpg)
Address VS. ValueEach memory cell could store some VALUE.Each cell has an ADDRESS associated with it.
Note: Don’t confuse the address referring to a memory location with the value stored in that location.
![Page 3: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/3.jpg)
PointerJust another C variable whose value is an address of
another variable.
“Points to” that other variable.
![Page 4: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/4.jpg)
Declaring a pointerSyntax:
<data_type > *<pointer_name>;
Examples:int *ptr;char *cp;float *fp;
Note: <data_type > is the type of the data the pointer points to.
![Page 5: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/5.jpg)
Declaring a pointerAfter declaring a pointer:
int *ptr;
ptr doesn’t actually point to anything yet. (null pointer)We can either:
make it point to something that already existsallocate room in memory for something new that it will
point to
![Page 6: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/6.jpg)
Declaring a pointerDeclaring a pointer just allocates space to hold the
pointer; it does not allocate something to be pointed to.
If local variables in C are not initialized, they may contain anything.
![Page 7: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/7.jpg)
Operators Associated with PointersReference operator (&)
Also known as the address operator Read as “address of”
Dereference operator (*) Read as ”value pointed by”
![Page 8: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/8.jpg)
Reference Operator (&)The address that locates a variable within memory is
what we call a reference to that variable.
Example: int andy;
int *ted;
ted = &andy;
![Page 9: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/9.jpg)
Reference Operator (&)Example:
andy = 25;fred = andy;ted = &andy;
![Page 10: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/10.jpg)
Dereference Operator (*)Using a pointer we can directly access the value stored
in the variable which it points to.Example:
beth = *ted; /* beth is equal to value pointed by ted */
![Page 11: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/11.jpg)
Dereference Operator (*)Differentiate:
beth = ted; /* beth equal to ted ( 1776 ) */
beth = *ted; /* beth equal to value pointed by ted ( 25 ) */
Note:Reference and dereference operators have complementary
(or opposite) meanings. A variable referenced with & can be dereferenced with *.
![Page 12: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/12.jpg)
PointersExamples:
andy = 25; ted = &andy;
After these expressions:andy = 25 &andy = 1776 ted = 1776 *ted = 25
![Page 13: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/13.jpg)
Pointers
13
Data Variable
int x=5;
x=5&x
address data
Pointer Variable
int x=5;
int *p;
p=&x;
printf(“%p”, p);
printf(“%d”, *p);
the data inside address p
x=5&x=FF00
p=address
(FF00)
![Page 14: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/14.jpg)
14
Sample Program : Pointers
1 /* Filename: Pointers.c
Program Description: Pointer example
*/
Predict the output:
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
main()
{
int count, value;
int *count_address;
count = 100;
count_address=&count;
value= *count_address;
printf(“\n %p”, count_address);
printf(“\n %d”, value);
getch();
}
13
14
15
16
17
18
19
20
Pointers
![Page 15: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/15.jpg)
Review: Parameter PassingTwo Ways:
Pass by ValuePass by Reference
![Page 16: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/16.jpg)
Review: Pass by Valueint cubeByValue( int );
main(){ int n = 5;
printf(“Original value: %d”, n);n = cubeByValue( n );printf(“\nNew value: %d”, n);getch();
)
int cubeByValue( int n ){
return n * n * n;}
![Page 17: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/17.jpg)
Example 1: Pass by Referenceint cubeByReference( int * );
main(){ int n = 5;
printf(“Original value: %d”, n);n = cubeByReference( &n );printf(“\nNew value: %d”, n);getch();
)
int cubeByReference( int *nPtr ){
*nPtr = *nPtr * *nPtr * *nPtr;return *nPtr;
}
![Page 18: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/18.jpg)
Example 2: Pass by Referencevoid cubeByReference( int * );
main(){ int n = 5;
printf(“Original value: %d”, n);cubeByReference( &n );printf(“\nNew value: %d”, n);getch();
}
void cubeByReference( int *nPtr ){
*nPtr = *nPtr * *nPtr * *nPtr;}
![Page 19: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/19.jpg)
Exercise:Create a function that would swap two numbers. The
numbers must be pass as parameters of that function:
Using pass by value
Using pass by reference
![Page 20: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/20.jpg)
Pointer ArithmeticPointers only have limited set of operations:
A pointer could be incremented (++) and decremented (--)An integer may be added (+, +=) to a pointerAn integer may be subtracted (-, -=) from a pointerOne pointer may be subtracted from another
![Page 21: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/21.jpg)
Pointer Arithmeticint v[5];
vPtr can be initialized by: vPtr = v; vPtr = &v[0];
The diagram considers a machine (computer) with 4-byte integers.
3000
vPtr
59 8 7 6
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
![Page 22: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/22.jpg)
Pointer Arithmetic
Example: vPtr += 2; Result: 3000 + 2 * 4 = 3008
3000
vPtr
59 8 7 6
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
3008
vPtr
![Page 23: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/23.jpg)
Pointer ArithmeticFor arrays of other data types, vPtr would be incremented by
twice the number of bytes that it takes to store an object of that data type.
Example: char v[5];
vPtr += 2; Result: 3000 + 2 * 1 = 3002
3000
vPtr
‘5’‘9’ ‘8’ ‘7’ ‘6’
v[0] v[1] v[2] v[3] v[4]
3000 3001 3002 3003 3004
3002
vPtr
![Page 24: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/24.jpg)
Pointer ArithmeticSame case of a 2-byte integer machine.
Example: int v[5];
vPtr += 2; Result: 3000 + 2 * 2 = 3004
Most computers have 2-byte or 4-byte integers. Some newer ones uses 8-byte machines. Because of this, pointer arithmetic is machine-dependent.
3000
vPtr‘5’‘9’ ‘8’ ‘7’ ‘6’
v[0] v[1] v[2] v[3] v[4]
3000 3002 3004 3006 3008
3004
vPtr
![Page 25: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/25.jpg)
Pointer Arithmetic
If these are conducted in sequence:
vPtr is incremented to 3016: vPtr += 4;vPtr is set back to address 3000: vPtr -= 4;To increment vPtr: ++vPtr; or vPtr++;To decrement vPtr: --vPtr; or vPtr--;
3000
vPtr
59 8 7 6
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
![Page 26: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/26.jpg)
Pointer Arithmetic
What would be the value of x?
x = v2Ptr – vPtr;
Pointer arithmetic is meaningless unless performed on an array.
3000
vPtr
59 8 7 6
v[0] v[1] v[2] v[3] v[4]
3000 3004 3008 3012 3016
3008
v2Ptr
![Page 27: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/27.jpg)
Pointer ArithmeticPointers can be compared using equality and relational
operators, but is meaningless unless pointers point to members of the same array.
Pointer comparisons compare the addresses stored in the pointers.
![Page 28: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/28.jpg)
Generic PointersA pointer can be assigned to another pointer if both pointers
are of the same type.*a = *b;
Otherwise, a cast operator must be used to convert the pointer.
*a = * (int *) b;
![Page 29: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/29.jpg)
Generic PointersException:
pointer to void (i.e. void *)
Generic pointers can represent any pointer type.
A cast operation is not required but we can cast any data variable as a pointer to void.
A pointer to void cannot be dereferenced.
![Page 30: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/30.jpg)
Example: Generic Pointersmain(){ int i; char c; void *v; clrscr();
i = 6; c = 'a';
v = &i; printf(“v points to %d\n", *(int *) v);
v= &c; printf(“v now points to %c\n", *(char *) v);
getch();}
![Page 31: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/31.jpg)
Pointer ArithmeticVALID:
Add an integer to a pointer.
Subtract an integer from a pointer.
Subtract 2 pointers (from the same array).
Compare pointers (<, <=, ==, !=, >, >=).
Compare pointer to NULL (indicates that pointer points to nothing).
INVALID:
Adding 2 pointers.
Multiplying pointers.
Dividing pointers.
Subtracting a pointer from integer.
Add or subtract type float or double to or from pointers.
![Page 32: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/32.jpg)
Pointer ArithmeticPredict the output:
int x[5] = { 1, 2, 3, 4, 5 }; printf(“\n %d”, *p1 ); int *p1; x[2] = *p1 + p1[2]; p1 = x; printf(“\n %d”, x[2] ); p1++;
![Page 33: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/33.jpg)
Arrays and PointersArrays and pointers are closely related
An array name is a constant pointer
The name of the arrays points to the address/location of the first element of the array.
Constant pointer means that it’s value (address/reference) could not be changed.
![Page 34: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/34.jpg)
Using ARRAY INDEXING:
Arrays and Pointers
v59 8 7 6
v[0] v[1] v[2] v[3] v[4]
3000 3002 3004 3006 3008int v[5];
![Page 35: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/35.jpg)
Using POINTER MANIPULATION:
Pointers can also be subscripted (indexed) exactly like arrays can. This is referred to as pointer/subscript notation.
Arrays and Pointers
int v[5];int *p = v;
59 8 7 6
p[0] p[1] p[2] p[3] p[4]
3000 3002 3004 3006 3008
3000
p
![Page 36: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/36.jpg)
Using POINTER MANIPULATION:
Using pointer arithmetic, the array could be traversed.
Arrays and Pointers
int v[5];int *p = v;
p++;p += 3;*p--;
59 8 7 6
p[0] p[1] p[2] p[3] p[4]
3000 3002 3004 3006 30083000
p
![Page 37: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/37.jpg)
Passing Arrays using PointersSample Program : Passing Array Using Pointer
1 /* Filename: ArrayPtr.c
Program Description: Display the string character by character
*/
Predict the output:
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
void Display(char *p);
main()
{
char str[6] = “hello”;
Display( str );
getch();
}
void Display( char *p )
{
while( *p )
{
putchar( *p );
p++;
}
}
13
14
15
16
17
18
19
20
![Page 38: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/38.jpg)
Double PointersA pointer whose value is the address of another pointer.
Also called as a Pointers to pointers (i.e. pointers to int, pointers to char, etc.)
Syntax:int **ptr;char **a;
![Page 39: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/39.jpg)
Double PointersExample:
int **ipp;int i = 5, j = 6; k = 7; int *ip1 = &i, *ip2 = &j;
ipp = &ip1;
75 6
i j k
3000 … 4FF1 … … 581C
123D
ipp
3000
ip1
123D
4FF1
ip2
54EA
POINTER VALUES:
*ipp = 3000**ipp = 5
![Page 40: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/40.jpg)
Double PointersExample:
*ipp = ip2;
POINTER VALUES:
*ipp = 4FF1**ipp = 6
75 6
i j k
3000 … 4FF1 … … 581C
123D
ipp
4FF1
ip1
123D
4FF1
ip2
54EA
![Page 41: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/41.jpg)
Double PointersExample:
*ipp = &k;
POINTER VALUES:
*ipp = 581C**ipp = 7
75 6
i j k
3000 … 4FF1 … … 581C
123D
ipp
581C
ip1
123D
4FF1
ip2
54EA
![Page 42: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/42.jpg)
Dynamic Memory AllocationAllows to programmers to allocate (reserve) memory
dynamically.
Obtain more memory space at execution time.
Common Library functions associated:malloc()free()sizeof()
![Page 43: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/43.jpg)
Dynamic Memory AllocationSample Program : Dynamic Allocation
/* Filename: Dynamic.c
Program Description: Using malloc
*/
Output:
5
10
20
30
40
#include<stdio.h>
main()
{
int *p1,x;
int *p[4];
p1=(int *) malloc(sizeof(int));
*p1= 5;
printf(“\n%d”, *p1);
p[0] = (int *) malloc( sizeof( int ));
p[1] = (int *) malloc( sizeof ( int));
p[2] = (int *) malloc( sizeof( int ));
p[3] = (int *) malloc( sizeof( int ));
*p[0] = 10; *p[1] = 20;
*p[2] = 30; *p[3] = 40;
for(x=0; x<4; x++)
printf(“\n%d”, *p[0] );
for(x=0; x<4; x++)
free( p[x] );
getch();
}
![Page 44: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/44.jpg)
PointersEXERCISE (By pairs):
1. Make a function that accepts a pointer to a string and determine how many letter A’s are there in the string.
2. Make a function that accepts an array of n integers and returns the average of these integers. Implement using pointers.
![Page 45: Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.](https://reader033.fdocuments.in/reader033/viewer/2022061305/5514478a5503466d1a8b5b3c/html5/thumbnails/45.jpg)
NOTES
45
http://www.pcblab.net78.net/forum/(PCBLab Forum > ComE Subjects > ComE 211)