Pointers
This presentation is the property of its rightful owner.
Sponsored Links
1 / 10

Pointers PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on
  • Presentation posted in: General

Pointers. Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address of a variable that contains a specific value. A variable name directly references a value.

Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Pointers

Pointers

  • Pointers are variables that contain memory addresses as their values.

  • A variable directly contains a specific value.

  • A pointer contains an address of a variable that contains a specific value.

  • A variable name directly references a value.

  • A pointer indirectly references a value.

  • Referencing a value through a pointer is called indirection.


Directly and indirectly referencing a variable

Directly and Indirectly referencing a variable

sum directly

references a

variable whose

value is 5

5

sumPtr

indirectly

references a

variable whose

value is 5

sum

sumPtr sum

5


Initialization

Initialization

  • A pointer may be initialized to 0 , NULL or an address.

  • NULL is a symbolic constant defined in <stdio.h>

  • A pointer with the value NULL points to nothing.

  • The only integer that can be assigned to a pointer is 0.

    Dereferencing

  • The * operator – indirection or dereferencing operator, returns the value of the object that its operand points to in memory. This is called dereferencing the pointer.


Program to display the address and value of a pointer

/* Program to display the address and value of a pointer */

#include <stdio.h>

int main()

{

int a;

int *aPtr;

a = 200;

aPtr = &a;

printf(“The address of a is %p \n”, &a);

printf(“The value of aPtr is %p \n”, aPtr);

printf(“The value of a is %d \n”, a);

printf(“The value of *aPtr is %d \n”, *aPtr);

}


Calling functions by reference

Calling functions by reference

#include <stdio.h>

void cubebyref( int *);

int main()

{

int num = 5;

printf(“The number is %d \n”,num);

cubeByRef(&num);

printf(“The new value of number is %d \n”,num);

return 0;

}

void cubeByRef(int *numPtr)

{

*numPtr = *nPtr * *nPtr * *nPtr ;

}


Relationship between pointers and arrays

Relationship between Pointers and Arrays

  • An array name is the address of the first element of an array.

  • int b[5];

  • The address of the first element :

    using %p to print - b, &b[0], &b.

  • Assume that int b[5] and pointer variable bPtr have been declared. Since the array name is a pointer to the first element of the array:

    bPtr = b;

  • This is equivalent to bPtr = &b[0];

  • Array element b[1] can be referenced by:

    *( bPtr + 1)

  • 1 is the offset to the pointer.

  • The preceding notation is called pointer / offset notation.


Pointers

  • In pointer/offset notation, the offset is the same as the array subscript.

  • Pointers can be subscripted exactly as arrays can.

  • bPtr[1] refers to the array element b[1].

  • This is called pointer / subscript notation.


Pointers

#include <stdio.h>

int main()

{

int b[] = { 10 , 20 , 30 , 40 };

int *bPtr = b;

int i, offset;

printf(“Pointer/offset notation where the pointer is ”);

printf(“the array name \n”);

for(offset = 0; offset < 4; offset++)

printf(“*(b + %d) = %d \n”, offset, *( b + offset) );

printf(“Pointer/subscript notation \n”);

for( i = 0 ; i < 4 ; i++)

printf(“bPtr[ %d ] = %d \n”, i, bPtr [i ]);

printf(“pointer/offset notation \n”);

for (offset = 0; offset < 4 ; offset ++)

printf(“ *(bPtr + %d) = %d \n”, offset,*(bPtr + offset));

return 0;

}


Pointers

#include <stdio.h>

void copy( char *, const char *);

int main()

{

char string1[10], *string2 = “CS230”;

char string3[10], string4[ ] = “Lab”;

copy(string1, string2);

printf(“string1 = %s \n”,string1);

copy(string3,string4);

printf(“string3 = %s \n”,string3);

return 0;

}


Void copy char s1 const char s2 int i 0 do s1 i s2 i i while s2 i 0 s1 i 0

void copy(char *s1, const char *s2)

{

int i = 0;

do

{

s1[i] = s2[i];

i++;

}

while (s2[i] != ‘\0’);

s1[i] = ‘\0’;

}


  • Login