Introduction to Computer Organization & Systems

Slides:



Advertisements
Similar presentations
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.
Advertisements

POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Computer Science 210 Computer Organization Introduction to C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
CSE1301 Computer Programming: Lecture 6 Input/Output.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Introduction to Programming Lecture 5: Interaction.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2016 C Part IV.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
C Primer.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions and Pointers
Functions, Part 2 of 2 Topics Functions That Return a Value
Arrays Declarations CSCI N305
Quiz 11/15/16 – C functions, arrays and strings
Variables have a type have an address (in memory) have a value
Pointers.
CSCI206 - Computer Organization & Programming
Functions in C Mrs. Chitra M. Gaikwad.
POINTERS.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Functions and Pointers
Computer Science 210 Computer Organization
14th September IIT Kanpur
CSC 270 – Survey of Programming Languages
Computer Science 210 Computer Organization
The while Looping Structure
Programming and Data Structures
CSCE 206 Lab Structured Programming in C
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
Outline Defining and using Pointers Operations on pointers
Introduction to Computer Organization & Systems
Pointers.
7. Pointers, Dynamic Memory
Introduction to Computer Organization & Systems
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Introduction to Computer Organization & Systems
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Character Arrays char string1[] = “first”;
Programming Languages and Paradigms
CSCE 206 Lab Structured Programming in C
The while Looping Structure
CSCE 206 Lab Structured Programming in C
Introduction to Computer Organization & Systems
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
15213 C Primer 17 September 2002.
The while Looping Structure
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Introduction to Computer Organization & Systems John Barr Topics: Intro to C for C++ programmers Types in C: int and floating point C I/O

C Programming & Systems Programming Specific type of programming Not used in the development of most applications Emphasis is on conciseness and efficiency (space and time) Ignores readability and maintainability You will get fired if you program like this in most situations!

The three attributes of a C++/C variable A name A type A value

A C++ program that processes three integer values

A C++ program that processes three integer values Note the & #include <stdio.h> #define bonus 5 int exam1, exam2, score; main( ){ scanf(“%d %d”, &exam1, &exam2); score = (exam1 + exam2)/2 + bonus; printf(“score = %d\n”, score); } See Student/comp210/examples/testRead.c

A C++ program that processes three integer values (Cont’d)

Output: printf printf(“score = %d\n”,score);

Input: scanf scanf(“%d %d”,&exam1, &exam2);

The for statement with an array #include <stdio.h> #define SIZE 4 Int main() { int i; int v[SIZE]; for (i = 0; i < SIZE; i++) printf("Enter an integer: \n"); scanf("%d", &v[i]); } printf("v[%d] = %d: \n", i, v[i]); return 0; See Student/comp210/examples/array1.c

The for statement with an array (Cont’d) barr@comp210:~/Student/comp210/examples$ ./array1 Enter an integer: 9 8 7 6 v[0] = 9: v[1] = 8: v[2] = 7: v[3] = 6: barr@comp210:~/Student/comp210/examples$

The for statement with an array #include <stdio.h> #define SIZE 4 Int main() { int i; int v[SIZE]; for (i = 0; i < SIZE; i++) printf("Enter an integer: \n"); scanf("%d", &v[i]); } printf("v[%d] = %d: \n", SIZE-i-1, v[SIZE - i - 1]); return 0; Why the “- 1” ?? See Student/comp210/examples/array2.c

The for statement with an array (Cont’d) barr@comp210:~/Student/comp210/examples$ ./array2 Enter an integer: 9 8 7 6 v[3] = 6: v[2] = 7: v[1] = 8: v[0] = 9: barr@comp210:~/Student/comp210/examples$

Pointers #include <stdio.h> int main() { int n1, n2, *intPtr; intPtr = &n1; printf ( "Enter two numbers\n"); scanf("%d%d",&n1,&n2); printf ( "The numbers you entered are: %d and %d \n", n1, n2); printf ("n1 is %d\n", *intPtr); intPtr = &n2; printf ( "n2 is %d\n",*intPtr); *intPtr = n1 + n2; printf ( "%d + %d = %d\n",n1, *intPtr, n1 + *intPtr); return 0; } Pointer declaration Pointer assignment Accessing value that pointer points at Changing a value in variable that pointer points at See Student/comp210/examples/ptr1.c

Pointers Can create a pointer variable without memory To use must create memory There is a library function to do this: malloc #include <stdio.h> #include <stdlib.h> //need this library int main() { int *intPtr; // intPtr is a pointer to an int *intPtr = 10; // causes a seg fault!!

Pointers and Memory #include <stdio.h> #include <stdlib.h> //need this library for malloc int main() { int *intPtr; // intPtr is a pointer to an int intPtr = (int *)malloc(sizeof(int)); // must first allocate memory printf(“Enter an integer: “); scanf(“%d”, intPtr); printf(“You entered %d\n”, *intPtr); // why no & in front of intPtr? return 0; } No &!! Why? See Student/comp210/examples/ptr2.c

Arrays // array3.c #include <stdio.h> #define SIZE 5 int readints(int s[ ],int max) { int c,i=0; printf("Enter %d numbers: \n",max); while (i < max) scanf("%d",&s[i++]); return(i); } You don’t have to specify the size of an array that is a parameter See Student/comp210/examples/array3.c

Arrays // array3.c continued int main() { int line[SIZE]; int i, n; n = readints(line, SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < n; i++) printf("%d\n", line[i]); } You do have to specify the size of an array that is a variable (unless you want to use dynamic memory) See Student/comp210/examples/array3.c

Arrays // array4.c #include <stdio.h> #define SIZE 5 int readchars(char *s,int max) { int i=0; printf("Enter %d characters: \n",max); while (i < max){ scanf("%c",&s[i++]); } return(i); Instead of an array you could receive a pointer to an int and use that as an array. See Student/comp210/examples/array4.c

Arrays A pointer can be used as an array // arry4.c int main() { char *line; int i, n; line = (char *)malloc(SIZE*sizeof(char)); n = readchars(line, SIZE); printf("The char you entered are: \n"); for (i = 0; i < n; i++) printf("%c\n", *(line + i)); } return 0; A pointer can be used as an array Allocate memory for every array element (there are SIZE elements) This is pointer syntax and uses pointer arithmetic

Arrays barr@comp210:~/Student/comp210/examples$ !a array4 Enter 5 characters: abcde The char you entered are: a b c d e