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

Pointers: Understanding Pointer Initialization, Operators, and Function Calls, Slides of Computer Fundamentals

An overview of pointers in c programming language, including pointer initialization, operators (* and &), and passing arguments to functions by value, reference, and address. It includes examples and program outputs.

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Pointer Initialization
Initialize pointers to
0, NULL, or an address
0 or NULL points to nothing
(NULL preferred)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Pointers: Understanding Pointer Initialization, Operators, and Function Calls and more Slides Computer Fundamentals in PDF only on Docsity!

Pointer Initialization

  • Initialize pointers to

0 , NULL, or an address

 0 or NULL – points to nothing

(NULL preferred)

Initializing Pointers

int *ptr = &var ;

int *ptr = 0 ;

int *ptr = NULL ;

Pointer operators

 * and & are inverses
  • Complements of each other
  • They cancel each other out
OutlineOutline

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 /* Fig. 7.4: fig07_04.c

2 Using the & and * operators */

3 #include <stdio.h>

4

5 int main()

6 {

7 int a; /* a is an integer */

8 int aPtr; / aPtr is a pointer to an integer */

9

10 a = 7 ;

11 aPtr = &a; /* aPtr set to address of a */

12 13 printf( "The address of a is %p"

14 "\nThe value of aPtr is %p", &a, aPtr );

15

16 printf( "\n\nThe value of a is %d"

17 "\nThe value of *aPtr is %d", a, *aPtr );

18

19 printf( "\n\nShowing that * and & are complements of "

20 "each other\n&*aPtr = %p"

21 "\n&aPtr = %p\n", &aPtr, *&aPtr );

22

23 return 0 ; /* indicates successful termination */

24

25 } /* end main */

The address of a is the value fig07_04.c of aPtr.

The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a.

Notice how * and & are inverses

Pointers

 Pointers of the same type can be
assigned to each other
  • If not the same type, a cast operator

must be used

  • Exception: pointer to void (type void *)

 Generic pointer, represents any type

 No casting needed to convert a pointer to

void pointer

 void pointers cannot be dereferenced

void pointer

The type of the pointer has to match the
type of the variable being pointed to.
float flvar = 98.3;
int *iptr = &flvar; // error: can‟t assign
//float* to int*
void *vptr;
vptr = &flvar;
iptr = (int*)&flvar;

Pointers and Functions

Pointers and functions

 3 ways to pass arguments to a function
  • Pass by value
  • Pass by reference
  • Pass by address

Calling functions

 Call by reference with pointer arguments

  • Pass address of argument using & operator
  • Allows you to change actual location in memory
  • Arrays are not passed with & because the array name is
already a pointer

 * operator

  • Used as alias/nickname for variable inside of function
void doubleNum( int *number )
*number = 2 * ( *number );
  • *number used as nickname for the variable passed

Passing arguments by address

void func ( int *pValue )

{ *pValue = 6;

}

void main ( )

{ int nValue = 5;

printf(“nValue = %d\n“, nValue);

func ( &nValue );

printf("nValue = %d\n”, nValue);

} // 5 gets printed, then 6 gets printed docsity.com

OutlineOutline

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Program Output

The original value of number is 5 The new value of number is 125

OutlineOutline

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 /* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference with a pointer argument */ 3 4 #include <stdio.h> 5 6 void cubeByReference( int nPtr ); / prototype / 7 8 int main() 9 { 10 int number = 5 ; / initialize number / 11 12 printf( "The original value of number is %d", number ); 13 14 / pass address of number to cubeByReference / 15 cubeByReference( &number ); 16 17 printf( "\nThe new value of number is %d\n", number ); 18 19 return 0 ; / indicates successful termination / 20 21 } / end main / 22 23 / calculate cube of *nPtr; modifies variable number in main */ 24 void cubeByReference( int *nPtr ) 25 { 26 *nPtr = *nPtr * *nPtr * nPtr; / cube *nPtr / 27 } / end function cubeByReference */

fig07_07.c

Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable).

Inside cubeByReference, nPtr is used (nPtr is number).

Notice that the function prototype takes a pointer to an integer.