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

Understanding Pointers and Reference Passing in C, Slides of Computer Fundamentals

An explanation of pointers and reference passing in c programming language. It covers the concepts of passing pointers to functions, calling functions by reference, and using the const qualifier with pointers. The document also includes examples and program outputs.

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Pointers)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Understanding Pointers and Reference Passing in C and more Slides Computer Fundamentals in PDF only on Docsity!

Pointers

docsity.com

  • Passing Pointer to func/on
  • Pointers and Arrays
  • Array of Pointer
  • Dynamic Memory Alloca/on

Pointers

docsity.com

Calling Functions by Reference

  • 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 double( int *number ) { *number = 2 * ( *number ); }
      • *number used as nickname for the variable passed

docsity.com

.

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

`

nValue

5

pValue

00EF

00AA1 00EF

docsity.com

Outline

.

fig07_10.c (Part 1 of 2)

docsity.com

OutlineOutline

.

fig07_10.c (Part 2 of 2)

The string before conversion is: characters and Program Output $32. The string after conversion is: CHARACTERS AND $32.

docsity.com

OutlineOutline

.

fig07_11.c (Part 2 of 2)

Program Output The string is: print characters of a string

docsity.com

OutlineOutline

.

fig07_12.c

docsity.com

OutlineOutline

.

fig07_13.c

Program Output Compiling... FIG07_13.c D:\books\2003\chtp4\Examples \ch07\FIG07_13.c(15) : error C2166: l-value specifies const object Error executing cl.exe.

Changing *ptr is allowed – x is not a constant.

Changing ptr is an error – ptr is a constant pointer.

docsity.com

OutlineOutline

.

fig07_14.c

docsity.com

.

Swap

temp = x ;

x = y ;

y = temp ;

docsity.com

.

void swap ( int * , int * );

// function prototype

void main ( )

{

int x = 10 , y = 20 ;

printf(“Before calling swap \n”);

printf(“value of x=%d and y=%y \n”,x,y);

swap ( &x , &y ) ;

// function call

printf(“After calling swap \n”);

printf(“value of x=%d and y=%y \n”,x,y);

}

Swap using function

docsity.com

.

  • sizeof
    • Returns size of operand in bytes
    • For arrays: size of 1 element * number of elements
    • if sizeof( int ) equals 4 bytes, then int myArray[ 10 ]; printf( "%d", sizeof( myArray ) );
  • will print 40
  • sizeof can be used with
    • Variable names
    • Type name
    • Constant values

sizeof function

docsity.com