Arrays and Pointers.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers What is the data type of pointer variables?
Array in C# Array in C# RIHS Arshad Khan
EGR 2261 Unit 11 Pointers and Dynamic Variables
Linked List :: Basic Concepts
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions and Pointers
Two-Dimensional Arrays
Lectures linked lists Chapter 6 of textbook
Computer Programming BCT 1113
Hassan Khosravi / Geoffrey Tien
MEMORY REPRESENTATION OF STACKS
Pointers and Pointer-Based Strings
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Pointer Data Type and Pointer Variables II
Visit for more Learning Resources
Hassan Khosravi / Geoffrey Tien
Programming Languages and Paradigms
Pointers.
Functions and Pointers
Pointers and References
Dynamic Memory Allocation
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Object Oriented Programming COP3330 / CGS5409
Multiple Dimension Arrays
Pointers, Dynamic Data, and Reference Types
Lecture 18 Arrays and Pointer Arithmetic
Pointer Data Type and Pointer Variables III
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
2-d Arrays.
Chapter 17: Linked Lists.
Multidimensional array
Objectives You should be able to describe: Addresses and Pointers
Lecture 3: Arrays & Pointers
7 Arrays.
Overloading functions
Dr Tripty Singh Arrays.
C (and C++) Pointers April 4, 2019.
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Outline Announcements Differences between FORTRAN and C
Chapter 9: Pointers and String
Arrays Imran Rashid CTO at ManiWeber Technologies.
Pointers and References
Session #6 Memory Allocation and leaks Structures
Pointers, Dynamic Data, and Reference Types
Introduction to Pointers
Presentation transcript:

Arrays and Pointers

int num[ ] = { 24, 34, 12, 44, 56, 17 } ; We know that on mentioning the name of the array we get its base address. Thus, by saying *num we would be able to refer to the zeroth element of the array, that is, 24. One can easily see that *num and *( num + 0 ) both refer to 24.

Similarly, by saying *( num + 1 ) we can refer the first element of the array, that is, 34. In fact, this is what the C compiler does internally. When we say, num[i], the C compiler internally converts it to *( num + i ).

This means that all the following notations are same: num[i] *( num + i ) *( i + num ) i[num]

address = 65512 element = 24 24 24 24 address = 65514 element = 34 34 34 34 address = 65516 element = 12 12 12 12 address = 65518 element = 44 44 44 44 address = 65520 element = 56 56 56 56 address = 65522 element = 17 17 17 17

2D ARRAYS

Address of 0 th 1-D array = 65508 Address of 1 th 1-D array = 65512 Address of 2 th 1-D array = 65516 Address of 3 th 1-D array = 65520

the expressions s[0] and s[1] would yield the addresses of the zeroth and first one-dimensional array respectively. From above figure these addresses turn out to be 65508 and 65512.

Now, we have been able to reach each one-dimensional array Now, we have been able to reach each one-dimensional array. What remains is to be able to refer to individual elements of a one dimensional array. Suppose we want to refer to the element s[2][1] using pointers.

We know that s[2] would give the address 65516, the address of the second one-dimensional array. Obviously ( 65516 + 1 ) would give the address 65518

( s[2] + 1 ) would give the address 65518 ( s[2] + 1 ) would give the address 65518. And the value at this address can be obtained by using the value at address operator, saying *( s[2] + 1 ).

But, we have already studied while learning one-dimensional arrays that num[i] is same as *( num + i ). Similarly, *( s[2] + 1 ) is same as, *( *( s + 2 ) + 1 ). Thus, all the following expressions refer to the same element, s[2][1] ( s[2] + 1 ) * ( * ( s + 2 ) + 1 )

And here is the output... 1234 56 1212 33 1434 80 1312 78

Pointer to an Array If we can have a pointer to an integer, a pointer to a float, a pointer to a char, then can we not have a pointer to an array? We certainly can. The following program shows how to build and use it.

Passing 2D array to a function

A more general formula for accessing each array element would be: * ( base address + row no. * no. of columns + column no. )

Array of Pointers int *arr[4];

Self Referential Structure Pointer to Structure Within the same Structure : Self Referential In this Type , Structure has pointer to itself. Pointer stores the address of the Structure of same type.

Syntax : struct node { int data; struct node *ptr; //Pointer to Struct node }; Here ptr stores address of Structure node. This is useful in Dynamic Memory Allocation and Creating Linked Lists.