Programming Languages and Paradigms

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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.
Pointers in C Rohit Khokher
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
UBC104 Embedded Systems Variables, Structures & Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
CSE 220 – C Programming malloc, calloc, realloc.
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
Programming Paradigms
Module 2 Arrays and strings – example programs.
CSCI206 - Computer Organization & Programming
Computer science C programming language Lesson 5
Arrays in C.
Lecture 6 C++ Programming
CSCI206 - Computer Organization & Programming
14th September IIT Kanpur
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
INC 161 , CPE 100 Computer Programming
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory, Data, & Addressing II CSE 351 Winter 2018
EECE.2160 ECE Application Programming
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Memory, Data, & Addressing II CSE 351 Winter 2018
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Strings #include <stdio.h>
Programming Languages and Paradigms
Pointers, Dynamic Data, and Reference Types
Chapter 16 Pointers and Arrays
Presentation transcript:

Programming Languages and Paradigms The C Programming Language (pointers, arrays, and strings)

Pointer types In C, any existing type may have an associated pointer type Example (for the int type) int i; /* defines an int variable */ int *p; /* defines a pointer-to-int variable */ p i 5 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointer values: addresses Pointer variables hold addresses of memory locations e.g., Here, address 9006 is a memory location that contains the integer value 5 p 9006 9006: 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointer operations Address operator & : from variables to addresses int num = 10; int *p; p = &num; De-referencing operator * : from addresses to variables printf( “%d\n”, num ); printf( “%d\n”, *p ); printf( “%d\n”, p ); p num 2020 2020: 10 Prints: 10 <some address> 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Arrays are pointers An array name represents the address of the first element of the array Example: int scores[3]; scores[0] = 5; scores[1] = 10; scores[2] = 8; int *p; p = scores; printf( “%d”, *p ); /* same as p[0] */ Prints: 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

What values do we assign to pointer variables? An address generated through & An array name Pointer arithmetic calculation Through the malloc() function For char* variables, string literals 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointer arithmetic Adding a number to a pointer provides an address Let p be a pointer value of type T and i is an integer Suppose the address P contains a sequence of T values p+i is the address of the ith T data object in memory In fact, *(p+i) refers to this ith value and is synonymous to p[i] which is why indexing operations like arr[i] works 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

malloc malloc(n) allocates n bytes of memory and returns the address of the first byte Example char *s; s = (char *) malloc( 10 ); /* allocates 10 characters */ /* can now use s like as if char s[10]; */ Need to use sizeof function/macro for other types int *p = (int *) malloc( 10*sizeof(int) ); 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

About malloc and dynamic allocation malloc() returns NULL if allocation fails malloc()’s return type is void * which is why a cast is required To free up memory previously allocated by malloc(), use free free( s ); free( p ); Multidimensional array allocation provides many options (array of pointers or pointer to arrays?) 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointers and the -> operator Suppose struct stype { … }; struct stype *s; (*s).field is equivalent to s->field Useful for deep data structures 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Strings String: sequence of characters (stored in a character array) The null (‘\0’) terminator String literals ( e.g., “hello” ) String functions (<string.h>) strcpy strlen strcmp strcat … 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.