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

Lecture Slides on Passing Variables | CSE A205, Study notes of Engineering

Material Type: Notes; Class: Introduction to C Programming for Engineers; Subject: Computer Systems Engineering ; University: University of Alaska - Anchorage; Term: Spring 2009;

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-npk
koofers-user-npk 🇺🇸

4

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294A
Introduction to C Programming for Engineers
Lecture #13
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Lecture Slides on Passing Variables | CSE A205 and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Passing Variables

Pass by Value

When a variable is passed by value into a function, the value of thevariable passed in is copied into the value of the variable in theparameter list

Any change that occurs to the function’s parameter in the functiondoes not affect the value of the variable in the calling function

void
func(int
num)
num
} void
main()
int
num
func(num); printf
(“%d”,
num);

Pointers

Pointers enable programs to simulate call-by-reference and to create and manipulate dynamicdata structures (which are data structures that cangrow and shrink at run time)

Pointers are variables whose values are memoryaddresses

All of the scalar variables we have discussedcontain specific values, not memory addresses

Pointer Declarations

To create a pointer, use the *

int *count_ptr;

count_ptr is then a pointer to an object of type int

To create an int variable, we use

int count;

Then, to say that count_ptr contains the address of the count variable,we use

count_ptr = &count;

The & is the way we get the memory address of a variable

When the * is used in any line

after

the declaration, it is called

dereferencing

Dereferencing will get the value at the address contained within thevariable

Pass by Reference

Using pointers, we can now get the same effect of passing variables byreference

void change_value (int *my_ptr)

*my_ptr

} void change_value (int val)

val

} void main()

int value

int *value_ptr; value_ptr

&value; printf (“%d”, value);

what is printed? change_value(value); printf (“%d”, value);

what is printed? change_value(value_ptr); printf (“%d”, value);

what is printed? change_value(&value); printf (“%d”, value);

what is printed? }

Program

Write a program to display the values of the address, value,and dereferenced values of pointers and scalar variables tobetter understand the * and & characters, as well aspointers.