5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall 20081.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Arrays and Pointers in C Alan L. Cox
Java Unit 9: Arrays Declaring and Processing Arrays.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
A First Book of ANSI C Fourth Edition
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Characters and Strings
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
C is a high level language (HLL)
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Computer science C programming language lesson 3.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Computer Organization and Design Pointers, Arrays and Strings in C
INC 161 , CPE 100 Computer Programming
Chapter 8 Arrays, Strings and Pointers
Pointers in C.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
14th September IIT Kanpur
5. Arrays, Pointers and Strings
Lecture 18 Arrays and Pointer Arithmetic
7. Pointers, Dynamic Memory
Exercise Arrays.
Arrays.
Introduction to Pointers
Presentation transcript:

5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall 20081

Arrays An Array is a collection of variables of the same type that are referred to through a common name. Declaration type var_name[size] e.g C Course, Programming club, Fall int A[6]; double d[15];

Array Initialization After declaration, array contains some garbage value. Static initialization Run time initialization C Course, Programming club, Fall int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int i; int A[6]; for(i = 0; i < 6; i++) A[i] = 6 - i;

Memory addresses Memory is divided up into one byte pieces individually addressed. - minimum data you can request from the memory is 1 byte Each byte has an address. for a 32 bit processor, addressable memory is 2 32 bytes. To uniquely identify each of the accessible byte you need log = 32 bits 0A0x x C0x D0x ‘W’0x ‘o’0x ‘w’0x A ‘\0’0x B x x x x C Course, Programming club, Fall 20084

Array - Accessing an element int A[6]; 6 elements of 4 bytes each, total size = 6 x 4 bytes = 24 bytes Read an element Write to an element {program: array_average.c} A[0]A[1]A[2]A[3]A[4]A[5] 0x10000x10040x10080x10120x10160x C Course, Programming club, Fall int tmp = A[2]; A[3] = 5;

Strings in C No “Strings” keyword A string is an array of characters. OR C Course, Programming club, Fall char string[] = “hello world”; char *string = “hello world”;

Significance of NULL character ‘\0’ Compiler has to know where the string ends ‘\0’ denotes the end of string {program: hello.c} Some more characters (do $man ascii): ‘\n’ = new line, ‘\t’ = horizontal tab, ‘\v’ = vertical tab, ‘\r’ = carriage return ‘A’ = 0x41, ‘a’ = 0x61, ‘\0’ = 0x00 C Course, Programming club, Fall char string[] = “hello world”; printf(“%s”, string);

Pointers in C A char pointer points to a single byte. An int pointer points to first of the four bytes. A pointer itself has an address where it is stored in the memory. Pointers are usually four bytes. * is called the dereference operator *p gives the value pointed by p 4i p & (ampersand) is called the reference operator &i returns the address of variable i C Course, Programming club, Fall int *p;  int* p; int i = 4; p = &i;

More about pointers int x = 1, y = 2, z[10]; int *ip;/* A pointer to an int */ ip = &x;/* Address of x */ y = *ip;/* Content of ip */ *ip = 0;/* Clear where ip points */ ip = &z[0];/* Address of first element of z */ {program: pointer.c} C Course, Programming club, Fall 20089

Pointer Arithmetic A 32-bit system has 32 bit address space. To store any address, 32 bits are required. Pointer arithmetic : p+1 gives the next memory location assuming cells are of the same type as the base type of p. C Course, Programming club, Fall

Pointer arithmetic: Valid operations pointer +/- integer  pointer pointer - pointer  integer pointer pointer  invalid – pointer +/- pointer  invalid C Course, Programming club, Fall

Pointer Arithmetic: Example int *p, x = 20; p = &x; printf("p = %p\n", p); printf("p+1 = %p\n", (int*)p+1); printf("p+1 = %p\n", (char*)p+1); printf("p+1 = %p\n", (float*)p+1); printf("p+1 = %p\n", (double*)p+1); Sample output: p = 0022FF70 p+1 = 0022FF74 p+1 = 0022FF71 p+1 = 0022FF74 p+1 = 0022FF78 {program: pointer_arithmetic.c} C Course, Programming club, Fall

Pointers and arrays Pointers and arrays are tightly coupled. char a[] = “Hello World”; char *p = &a[0]; C Course, Programming club, Fall

Pointers and function arguments Functions only receive copies of the variables passed to them. {program: swap_attempt_1.c} A function needs to know the address of a variable if it is to affect the original variable {program: swap_attempt_2.c} Large items like strings or arrays cannot be passed to functions either. What is passed is the address of “hello world\n” in the memory. C Course, Programming club, Fall printf(“hello world\n”);

2-Dimensional Arrays (Array of arrays) int d[3][2]; Access the point 1, 2 of the array: d[1][2] Initialize (without loops): int d[3][2] = {{1, 2}, {4, 5}, {7, 8}}; C Course, Programming club, Fall

More about 2-Dimensional arrays d[0][0]d[0][1]d[0][2]d[0][3] d[1][0]d[1][1]d[1][2]d[1][3] d[2][0]d[2][1]d[2][2]d[2][3] A Multidimensional array is stored in a row major format. A two dimensional case:  next memory element to d[0][3] is d[1][0] What about memory addresses sequence of a three dimensional array?  next memory element to t[0][0][0] is t[0][0][1] C Course, Programming club, Fall