CSCE 206 Lab Structured Programming in C

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Introduction to C Programming CE
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CS1010 Programming Methodology
CS1010 Programming Methodology
Computer Organization and Design Pointers, Arrays and Strings in C
UNIT 5 C Pointers.
CSCI206 - Computer Organization & Programming
BILASPUR UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
Functions and Pointers
Hassan Khosravi / Geoffrey Tien
C++, OBJECT ORIENTED PROGRAMMING
Pointers.
Visit for more Learning Resources
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Programming Languages and Paradigms
Lecture 6 C++ Programming
void Pointers Lesson xx
Pointers.
Functions and Pointers
Tejalal Choudhary “C Programming from Scratch” Pointers
14th September IIT Kanpur
Buy book Online -
CSC 253 Lecture 8.
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Pointers  Week 10.
CSCE 206 Lab Structured Programming in C
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Introduction to Problem Solving and Programming
Dynamic Memory A whole heap of fun….
Pointers.
Initializing variables
CSCE 206 Lab Structured Programming in C
C++ Pointers and Strings
ECE Application Programming
Chien-Chung Shen CIS/UD
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Chapter 9: Pointers and String
Chapter 9: Pointers and String
The Stack.
CSCE 206 Lab Structured Programming in C
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
CSCE 206 Lab Structured Programming in C
Pointer Arithmetic By Anand George.
CSCE 206 Lab Structured Programming in C
C++ Pointers and Strings
CSCE 206 Lab Structured Programming in C
Introduction to Pointers
Presentation transcript:

CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 7

Pointer Pointers are variables that stores/points the address of another variables Address refers to the memory location of a variable

Pointer operators & is the address operator. It refers to the address of a variable. * is the dereference operator. It refers to value at a specific address. Why pointer? Call by reference Dynamic Memory Allocation

Pointer example int main(void) { int var = 50; int *ptr; ptr=&var; printf("value is %d\n", var); printf("value is %d\n", *ptr); printf("Address of value is %d", ptr); return 0; } Prints: value is 50 Address of value is 1001

Pointers on consecutive memory If the data you are iterating is consecutive in memory, such as arrays, you can also access consecutively using pointer arithmetic If you have a pointer to any array position you can use: ptr++ to go to the next array position ptr-- to go to the previous array position ptr += x to skip x array positions (beware, it must be within the array) ptr -= x same as previous but in the other direction

Pointers on consecutive memory To iterate through consecutive memory you need an end condition, for example, the address of the last valid consecutive memory In arrays it would be &array[size-1] The first item to iterate could be any, typically you want to use &array[0] The pointer arithmetic calculates automatically the address depending on the size of the pointer type you are using (char, int, double, …) Char would typically increase 1B, int 4B, double 8B

Iterate array with pointers int array[10]; int *start = &array[0]; int *end = &array[9]; int *current = start; while (current <= end) { scanf("%d", current); current++; }

Practice Problem-1 Write a program that adds 2 integers using pointers. Take 2 integers in 2 variables named a and b. Add those numbers and save it in another variable. Print the address and values of a, b and c.

Practice Problem-2 Write a program that takes 3 integers as input and find the largest number among them. Use pointer variables to do the comparison and output printing.

Practice Problem-3 Write a program that takes 2 integers as input and swap their values. You are required to write a function named SwapValues, that takes 2 pointer variables and within the function swap the values. Print the output from main function.