UNIT 1: POINTERS & DYNAMIC MEMORY ALLOCATION. Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

Size: px
Start display at page:

Download "UNIT 1: POINTERS & DYNAMIC MEMORY ALLOCATION. Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru."

Transcription

1 UNIT 1: POINTERS & DYNAMIC MEMORY ALLOCATION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

2 Table of Contents 1. Simple pointer program demo Chaining of pointers D Arrays & array of pointers Pointers as function parameters: pointer to an integer Pointers as function parameters: pointer to char array Pointers as function parameters : pointer to char array Pointers as function return type Dynamic memory allocation: memory for single integer Dynamic memory allocation: memory for an array of elements Dynamic memory allocation: memory for structure instance Dynamic memory allocation: memory for array of structure instances Dynamic memory allocation: memory for an array using calloc Dynamic memory allocation: 2D arrays & Dynamic memory allocation Dynamic memory allocation: realloc & reducing size of allocated block Pointer to structure and member access Pointer to structure and member access. Structure having pointer type member Address of functions Function pointers Void pointer (void *) and automatic type conversion btn void * & int * Demo program to illustrate use of void *...25

3 1. Simple pointer program demo. int x =10; int *p1 = &x; int **p2; p2 = &p1; printf("\np2 address(&p2)= %X\np2 destination(p2)\ = %X\np2 1stlevel dest value(*p2) =%X\np2 2nd\ level dest value(**p2):%x\n",&p2, p2, *p2, **p2); printf("\np1 address(&p1)= %X\np1 destination(p1)\ = %X\np1 dest value(*p1) =%X\nvalue(x)=\ %X\n",&p1, p1, *p1, x);

4 2. Chaining of pointers int x = 10; int *p1 = &x; printf("x = %d\n",*p1); int **p2 = &p1; printf("x = %u\n",**p2); // Display what???

5 3. 2D Arrays & array of pointers int x[2][3] = 1,2,3,4,5,6; int i,j; // 2-D Array printf("\starting address of 2-D array: %u \n",x); for(i=0;i<2;i++) for(j=0;j<3;j++) printf("%d ",x[i][j]); printf("\n"); //1. pointer to the array. int *p = x; printf("\accessing as a linear array using pointer:linear\n"); for(i=0;i<2*3;i++) printf("%d ",*(p+i)); // 3= no.cols printf("\accessing as a 2D array using pointer\n"); for(i=0;i<2;i++) for(j=0;j<3;j++) printf("%d ",*(p+i*3+j)); //2. Pointer to array of 3 elements(here each row has 3 elems) int (*p1)[3] = x ; printf("\n21. Addres Val p1 = %u (p1+1) = %u\n",p1, (p1+1)); printf("\n22. Addres Val *p1 = %u *(p1+1) = %u\n",*p1, *(p1+1)); for(i=0;i<2;i++) for(j=0;j<3;j++) printf("%d ",*(*(p1+i)+j)); //3. Array of pointers to point to each row separately int *p2[2]; p2[0] = &x[0]; p2[1] = &x[1]; printf("\n31.address Val &p2 = %u p2 = %u *p2 = %u\n",&p2, p2, *p2); printf("\n32.address Val p2+1 = %u *(p2+1) = %u\n",p2+1, *(p2+1)); for(i=0;i<2;i++)

6 for(j=0;j<3;j++) printf("%d ",*(*(p2+i)+j));

7 5. Pointers as function parameters: pointer to an integer. void display_int(int *x); int num =10; display_int(&num); printf("x = %d",num); void display_int(int *p) printf("int val = %d\n",*p); *p =20; printf("int val = %d\n",*p);

8 6. Pointers as function parameters: pointer to char array. void display_string(char *); char s[] = "hello world!\n"; display_string(s); void display_string(char *str) while(*str!= '\0') putc(*str,stdout); str++;

9 7. Pointers as function parameters : pointer to char array. void display_numbers(int *, int); int nums[] = 1,2,3,4,5; display_numbers(nums,5); printf("\nagain display.\n"); display_numbers(nums,5); void display_numbers(int *n, int count) int i; for(i=0;i<count ; i++,n++) printf(" %d ",*n); (*n)++; // Modifiying the array elems

10 8. Pointers as function return type. int* larger(int *x,int *y); int p =30, q =20; int *z = larger(&p, &q); // pass-by-ref printf("larger number is %d\n",*z); *z = 100; //?? printf("number is %d\n",p); // What is printed int* larger(int *x, int *y) if(*x>*y) return x; else return y;

11 9. Dynamic memory allocation: memory for single integer #include <stdlib.h> int *x; x = (int*)malloc(sizeof(int)); *x = 100; printf("starting address = %u Value = %u\n",x, *x); free(x);

12 10. Dynamic memory allocation: memory for an array of elements. #include <stdlib.h> float *x; int numel = 10; // sie of array x = (float*)malloc(numel*sizeof(float)); // initializing the array int i=0; for(;i<numel;i++) *(x+i) = 100+i; // Displaying array printf("the dynamically allocated array contents are:\n"); for(i = 0;i<numel;i++) printf("%.0f",*(x+i - 1)); free(x); // freeing the memory

13 11. Dynamic memory allocation: memory for structure instance #include <stdlib.h> #include <string.h> struct student char name[20]; int usn; ; struct student * s; s = (struct student*)malloc(sizeof(struct student)); printf("size of struct student is %d\n",sizeof(struct student)); // Initializing strcpy(s->name, "Sachin Tendulkar"); s->usn = 200; printf("name is %s USN is %d\n",s->name,s->usn); free(s); // freeing the memory

14 12. Dynamic memory allocation: memory for array of structure instances. #include <stdlib.h> #include <string.h> struct student char name[20]; int usn; ; struct student * s; int n = 10; s = (struct student*)malloc(n*sizeof(struct student)); int i=0; // Initializing struct student * temp = s; for(;i<n;i++) strcpy(temp->name, "Sachin Tendulkar"); temp->usn = 100+i; temp++; // Displaying temp = s; for(i = 0;i<n;i++) printf("name is %s USN is %d\n",temp->name,temp->usn); temp++; free(s); // freeing the memory

15 13. Dynamic memory allocation: memory for an array using calloc #include <stdlib.h> float *x; int numel = 10; // size of array x = (float*)calloc(numel, sizeof(float)); // initializing the array int i=0; for(;i<numel;i++) *(x+i) = 100+i; // Displaying array printf("the dynamically allocated array contents are:\n"); for(i = 0;i<numel;i++) printf("%.2f ",*(x+i)); free(x); // freeing the memory

16 14. Dynamic memory allocation: 2D arrays & Dynamic memory allocation // Dynamic memory allocation: 2D arrays & Dynamic memory allocation #include <stdlib.h> int **x; int rows = 3,cols =4; printf("enter num. of rows & cols:\n"); scanf("%d%d",&rows,&cols); // Creating array of pointers for printing to each row x = (int**)malloc(rows*sizeof(int*)); // Allocating memory for each row int i=0; for(;i<rows;i++) *(x+i) = (int*)malloc(cols*sizeof(int)); int j =0; printf("enter %d elements of the martix:\n",rows*cols); // Initializing each elemet for(i=0;i<rows;i++)

17 for(j=0;j<cols;j++) //*(*(x+i)+j) = j; scanf("%d",*(x+i)+j); // Displaying array // The matrix is : for(i=0;i<rows;i++) for(j=0;j<cols;j++) printf("%d ",*(*(x+i)+j)); printf("\n"); // Freeing the memory: Free the rows for(i = 0;i<rows;i++) free(*(x+i)); // Free the array of pointers to rows free(x); // freeing the memory

18 15. Dynamic memory allocation: realloc & reducing size of allocated block. #include <stdlib.h> float *x; int i=0; int numel = 10; // sie of array x = (float*)malloc(numel*sizeof(float)); // initializing the array printf("address located: %u\n",x); for(;i<numel;i++) *(x+i) = 100+i; // or scanf( %d,x+i); // Displaying array printf("the dynamically allocated array contents are:\n"); for(i = 0;i<numel;i++) printf("%.2f ",*(x+i)); x = (float*)realloc(x,5*sizeof(float)); printf("new Address located: %u\n",x); // address remains same for(i = 0;i<numel;i++) printf("%.2f ",*(x+i)); free(x); // freeing the memory

19 16. Pointer to structure and member access. #include <stdlib.h> #include <string.h> struct student char name[20]; // Array in struct int usn; // Normal variable in struct ; struct student * s; s = (struct student*)malloc(sizeof(struct student)); printf("size of struct student is %d\n",sizeof(struct student)); printf("enter firstname & usn:"); scanf("%s%d",s->name, &s->usn); printf("name is %s USN is %d.\n",s->name,s->usn); free(s); // freeing the memory

20 17. Pointer to structure and member access. Structure having pointer type member. #include <stdlib.h> #include <string.h> struct student char name[20]; // Array in struct int usn; // Normal variable in struct char *section; // Pointer type member in structure ; struct student * s; s = (struct student*)malloc(sizeof(struct student)); printf("size of struct student is %d\n\n",sizeof(struct student)); // Initializing strcpy(s->name, "Sachin Tendulkar"); // or scanf("%s%d",s->name, &s->usn); s->usn = 200; // Allocating memory for pointer type member of structure s->section = (char*)malloc(sizeof(char)); *s->section = 'A'; // Initializing section printf("name:%s \nusn:%d \nsection:%c\n",s->name,s->usn, *s->section); free(s->section); // Freeing memory allocated for char variable free(s); // freeing the memory

21 18. Address of functions.. void fun(int x) printf("function fun() called\n"); printf("address of function main():%u\n", main); printf("address of function fun():%u\n", fun);

22 19. Function pointers void fun(int x) printf("function fun() called %d\n",x); void fun2(int x) printf("function fun2() called %d\n",x); void (*fptr)(int); // Function pointer defined. fptr = fun; //initializing function pointer with address of fun() fun(5); fptr(10); // calling fun() using function pointer. fptr = fun2; // calling fun() using function pointer. fptr(100);

23 20. Void pointer (void *) and automatic type conversion btn void * & int * int x = 99; int *p = &x; void *vp = p; int *ip = vp; printf("%d",*ip);

24 21. Demo program to illustrate use of void *. #include <string.h> struct student char name[20]; ; struct faculty char name[20];int empid; ; struct principal char name[20]; char hdegree; ; void display(void * ptr, int type) if(type == 1) // Student struct student *s = ptr; printf("student name %s\n",s->name); else if(type == 2) // faculty struct faculty *f = ptr; printf("faculty name %s empid:%d\n",f->name,f->empid); else if(type == 3) // principal // no harm with explicite casting. Improves code readability. struct principal *p = (struct principal *)ptr; printf("principal name %s degree: %c\n",p->name, p->hdegree); struct student s1; strcpy(s1.name, "Akhil"); struct faculty f1; strcpy(f1.name, "Narayana"); f1.empid = 401; struct principal p1; strcpy(p1.name, "Dr. Bhaskar"); p1.hdegree = 'P'; display(&s1,1); display(&f1,2); display(&p1,3);

25

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

Pointers, Arrays, and Strings. CS449 Spring 2016

Pointers, Arrays, and Strings. CS449 Spring 2016 Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.

More information

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Memory Allocation. General Questions

Memory Allocation. General Questions General Questions 1 Memory Allocation 1. Which header file should be included to use functions like malloc() and calloc()? A. memory.h B. stdlib.h C. string.h D. dos.h 2. What function should be used to

More information

More Arrays. Last updated 2/6/19

More Arrays. Last updated 2/6/19 More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1 C Pointers 1. Objective... 2 2. Introduction... 2 3. Pointer Variable Declarations and Initialization... 3 4. Reference operator (&) and Dereference operator (*) 6 5. Relation between Arrays and Pointers...

More information

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size] (November 10, 2009 2.1 ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved. Chapter 14 Dynamic Data Structures Instructor: Öğr. Gör. Okan Vardarlı Copyright 2004 Pearson Addison-Wesley. All rights reserved. 12-1 Dynamic Data Structure Usually, so far, the arrays and strings we

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

int marks[10]; // fixed size and fixed address No change in Memory address.

int marks[10]; // fixed size and fixed address No change in Memory address. Dynamic Memory Allocation : Used When we want to allocate memory during run time. int marks[10]; // fixed size and fixed address No change in Memory address. // fixed size. ( no change in size possible

More information

MODULE 1. Introduction to Data Structures

MODULE 1. Introduction to Data Structures MODULE 1 Introduction to Data Structures Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way. Data Structures is about

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Spring 2018 COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. Simple Queue Implementation with arrays...3 2. Circular queue with global container

More information

Single Dimension Arrays

Single Dimension Arrays ARRAYS Single Dimension Arrays Array Notion of an array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element,

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

UNIT - V STRUCTURES AND UNIONS

UNIT - V STRUCTURES AND UNIONS UNIT - V STRUCTURES AND UNIONS STRUCTURE DEFINITION A structure definition creates a format that may be used to declare structure variables. Let us use an example to illustrate the process of structure

More information

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

MODULE V: POINTERS & PREPROCESSORS

MODULE V: POINTERS & PREPROCESSORS MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which

More information

Programming in C Lab

Programming in C Lab Programming in C Lab 1a. Write a program to find biggest number among given 3 numbers. ALGORITHM Step 1 : Start Start 2 : Input a, b, c Start 3 : if a > b goto step 4, otherwise goto step 5 Start 4 : if

More information

POINTER & REFERENCE VARIABLES

POINTER & REFERENCE VARIABLES Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

Laboratory 10 POINTERS I. FUNDAMENTALS

Laboratory 10 POINTERS I. FUNDAMENTALS PROGRAMMING LANGUAGES Laboratory 10 POINTERS I. FUNDAMENTALS As already known, a program together with its data is stored in the computer s memory. The RAM memory is a random access memory (RAM) which,

More information

Content. In this chapter, you will learn:

Content. In this chapter, you will learn: ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

2.2 Pointers. Department of CSE

2.2 Pointers. Department of CSE 2.2 Pointers 1 Department of CSE Objectives To understand the need and application of pointers To learn how to declare a pointer and how it is represented in memory To learn the relation between arrays

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

20 Dynamic allocation of memory: malloc and calloc

20 Dynamic allocation of memory: malloc and calloc 20 Dynamic allocation of memory: malloc and calloc As noted in the last lecture, several new functions will be used in this section. strlen (string.h), the length of a string. fgets(buffer, max length,

More information

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS Programming Year 2017-2018 Industrial Technology Engineering Paula de Toledo Contents 1. Structured data types vs simple data types 2. Arrays (vectors and matrices)

More information

Arrays, Pointers and Memory Management

Arrays, Pointers and Memory Management Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by

More information

Memory Management. CSC215 Lecture

Memory Management. CSC215 Lecture Memory Management CSC215 Lecture Outline Static vs Dynamic Allocation Dynamic allocation functions malloc, realloc, calloc, free Implementation Common errors Static Allocation Allocation of memory at compile-time

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

Write a C program using arrays and structure

Write a C program using arrays and structure 03 Arrays and Structutes 3.1 Arrays Declaration and initialization of one dimensional, two dimensional and character arrays, accessing array elements. (10M) 3.2 Declaration and initialization of string

More information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

Informatica e Sistemi in tempo Reale

Informatica e Sistemi in tempo Reale Informatica e Sistemi in tempo Reale Introduzione alla Programmazione C- II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant

More information

PDS Class Test 2. Room Sections No of students

PDS Class Test 2. Room Sections No of students PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not

More information

Basic and Practice in Programming Lab7

Basic and Practice in Programming Lab7 Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;

More information

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

COL 100 Introduction to Programming- MINOR 1 IIT Jammu COL 100 Introduction to Programming- MINOR 1 IIT Jammu Time 1 Hr Max Marks 40 03.09.2016 NOTE: THERE 4 QUESTIONS IN ALL NOTE: 1. Do all the questions. 2. Write your name, entry number and group in all

More information

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. C Program to Check for balanced parenthesis by using Stacks...3 2. Program

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1 Chapter 8 Arrays, Addresses, and Pointers 204112: Structured Programming 204112 Structured Programming 1 Pointer Pointer is a variable that contains an address. If num_ptr is a pointer, *num_ptr means

More information

Test Paper 3 Programming Language Solution Question 1: Briefly describe the structure of a C program. A C program consists of (i) functions and (ii) statements There should be at least one function called

More information

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming Procedural Programming & Fundamentals of Programming Exercise 3 (SS 2018) 29.05.2018 What will I learn in the 4. exercise Pointer (and a little bit about memory allocation) Structure Strings and String

More information

ESc101: Pointers and Arrays

ESc101: Pointers and Arrays ESc101: Pointers and Arrays Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 The content of some of these slides are from the lecture slides of Prof. Arnab Bhattacharya 2 1 Movie Theater Seat

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 9 Principles of C and Memory Management Dr Lei Shi Last Lecture Today Pointer to Array Pointer Arithmetic Pointer with Functions struct Storage classes typedef union String struct struct

More information

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty. ECE 264 Exam 2 6:30-7:30PM, March 9, 2011 I certify that I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise you will receive a 1-point penalty.

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class. Midterm Written Exam Practice Midterm will cover all concepts covered up to the midterm exam. Concepts of arrays, LL s, pointers (*,**,***), malloc, calloc, realloc, function pointers, Hash tables will

More information

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array? 1 What is an array? Explain with Example. What are the advantages of using an array? An array is a fixed-size sequenced collection of elements of the same data type. An array is derived data type. The

More information

Worksheet 11 Multi Dimensional Array and String

Worksheet 11 Multi Dimensional Array and String Name: Student ID: Date: Worksheet 11 Multi Dimensional Array and String Objectives After completing this worksheet, you should be able to Declare multi dimensional array of elements and string variables

More information

Memory Allocation in C

Memory Allocation in C Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called

More information

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

More information

C Program Reviews 2-12

C Program Reviews 2-12 Program 2: From Pseudo Code to C ICT106 Fundamentals of Computer Systems Topic 12 C Program Reviews 2-12 Initialise linecount and alphacount to zero read a character while (character just read is not end

More information

Basis and Practice in Programming Lab 9

Basis and Practice in Programming Lab 9 Basis and Practice in Programming Lab 9 User defined data structure Consists of one or more basic data type struct point // declaring a structure named as point int x; int y; // a member of the structure

More information

Final Intro to C Review

Final Intro to C Review Final Exam Content: Final Intro to C Review - Pass by reference Functions - General Syntax - Structures - Recursion(maybe?) - Programming by nature is cumulative so any past material is up for grabs as

More information

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables) Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)

More information

C LANGUAGE A Short Course

C LANGUAGE A Short Course C LANGUAGE A Short Course Alvaro F. M. Azevedo http://www.fe.up.pt/~alvaro January 2002 C Language - Alvaro Azevedo 1 ANSI C Standard (ANSI, ISO) Compiled - efficient Low level / high level Other languages

More information

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7 Strings and Clases BIL104E: Introduction to Scientific and Engineering Computing Lecture 7 Manipulating Strings Scope and Storage Classes in C Strings Declaring a string The length of a string Copying

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Final Examination December 14, 2012 2:00 p.m. 4:30 p.m. (150 minutes) Examiners: J. Anderson, B. Li, M. Sadoghi,

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same

More information

Chapter 7 Solved problems

Chapter 7 Solved problems Chapter 7 7.4, Section D 20. The coefficients of a polynomial function are stored in a single-dimensional array. Display its derivative. If the polynomial function is p(x) = a n x n + a n-1 x n-1 + + a

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf(%f\n, *j); You try /* EXAMPLE 3 */ #include int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; /* EXAMPLE

More information

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size APS105 Malloc and 2D Arrays Textbook Chapters 6.4, 10.2 Datatype Size Datatypes have varying size: char: 1B int: 4B double: 8B int sizeof(): a builtin function that returns size of a type int x =

More information

Contents ARRAYS. Introduction:

Contents ARRAYS. Introduction: UNIT-III ARRAYS AND STRINGS Contents Single and Multidimensional Arrays: Array Declaration and Initialization of arrays Arrays as function arguments. Strings: Initialization and String handling functions.

More information

2. Which of the following will print the value 2 for the above code?

2. Which of the following will print the value 2 for the above code? Computers are good at following instructions, but not at reading your mind. - Donald Knuth IMPORTANT QUESTIONS ON C LANGUAGE 1. What is the output of this program? char *ptr; char string[] = "How are you?";

More information

High Performance Programming Programming in C part 1

High Performance Programming Programming in C part 1 High Performance Programming Programming in C part 1 Anastasia Kruchinina Uppsala University, Sweden April 18, 2017 HPP 1 / 53 C is designed on a way to provide a full control of the computer. C is the

More information

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros. C Programming The C Preprocessor and Some Advanced Topics June 03, 2005 Learn More about #define Define a macro name Create function-like macros to avoid the time might be longer #define SUM(i, j) i+j

More information

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers Overview The C Programming Language Part 4 (with material from Dr. Bin Ren, William & Mary Computer Science, and www.cpp.com) Basic Concepts of and Arrays and Strings Dynamic Memory Allocation 1 2 pointer

More information

MTH 307/417/515 Final Exam Solutions

MTH 307/417/515 Final Exam Solutions MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =

More information

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and The C Programming Language Part 4 (with material from Dr. Bin Ren, William & Mary Computer Science, and www.cpp.com) 1 Overview Basic Concepts of Pointers Pointers and Arrays Pointers and Strings Dynamic

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

Two Dimensional Arrays

Two Dimensional Arrays 2-d Arrays 1 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of values Student 1 Student 2 Student 3 Student 4 Subject

More information

Computers Programming Course 12. Iulian Năstac

Computers Programming Course 12. Iulian Năstac Computers Programming Course 12 Iulian Năstac Recap from previous course Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of

More information

{ int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } A. Function addmult() return 7 and 12 B. No output C. Error: Compile error D.

{ int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } A. Function addmult() return 7 and 12 B. No output C. Error: Compile error D. SAP TECHNICAL PAPER 1. If int is 2 bytes wide.what will be the output of the program? void fun(char**); char *argv[] = "ab", "cd", "ef", "gh"; fun(argv); void fun(char **p) char *t; t = (p+= sizeof(int))[-1];

More information

Engineering program development 6. Edited by Péter Vass

Engineering program development 6. Edited by Péter Vass Engineering program development 6 Edited by Péter Vass Variables When we define a variable with its identifier (name) and type in the source code, it will result the reservation of some memory space for

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

Compiling and Running a C Program in Unix

Compiling and Running a C Program in Unix CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name

More information

2. Pointer to Array of Structure stores the Base address of the Structure array.

2. Pointer to Array of Structure stores the Base address of the Structure array. Pointer to Array of Structure 1. Pointer Variable can also Store the Address of the Structure Variable. 2. Pointer to Array of Structure stores the Base address of the Structure array. 3. Suppose struct

More information

Tech Mahindra Placement Paper 1. C-Test Paper. 1. #include. * What is wrong in the following problem. main() { int i,j; j = 10;

Tech Mahindra Placement Paper 1. C-Test Paper. 1. #include. * What is wrong in the following problem. main() { int i,j; j = 10; Tech Mahindra Placement Paper 1 C-Test Paper 1. #include * What is wrong in the following problem int i,j; j = 10; i = j++ - j++; printf("%d %d", i,j); ans: 0, 12 2.#include * What is the output of the

More information

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths

More information

Dynamic Allocation of Memory Space

Dynamic Allocation of Memory Space C Programming 1 Dynamic Allocation of Memory Space C Programming 2 Run-Time Allocation of Space The volume of data may not be known before the run-time. It provides flexibility in building data structures.

More information

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani Arrays Dr. Madhumita Sengupta Assistant Professor IIIT Kalyani INTRODUCTION An array is a collection of similar data s / data types. The s of the array are stored in consecutive memory locations and are

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

More information

Lecture 9 - Pointers 1

Lecture 9 - Pointers 1 Lecture 9 - Pointers 1 Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer Expressions and Pointer Arithmetic Relationship between

More information

int Return the number of characters in string s.

int Return the number of characters in string s. 1a.String handling functions: Function strcmp(const char *s1, const char *s2) strcpy(char *s1, const char *s2) strlen(const char *) strcat(char *s1, Data type returned int Task Compare two strings lexicographically.

More information