SBE201 Data Structures and Algorithms in C
|
|
- Alexina Price
- 5 years ago
- Views:
Transcription
1 SBE201 Data Structures and Algorithms in C Tutorial 3 Dina S. El-Kholy, Islam S. Badreldin Biomedical Engineering Department Faculty of Engineering Cairo University March 13, 2010
2 Copyright Copyright c 2010 Dina S. El-Kholy, Islam S. Badreldin. Some rights reserved. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License
3 Outline 1 Arrays Introduction Array Representation in Memory Reading and Writing 2 Strings Strings as Character Arrays The Null Termination Character String Manipulation 3 Pointers Introduction Pointer Dereferencing Pointers and Arrays Pointers and Strings Bad Pointers and the NULL Pointer
4 Arrays Outline 1 Arrays Introduction Array Representation in Memory Reading and Writing 2 Strings Strings as Character Arrays The Null Termination Character String Manipulation 3 Pointers Introduction Pointer Dereferencing Pointers and Arrays Pointers and Strings Bad Pointers and the NULL Pointer
5 Arrays Introduction Array Introduction Sometimes we want to be able to refer to a number of similar data elements collectively It will not be convenient to have a unique variable name for each element The Arrays are useful in such situations Arrays are collection of variables of a certain type that are placed contiguously in memory In order to define an array, like a variable, the compiler needs to know the data type of the elements contained in the array and the size of the array.
6 Arrays Introduction Array definition Example Let us define an array for the students marks, float marks[100]; The array definition involves the array elements data type (float), the array name (marks), and the array size enclosed in squared brackets (100)
7 Arrays Introduction Array initialization We can initialize the array as follows: int numbers[4]={1,2,3,4,5; Also we can omit the array size from the initialization, the compiler will simply count the number of elements you placed in the array and use it as the array size: int numbers[]={1,2,3,4,5;
8 Arrays Array Representation in Memory How does the array look like in the memory? Consider we declare an array s of type int and a size of 4 as follows Example int s[3]; Here is how the array will look like in the memory:
9 Arrays Array Representation in Memory Accessing array elements We can access individual array elements through indexing The first element index in C is zero, so to access element number zero of an array we use the statement: array_name[0] The last element index in C is size of array-1, so to access the last element of an array we use the statement: array_name[size of array-1]
10 Arrays Array Representation in Memory Array elements are contiguous in memory! Try out the following example. Note that the operator & gets the address of the variable in memory Example #include <stdio.h> int main(){ int s[4]; printf("%x\n",&s[0]); printf("%x\n",&s[1]); printf("%x\n",&s[2]); printf("%x\n",&s[3]); return 0; Examine the output: the addresses are consecutive and separated by 4 bytes since we declared an int array
11 Arrays Array Representation in Memory How does the compiler calculate the address of each element in the array? Examine the addresses obtained from the previous program Each element in the array has a distinct address but all are related! The first element of the array is given an address known as the base address Also, using the array name alone is equivalent to the address of the first element of the array To get the address of element i in the array we define the offset=i, then the address of element i is: Calculating the address base address + offset sizeof(array data type)
12 Arrays Array Representation in Memory Try this out! This example illustrates that the array name is equivalent to the address of the first array element! Example #include <stdio.h> int main(){ long int s[4]; printf("the array first element address \%x \n",&s[0]); printf("the value of the array name \%x \n",s); return 0;
13 Arrays Reading and Writing Reading and writing data to an array Try out this example, we will place values from 0 to 10 in the array and then print them out on the screen Example #include <stdio.h> int main(){ char arrays[11]; // size of the array=11 int i,j; for(i=0;i<11;i++) // loop stops at index size-1=11-1=10 { arrays[i]=i*i+5; for(j=0; j<=10;j++) // loop stops at index size-1=11-1=10 { printf("%d \n",arrays[j]);
14 Arrays Reading and Writing Out-of-bounds problem As a part of the array declaration, you are the one who decides the size of the array, that is the array size cannot be changed in runtime We emphasize that the array elements can be accessed through indexing the array from array[0] till array[size-1] What happens when you try to access an array location out of the size of the array?
15 Arrays Reading and Writing Try out this example Example #include <stdio.h> int main(){ int i,s[4],t[4],u=0; for(i=0; i<=4 ; i++)//note:we accesselement#5 of the array!! { s[i]=i; t[i]=i; printf("s:t \n"); for(i=0; i<=4;i++) { printf("%d:%d\n",s[i],t[i]); printf("%x %x", &s[0],&t[4]);
16 Arrays Reading and Writing Remarks In the previous example, We tried to access s[4] and t[4]. Both are not in the arrays bounds (size of the array=4) The C compiler did not complain but the content of s[0] is not right! Remember: you should always make sure you are not accessing an element outside of the array bounds, the compiler will not warn you and you will get wrong results, or the program may crash!
17 Strings Outline 1 Arrays Introduction Array Representation in Memory Reading and Writing 2 Strings Strings as Character Arrays The Null Termination Character String Manipulation 3 Pointers Introduction Pointer Dereferencing Pointers and Arrays Pointers and Strings Bad Pointers and the NULL Pointer
18 Strings Strings as Character Arrays Strings Strings are the form of data used in programming languages for storing and manipulating text such as words, names, and sentences In C, strings are represented by arrays of characters, meaning that is an array of type char So far, we only dealt with constant strings, like "Greetings!" in this example: Example printf("%s","greetings");
19 Strings Strings as Character Arrays Strings Now, how is the constant string Greetings represented in memory? this is how it looks like in the memory: What is the last character( 0 ) in the array? This is the null termination character
20 Strings The Null Termination Character The null termination character The null character stands for the character with numerical value zero Both the expressions char c= \0 and char c=0 are equivalent! They both place a numerical value of zero inside the variable c, which is called the null termination character Any string is automatically ended with this null character The null character is used to mark the ending of the string So, when you allocate a character array for your string, do not forget to count the null character!
21 Strings The Null Termination Character Declaring and initializing a string variable The are 2 possible ways to declare and initialize a string variable: char string[]={ c, a, t, \0 ; char string[]="cat"; The 2 ways are equivalent, in the second case the double quotes let the compiler know that this is a special char array which is a string and so the compiler will automatically terminate the string for you by the null terminator Also, no need to specify the array size for the compiler, the compiler will count the number of characters which is equal to 3 here and add the null character which makes the char array size equals 4
22 Strings String Manipulation String functions In order to work with strings, The C library contains a few basic string manipulation functions, these functions can be found in the <string.h> header file The function strlen is one famous function that returns the length of the string not including the null terminator C never lets you assign entire arrays, so the function strcpy(string2,string1) will let you copy string1 to string2, but make sure that string2 is allocated big enough to hold string1 Try to check strcat and strcmp as well!
23 Strings String Manipulation Strings example Example #include <stdio.h> #include <string.h> int main(){ char yourname[5]; strcpy(yourname,"name"); int i; int string_length; string_length=strlen(yourname); for(i=0;i<string_length+1;i++) { printf("%c:%i\n",yourname[i],yourname[i]);
24 Pointers Outline 1 Arrays Introduction Array Representation in Memory Reading and Writing 2 Strings Strings as Character Arrays The Null Termination Character String Manipulation 3 Pointers Introduction Pointer Dereferencing Pointers and Arrays Pointers and Strings Bad Pointers and the NULL Pointer
25 Pointers Introduction Pointers Up till now, we accessed variables in memory directly using the variable name Pointers provide a way of accessing a variable in an indirect way without referring to the variable itself but using the address of the variable in memory So, a pointer is a variable (location in memory) that holds an address of the location of some other variable The variable pointed to by the pointer can be of any data type
26 Pointers Introduction Pointer constant and pointer variable A pointer constant is an address while a pointer variable is a memory location that stores addresses The array name is a constant pointer (address) to the first element of an array. You can not modify its value since the linker decides where the array will go and it will stay there during the program execution C enables you to define pointer variables where you can store addresses and also modify these addresses
27 Pointers Introduction Defining Pointer variables The pointer variable definition looks like: <data type> * pointer variable name; The asterisk* sign tells the compiler that this variable will hold an address and not a value The data type tells the compiler that the address carried in this pointer variable is the address of a variable of this particular data type
28 Pointers Introduction What is the size of the pointer variable in memory? Example #include <stdio.h> int main(){ int *x; float *y; double *z; char *l; printf("the size of pointer to an int=%i\n",sizeof(x)); printf("the size of pointer to float=%i\n",sizeof(y)); printf("the size of pointer to double=%i\n",sizeof(z)); printf("the size of pointer to char=%i\n",sizeof(l)); return 0;
29 Pointers Introduction The & operator We said that pointer variables carry the address of some other variable, so to assign the an address of the variable to a pointer we use the & operator Example char a=17; char *b=&a; Here is how this may look like in the memory:
30 Pointers Pointer Dereferencing Pointer dereferencing We can access the variable pointed to by the pointer by using the * operator The *symbol has two uses, one is to tell the compiler that this variable is a pointer and the other is to access the variable pointed to by the pointer, the compiler can differentiate between them depending on the context of their appearance Example #include <stdio.h> int main(){ int x=5; int* p=&x; *p=(*p)*(2); printf("the value of x is %i",x);
31 Pointers Pointer Dereferencing Pointer and functions We saw before with function that we can only return one single value form the called function to the calling function, what if we want more that one variable to be modified by the called function? One solution is to use pointers as the function input arguments, here is an example:
32 Pointers Pointer Dereferencing Example #include <stdio.h> void rets2(int* px,int* py)//the function inputs are pointers {*px=3; /*derefrencing the pointer to access the variable x in main()*/ *py=5; int main() { int x=0; int y=0; rets2(&x,&y);//passing the addresses of x and y to the function printf("\nthe value of x is %i and y is %i \n",x,y);
33 Pointers Pointers and Arrays Pointers with arrays The following 2 examples serve the same purpose: Example #include <stdio.h> int main(){ int nums[]={92,81,70,69,58; int index; for (index=0;index<5;index++) { printf("\n%d",nums[index]);
34 Pointers Pointers and Arrays Example #include <stdio.h> int main(){ int nums[]={92,81,70,69,58; int index; for (index=0;index<5;index++) { printf("\n%d",*(nums+index));
35 Pointers Pointers and Arrays
36 Pointers Pointers and Arrays Arrays as constant pointers Example #include <stdio.h> int main (){ int numbers[]={10,20,30,40,50,-1; int index=0; while(*(numbers+index)>0) { printf("non-negative numbers are %i\n",*(numbers+index)); index++;
37 Pointers Pointers and Arrays Arrays as constant pointers The previous example had no problem, now try this: Example #include <stdio.h> int main (){ int numbers[]={10,20,30,40,50,-1; while(*(numbers++)>0) { printf("non-negative numbers are %i\n",*(numbers++));
38 Pointers Pointers and Arrays Try out this example Example #include <stdio.h> int main() { char *array="mona"; int i; for(i=0;i<=3;i++) {printf("%c \n",*(array++)); printf("%i \n",array); array--; printf("%c \n",*(array));
39 Pointers Pointers and Arrays Passing Arrays to functions This example illustrates how pointers can be used to let a function access and modify array elements: Example #include <stdio.h> #define SIZE 5 void addcon(int *ptr,int size,int con) {int k; for(k=0;k<size;k++) {*(ptr+k)=*(ptr+k)+con; int main () {int array[size]={3,5,7,9,11; int konst=10; int j; addcon(array,5,konst); for(j=0;j<size;j++){ printf("%d ",*(array+j));
40 Pointers Pointers and Strings Pointers and strings Example #include <stdio.h> int main(){ char *p="greetings"; printf("the whole string %s \n",p); printf("the first character of the string %c \n",*p); printf("the second character of the string is %c \n",*(p+1));
41 Pointers Bad Pointers and the NULL Pointer Bad pointers When a pointer is first allocated and not initialized, we call it a bad pointer Example We do not know where the bad pointer is pointing at Do not dereference a bad pointer. It may result in program crash! #include <stdio.h> int main(){ int* y; *y=*y+5; printf("%i",*y);
42 Pointers Bad Pointers and the NULL Pointer The NULL pointer The constant NULL is a special pointer value which encodes the idea of points to nothing The C language uses the NULL symbol for this purpose It is a runtime error to dereference a NULL pointer (Segmentation fault program crash) The NULL pointer is nothing more than assigning a decimal value of zero as the address inside the pointer variable
43 Appendix For Further Reading Further Reading Robert Lafore. C Programming Using Turbo C++. Wikibooks Contributors. C Programming. Wikibooks, Online: Programming
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 informationSir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7
Computer Programming & Problem Solving ( CPPS ) Chapter No 7 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan
More informationParameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265
Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =
More informationI2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub 2Computer Science BS Degree -Imperative Programming
More informationGabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming
1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are
More informationProcedural Programming & Fundamentals of Programming
Procedural Programming & Fundamentals of Programming Lecture 3 - Summer Semester 2018 & Joachim Zumbrägel What we know so far... Data type serves to organize data (in the memory), its possible values,
More information[0569] p 0318 garbage
A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the
More informationCreate a Program in C (Last Class)
Create a Program in C (Last Class) Input: three floating point numbers Output: the average of those three numbers Use: scanf to get the input printf to show the result a function to calculate the average
More informationEngineering 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 informationC++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters
C++ Arrays Areas for Discussion Strings and Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Lecture Arrays 1 Lecture Arrays 2 To declare an array: follow
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationVariation of Pointers
Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before
More informationECE 15B COMPUTER ORGANIZATION
ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19
More informationComputer 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 informationtype arrayname [ size type arrayname [ size ] = { item ,, item size size
CMPE-3/L and Strings Gabriel Hugh Elkaim Winter 24 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are uniquely
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationTo declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows
Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful
More informationComputers Programming Course 10. Iulian Năstac
Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in
More informationPointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays
EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and
More informationEM108 Software Development for Engineers
EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and
More informationChapter IV Introduction to C for Java programmers
Chapter IV Introduction to C for Java programmers Now that we have seen the native instructions that a processor can execute, we will temporarily take a step up on the abstraction ladder and learn the
More informationWeek 8: Arrays and File I/O. BJ Furman 21OCT2009
Week 8: Arrays and File I/O BJ Furman 21OCT2009 The Plan for Today Arrays What is an array? How do you declare and initialize an array? How can you use an array? Array Examples Array Practice File I/O
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationPointers. Memory. void foo() { }//return
Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationBil 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 informationgcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world
alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationPointers. Reference operator (&) ted = &andy;
Pointers We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory,
More informationtoday cs3157-fall2002-sklar-lect05 1
today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical
More informationONE DIMENSIONAL ARRAYS
LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers
More informationLecture07: Strings, Variable Scope, Memory Model 4/8/2013
Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationby Pearson Education, Inc. All Rights Reserved.
The string-handling library () provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationComputer 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 informationFORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere
More informationMemory, Arrays & Pointers
1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator
More informationPointers in C/C++ 1 Memory Addresses 2
Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationReference operator (&)
Pointers Each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it
More informationLecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings
Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in
More informationA Fast Review of C Essentials Part I
A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types
More informationCSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming
Arrays CSE101-Lec#17 (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline To declare an array To initialize an array To pass an array to a function Arrays Introduction
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More informationArrays. 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 informationComputers Programming Course 11. Iulian Năstac
Computers Programming Course 11 Iulian Năstac Recap from previous course Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in
More informationTutorial 10 Pointers in C. Shuyue Hu
Tutorial 10 Pointers in C Shuyue Hu Content Basic concept of pointers Pointer arithmetic Array of pointers Pointer to pointer Passing pointers to functions in C Return pointer from functions in C 2 Content
More information(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University
(13-2) Dynamic Data Structures I H&K Chapter 13 Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University Dynamic Data Structures (1) Structures that expand and contract
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationSYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University
SYSC 2006 C Winter 2012 String Processing in C D.L. Bailey, Systems and Computer Engineering, Carleton University References Hanly & Koffman, Chapter 9 Some examples adapted from code in The C Programming
More informationCS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays
CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements
More informationArithmetic Expressions in C
Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic
More informationCS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers
CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,
More informationPointers. 10/5/07 Pointers 1
Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include
More informationQUIZ on Ch.8. What is kit?
QUIZ on Ch.8 What is kit? QUIZ on Ch.8 What is kit? A: A vector of nested structures! QUIZ on Ch.8 C crash-course Compilation and Execution of a C Program Run-time Interpreted languages (like MATLAB, Python,
More informationSlide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide
More informationString constants. /* Demo: string constant */ #include <stdio.h> int main() {
Strings 1 String constants 2 /* Demo: string constant */ #include s1.c int main() { printf("hi\n"); } String constants are in double quotes A backslash \ is used to include 'special' characters,
More informationCharacters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,
Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape
More informationComp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57
Comp 11 Lectures Mike Shah Tufts University June 26, 2017 Mike Shah (Tufts University) Comp 11 Lectures June 26, 2017 1 / 57 Please do not distribute or host these slides without prior permission. Mike
More informationRicardo 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 informationWrite 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 informationC Programming Review CSC 4320/6320
C Programming Review CSC 4320/6320 Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile &
More informationWhy Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...
Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such
More informationProcedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationTopics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin
15-123 Effective Programming in C and Unix Announcements SL2 is due Thursday 1/20 midnight Complete the Academic Honesty Form in class All code downloads are from /afs/andrew/course/15/123/download All
More informationby Pearson Education, Inc. All Rights Reserved.
Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]
More informationAdvanced Pointer Topics
Advanced Pointer Topics Pointers to Pointers A pointer variable is a variable that takes some memory address as its value. Therefore, you can have another pointer pointing to it. int x; int * px; int **
More informationChapter 10. Arrays and Strings
Christian Jacob Chapter 10 Arrays and Strings 10.1 Arrays 10.2 One-Dimensional Arrays 10.2.1 Accessing Array Elements 10.2.2 Representation of Arrays in Memory 10.2.3 Example: Finding the Maximum 10.2.4
More informationBinary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal
Decimal Representation Binary Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write
More informationDecimal Representation
Decimal Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write number as 4705 10
More informationPES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo
PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci
More informationArrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common
More informationComputers 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 informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationBinghamton University. CS-211 Fall Pointers
Pointers 1 What is a pointer? Says I m not important what s important is over there Points AT or TO something else 2 Memory Array of bytes Each element has a value 0 1 2 3 xffff fffd xffff fffe xffff ffff
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationCS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016
CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers Kevin Webb Swarthmore College March 1, 2016 Overview Accessing things via an offset Arrays, Structs, Unions How complex structures are stored
More informationarrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review
of char imperative week 3 and Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 : miscellaneous of char several library functions are have put or get in their name
More informationCOMP26120: Pointers in C (2018/19) Lucas Cordeiro
COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11
More informationArrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga
Arrays and Pointers CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 2 Alaca & Vrbik (UTM)
More informationC Pointers. CS 2060 Week 6. Prof. Jonathan Ventura
CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.
More informationSingle 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 informationOutline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging
Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture
More informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Seven: Strings and Files Name: First Name: Tutor: Matriculation-Number: Group-Number:
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics 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 address (rather than the value)
More informationEL2310 Scientific Programming
Lecture 11: Structures and Memory (yaseminb@kth.se) Overview Overview Lecture 11: Structures and Memory Structures Continued Memory Allocation Lecture 11: Structures and Memory Structures Continued Memory
More informationChapter 2 (Dynamic variable (i.e. pointer), Static variable)
Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #
More informationData Types and Computer Storage Arrays and Pointers. K&R, chapter 5
Data Types and Computer Storage Arrays and Pointers K&R, chapter 5 Fundamental Data Types Most fundamental types are numeric (integer): - char - signed or unsigned - short int - signed or unsigned - int
More informationC Program Development and Debugging under Unix SEEM 3460
C Program Development and Debugging under Unix SEEM 3460 1 C Basic Elements SEEM 3460 2 C - Basic Types Type (32 bit) Smallest Value Largest Value short int -32,768(-2 15 ) 32,767(2 15-1) unsigned short
More informationQuiz 0 Review Session. October 13th, 2014
Quiz 0 Review Session October 13th, 2014 Topics (non-exhaustive) Binary. ASCII. Algorithms. Pseudocode. Source code. Compiler. Object code. Scratch. Statements. Boolean expressions. Conditions. Loops.
More informationExercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!
Exercise 3 / Ch.7 Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.8 Given the following array, write code
More informationFunctions. Introduction :
Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In
More informationQuestion 1. [15 marks]
Note to Students: This file contains sample solutions to the term test together with the marking scheme and comments for each question. Please read the solutions and the marking schemes and comments carefully.
More informationPointers. Pointer References
Pointers Pointers are variables whose values are the addresses of other variables Basic operations address of (reference) indirection (dereference) Suppose x and y are integers, p is a pointer to an integer:
More informationPointers, 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 informationFundamental 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 informationHacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019
Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of
More information