Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.
|
|
- Coleen Watson
- 6 years ago
- Views:
Transcription
1 1 The previous lecture Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations Pointer basics 1 Common library functions [Appendix of K+R] <stdio.h> <string.h> <stdlib.h> <ctype.h> getc() putc() printf() scanf() sscanf() sprintf() gets() puts() fgets() fputs() 2 strlen(s) strcpy(s,s) strcat(s,s) strcmp(s,s) <math.h> sin() cos() exp() log() pow() sqrt() ceil() floor() double atof(s) int long void void void int <assert.h> assert() atoi(s) atol(s) rand() system() exit() abs(int) Included in C++ e.g., cstring.h cmath.h int islower(int) int isupper(int) int isdigit(int) int isxdigit(int) int isalpha(int) int tolower(int) int toupper(int) <signal.h>
2 2 Char arrays: set /get in general Some very import functions you might want to know char message[10]; To get from another string (literal) <string.h> strcpy(message, hello ) H e l l o \0 strcpy(message, OK );? O K \0 l o \0 3 strlen(message)? message[4]? Char arrays: set /get in general other ways of generating a string Some very import functions you might want to know char message[12]; int age = 12; float wage =2.34; Defined in standard library, prototype <stdio.h> sprintf(message, %s %d-%.1f, Sue,age,wage); S u e \0 sscanf(message, %s %d-%f, name, &age, &wage ); fgets(message, 10, stdin) fputs(message, stdout) tokenizing the string 4 FILE * stream
3 3 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There }; Array of strings H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string) 5 messages [0] Hello printf( %s, messages[0]); messages [1] Hi printf( %c, messages[2][1]); messages [2] There arrays: set /get in general char message[3][7]; H e l l o \0 H i \0 strcpy(message[0], hello ) 2 T h e r e \0 sprintf(message, %s there!, hi ); hi ther sprintf(message[1], %s %d %.0f, j, 1, 2.3); sscanf(message[1], %s %d %f, name, &age, &wage ) tokenizing the string fgets(message[2], 7, stdin) fputs(messge[2], stdout) 6
4 4 Now it is time to start POINTERS!!! 7 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions today Pointer arithmetic Pointers and arrays Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation Later: file IO
5 5 Motivations: Calling-by-Value In C, all functions are called by value Value of the arguments are passed to functions, but not the arguments themselves (i.e., not call by reference ) 9 int swap (int x, int y) { int tmp; tmp = x; x = y; y = tmp; } main(){ int i=3, j=4; swap(i,j) } running main() int i = 3 int j = 4 int k int x = i = 3 4 int y = j = 4 3 int tmp 3 running swap() char [] fromstr = Hello! char [20] tostr; strcpy(tostr, fromstr); fgets(tostr, 10, stdin); Given an array as an argument, a function can modify the contents of the array -- Arrays are passed as if call-byreference also scanf ( %d %s, &a, arr); But isn t C call-by-value? -- pass single numerical value 1) How to pass strings to strcpy()? 2) How does strcpy(),scanf(),fgets() modify argument? 10
6 6 Declare and initialize pointer int *ptr; /* declare a pointer to int */ Create a special variable that stores the address of other variable 4 bytes, why? ptr ptr = &x /*assigning address of rate*/ Store address of x in ptr (ptr s value is the address) ptr now points to x x ptr x int *ptr; /* I m a pointer to an int */ 91 ptr 96 7 x ptr= &x; /*I got the address of rate */ ptr x mnemonic: expression *prt is an int *ptr; /* dereferencing. Indirect assess. Get contents of the pointee*/ ptr &x --- address of x * ptr x --- content (value) of x 12 printf( %d, x ); 7 direct access printf( %d, *ptr); 7 indirect access
7 7 Some example of Pointers int *p1, *p2; int x = 8, y = 9; p1 = &x; p2 = &y; *p1 = *p2; x = y x y Some example of Pointers int *p1, *p2; int x = 8, y = 9; p1 = &x; p2 = &y; p1 = p2; /*copy the content of p2 (address of y) into p1 */ x y
8 8 Some example of Pointers int *p1, *p2, x = 8, y = 9; p1 = &x; p2 = &y; x = y Precedence and Associativity p53 Operator Type Operator Associativity Primary Expression Operators Unary Operators Binary Operators () []. -> left-to-right * & + -! ~ (typecast) sizeof * / % arithmetic + - arithmetic >> << bitwise < > <= >= relational ==!= relational & ^ bitwise bitwise bitwise && logical logical right-to-left left-to-right Ternary Operator?: right-to-left Assignment Operators = += -= *= /= %= >>= <<= &= ^= = right-to-left Comma, left-to-right ptr = &x; *x = 5; No () needed
9 9 ++ * ptr * ptr; * ptr = * ptr + 1 * ++ ptr ptr = ptr +1; *ptr; (* ptr) ++ * ptr; * ptr = * ptr + 1 * ptr ++ * ptr; ptr = ptr Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions Pointer arithmetic Pointers and arrays Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation Later: file IO
10 10 Pointer to pointers int x = 7; int * ptr = &i; ptr_ptr ptr x int ** ptr_ptr = &ptr; /* ptr_ptr = 91 */ ** ptr_ptr = 20; /* set x = 20 */ Pointer to pointers another example int x = 12; int *ptr; ptr = &x; ptr x *ptr = 300; 20
11 11 Pointer to pointers another example int x = 12; int *ptr; ptr = &x; ptr int **ptr-to-ptr /* I am a pointer to pointer */ ptr-to-ptr = &ptr; /* points to ptr */ **ptr-to-ptr = 20; /* multiple indirection*/ valid operations x, &x *x ptr_to_ptr x ptr, &ptr, *ptr, **ptr ptr-to-ptr, &ptr_to_ptr *ptr-to-ptr, 21 ** prt-to-ptr, More Examples int x = 1, y = 2; int *ip, *ip2; ip = &x; int **pip /* I am a pointer to pointer */ pip = &ip; /* pip points to ip */ y = **pip; /* y=x y is now 1 */ ip2 = ip; *ip2 += 10; /* x=x+10 x is 11 */ ip = &y; (** pip)--; /* y = y-1 y = 0 */ ip2 = pip;? x? 22 y? 11 0
12 12 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions Pointer arithmetic Pointers and arrays Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation Later: file IO Pointers and function arguments In C, all functions are called by value Value of the arguments are passed to functions, but not the arguments themselves (i.e., not call by reference) How to pass a structure such as array? How to modify the argument? swap() How to return structure, and/or more than one values Passing structure and modify by passing pointer Efficient. Input / output Possibly modify passed arguments strcpy (char[] dest, char[] source) 24 strcpy (char * dest, char * source)
13 13 Mr. Main: Hi, Binding function, I have some manuscripts, stored in lockers (memory), and I want you to bind them into a book. Could I bring the manuscripts to you? main Ms function: Hi, Mr Main, here is how we work: First, we don t take your original manuscript (not pass by reference). We always photocopy things (call by value), and work on copies. Second, we only photocopy one paper a time(a single value) Mr. Main: Then, is there a way to have my original manuscripts bound by you? Calling by Value In C, all functions are called by value Value of the arguments are passed to functions, but not the arguments themselves (i.e., not call by reference) 26 int swap (int x, int y) { int tmp; tmp = x; x = y; y = tmp; } main(){ int a=3, b=4; swap(a,b); } running main() int a =3 int b = 4 int x = a= 3 int y = b=4 int tmp call swap() running swap()
14 14 Mr. Main: Hi, Mrs binding function, I have some manuscripts, stored in lockers (memory), and I need you to bind them into a book. Could I bring the manuscripts to you? main Ms function: Hi, Mr Main, here is how we work: First, we don t take your original manuscript (not pass by reference). We always photocopy things (call by value), and work on copies. Second, we only photocopy one paper a time(a single value) Mr. Main: Then, is there a way to have my original papers bound by you? Ms function: Write down the locker number (address) on a paper, bring that paper to us (pass pointer/address) we photocopy the paper (still pass by value), Then, based on the locker number on the copy, we go to your locker, fetch your original manuscripts there and bind them! Mr. Main: Cool! The Correct Version I am expecting int pointers void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } px = &a; py = &b px = &a; py = &b; void main( ) { int a=2, b=40; swap(&a, &b); } 28 printf( %d %d, a, b);
15 15 The Correct Version I am expecting int pointers void swap(int *px, int *py) { } int temp; temp = *px; *px = *py; *py = temp; void main( ) { int a=2, b=40; int *pa=&a; int *pb=&b; swap(pa,pb); printf( %d %d, a, b); } 29 px = pa = &a; py = pb = &b The other way to pass address px = pa = &a; py = pb = &b; Another example void increment(int *px2, int *py2) { (*px2) ++;? (*py2) += 30; } void swapincre(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; increment(?,?); } increment(px, py) void main( ) { int a=2, b=40; /* Input a and b */ swapincre(&a, &b); printf( %d %d, a, b); }30
16 16 Now understand scanf() -- more or less int x=1; int y = 2; swap(&x,&y); int x; scanf ( %d, &x); printf( %d,x); int x; int *px = &x; scanf( %d, px); printf( %d,*px); But why array name is used directly scanf ( %s, arrname) scanf ( %d %s, &x, arrname) 31 scanf( %d, &px);? // store input value into px, not x Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions Pointer arithmetic Pointers and arrays Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation Later: file IO
17 17 Pointers and variable type base type is important! int x = 1, y = 2; int *ip; ip = &x; /* ip points to x */ y = *ip; /* y is now 1 */ oo c sh i Each pointer stores the address of the first byte of its pointee How many bytes to transfer? -- Base type is important! 33 int i; char c; short sh; int* pi; char *pc; short *psh; Pointers and variable type base type is important! oo c sh i int* pi = &i; char *pc=&c; short *psh=&sh; Each point store the address of the first byte of its pointee How many bytes to transfer? Base type is important! Allowing proper read/write. y = *pi; *pi = 100; r/w 4 bytes from 96 [96,97,98,99] c = *pc; *pc= d ; r/w 1 byte from 91 s=*psh; *psh=2; r/w 2 bytes from 93 [93, 94] 34
18 18 Pointer arithmetic pi pi oo c sh i Limited math on a pointer Four arithmetic operators that can be applied Result is a pointer (address) int* pi=&i; char * pc=&c; short * psh=&sh; 35 pi + 1? address =100 psh + 2? address = 97. pi ++? pc++? psh++? pi = pc = psh = Pointer arithmetic Incrementing / decrementing a pointer by n moves it ahead n units p ± n unit Size of a unit is based upon the size of the type If p points to an integer of 4 bytes, p + n advances by 4*n bytes: p + 2 = *2 = 104 p p+1 p *p *(p+1) *(p+2) 36 How would we need to move pointer?
19 19 arithmetic2017.c Arithmic.c int * pint; short * pshort; char * pchar; double * pdouble; 37 +1n +2n +4n +8n Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions Pointer arithmetic Pointers and arrays (5.3) Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation Later: file IO
20 20 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions (pass pointer by value) Pointer arithmetic Pointers and arrays (5.3) How arrays are stored consecutively Pointer to array elements p + i = &a[i] *(p+i) = a[i] Array name contains address of 1 st element a = &a[0] Pointer arithmetic on array (extension) Array as function argument decay Pass sub_array Later: file IO Pointers and Arrays (5.3) Array members are next to each other in memory arr[0] occupies in the lowest address e.g. int arr[3]; arr[0] arr[1] arr[2] short 40
21 21 Array members are next to each other in memory arr[0] occupies in the lowest address arr[0] arr[1] arr[2] arrayelementsize2017.c 41 Array members are next to each other in memory arr[0] occupies in the lowest address arr[0] arr[1] arr[2]
22 22 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions (pass pointer by value) Pointer arithmetic Pointers and arrays (5.3) Stored consecutively Pointer to array elements p + i = &a[i] *(p+i) = a[i] Array name contains address of 1 st element a = &a[0] Pointer arithmetic on array (extension) Array as function argument decay Pass sub_array Later: file IO Pointers and Arrays (5.3) Array members are next to each other in memory arr [0] occupies in the lowest address e.g. if x is an integer array int x[3]; arr[0] arr[1] arr[2] int arr[3]; int *ptr; ptr = &x[0]; ptr + 1?; /* &arr[1] */ ptr = ptr + 2; /* ptr = &arr[2] */ *(ptr +2 ) =. /* *&arr[2] access arr[2] */ ptr + i == & arr[i] *(ptr + i) == arr[i]
23 23 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions (pass pointer by value) Pointer arithmetic Pointers and arrays (5.3) Stored consecutively Pointer to array elements p + i = &a[i] *(p+i) = a[i] Array name contains address of 1 st element a = &a[0] Pointer arithmetic on array (extension) Array as function argument decay Pass sub_array Later: file IO Pointers and Arrays (5.3) There is special relationship between pointers and arrays when you use array, you are using pointers! int i, arr[20], char c; scanf( %d %c %s, &i, &c, arr); /* &arr is wrong */ Identifier (name) of an array is equivalent to the address of its 1 st element. arr == &arr[0] arr + 1 == address of next element == &arr[1] *(arr + 20) == *(&arr[20]) == arr[20] int arr[20]; int * p; ptr = arr; /* == (ptr = &arr[0]) */ ptr + 8 == & arr[8] *(ptr + 8) == arr[8] 46 Pass (copy) to a function (arr) -- the address of arr[0]
24 24 Pointers and Arrays (5.3) There is special relationship between pointers and arrays Identifier (name) of an array is equivalent to the address of its 1 st element. arr == &arr[0] int arr[20]; int * p; ptr = arr; /* ptr = &arr[0] */ arr[4] *(ptr + 4) *(arr + 4) equivalent 47 ptr[4]; Pass (copy) to a function (arr) -- the address of arr[0] arrayaddressppp.c /* Demonstrates use of pointer arithmetic in array*/ main() { int arr[10] = {0,10,20,30,40,50,60,70,80,90}, x; int *ptr = arr; /* = &arr[0] */ printf( %p %p, arr, prt); /* Print the addresses of each array element. */ for (x = 0; x < 10; x++) printf("%p %p %p", &arr[x], arr+x, ptr+x); /* Print the content of each array element. */ for (x = 0; x < 10; x++) printf( %d: %d %d %d", arr[x], *(arr+x), *(ptr+x), ptr[x]); } 48 return 0;
25 25 arr == &arr[0] Another way ++ /* Demonstrates use of pointer arithmetic in array*/ main() { int arr[10] = {0,10,20,30,40,50,60,70,80,90}, x; int *ptr = arr; /* = &arr[0] */ /* Print the addresses of each array element. */ for (x = 0; x < 10; x++) printf("%p %p %p", &arr[x], arr+x, ptr++); /* Print the content of each array element. */ for (x = 0; x < 10; x++) printf("%d %d %d", arr[x], *(arr+x), *(ptr++)); 50 } return 0; arr++???
26 26 Array name is not pointer! int arr[20]; int * p = arr; p and arr are equivalent and they have the same properties: &arr[0] Difference is that arr is a string constant we could assign another value to the pointer p arr will always point to the first of the 20 integer numbers of type int. p = arr; /*valid*/ arr = p; /*invalid*/ p++; /*valid*/ arr++; /*invalid*/ 51 char arr[10] = hello ; int i; char * p; p = arr; strlen(arr); /*valid*/ sizeof (arr)? strlen(p); /*valid*/ sizeof (p)? arr = NULL; /*invalid*/ arr = &i; /*invalid*/ arr = arr +1; /*invalid*/ arr ++; /*invalid*/ *(arr + 1)=5 /*valid*/ p = NULL; p++; p = &i; 52 /*valid*/ /*valid*/ /*valid. now point to others*/
27 27 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions (pass pointer by value) Pointer arithmetic Pointers and arrays (5.3) Stored consecutively Pointer to array elements p + i = &a[i] *(p+i) = a[i] Array name contains address of 1 st element a = &a[0] Pointer arithmetic on array (extension) Array as function argument decay Pass sub_array Later: file IO Pointer arithmetic (revisit) +n -n If p1, p2 points to different elements of the same array Differencing: p1 p2 how far apart the elements are Comparison : ==!= > < >= <= p1 < p2 is true (1) if p1 points to earlier elements than p2 p1 p2 54
28 28 Pointer arithmetic on arrays (revisit) Adding an Integer to a Pointer Adding an integer j to a pointer p yields a pointer to the element j places after the one that p points to. More precisely, if p points to the array element a[i], then p + j points to a[i+j]. Assume that the following declarations are in effect: int a[10], *p, *q, i; Pointer arithmetic on arrays (revisit) Adding an Integer to a Pointer 7 Example of pointer addition: p = &a[2]; q = p + 3; p += 6; Assume int array. Assume p= &a[2] = 96 Then q = =108 = &a[5] 56
29 29 Pointer arithmetic on arrays (revisit) Subtracting an Integer from a Pointer If p points to a[i], then p - j points to a[i-j]. Example: p = &a[8]; 7 q = p - 3; p -= 6; Assume int array. assume p= &a[8] = 120 Then q = =108 = &a[5] 57 Pointer arithmetic on arrays (revisit) Subtracting One Pointer from Another When one pointer is subtracted from another, the result is the distance (measured in array elements) between the pointers. If p points to a[i] and q points to a[j], 7 then p - q is equal to i - j. p = &a[5]; // 108 q = &a[1]; // 92 i = p - q; /* i is 4 (108-92)/4 = 4 */ i = q - p; /* i is -4 */ 58
30 30 Pointer arithmetic on arrays (revisit) Comparing Pointers Pointers can be compared using the relational operators (<, <=, >, >=) and the equality operators (== and!=). Using relational operators is meaningful only for pointers to elements of the same array. The outcome of the comparison depends on the relative positions of the two elements in the array. 59 p = &a[5]; q = &a[1]; p <= q is 0 false p >= q is 1. true Summary of pointer arithmetic Legal: assignment of pointers of the same type p2 = p1 adding or subtracting a pointer with an integer p++, p+2, p-2 subtracting or comparing two pointers to members of the same array p2- p1, if (p1 < p2) assigning or comparing to zero (NULL) p = NULL p==null Illegal: add two pointers, multiply or divide two pointers, integers shift or mask pointer variables p1+p2; p1*p2; p1*3 add float or double to pointers p assign a pointer of one type to a pointer of another type (except for void *) without a cast 60
31 31 Pointers and Arrays: an Example int a[10]={3,4,5,6}; int *pa; pa = a; /* pa=&a[0] */ int x,y,z; x = *pa; /*same as x = a[0]*/ y = *(pa + 1); //a[1] z = *(pa + 2); //a[2] pa++; x = *(pa+2)? *(pa + 3) = 200; x:6 y:4 z:5 a: Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions (pass pointer by value) Pointer arithmetic Pointers and arrays (5.3) Stored consecutively Pointer to array elements p + i = &a[i] *(p+i) = a[i] Array name contains address of 1 st element a = &a[0] Pointer arithmetic on array (extension) p1-p2 p1<>!= p2 Array as function argument decay Pass sub_array Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation file IO today
Multidimension array, array of strings
1 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There ; Array of strings 0 1 2 0 1 2 3 4 5 6 H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string)
More informationPointers (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 informationQuick review pointer basics (KR ch )
1 Plan for today Quick review pointer basics (KR ch5.1 5.5) Related questions in midterm Continue on pointers (KR 5.6 -- ) Array of pointers Command line arguments Dynamic memory allocation (malloc) 1
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationArrays 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 informationMIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes
MIDTERM TEST EESC 2031 Software Tools June 13, 2017 Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes This is a closed-book test. No books and notes are allowed. Extra space for
More informationArrays 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 informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationC: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36
Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar
More informationArrays 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 informationArrays 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 informationLanguage 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 informationIntroduction to string
1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string
More informationSYSC 2006 C Winter 2012
SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization
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 informationArrays, 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 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 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 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 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 informationM4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
M4.1-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 informationupper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.
The char Type The C type char stores small integers. It is 8 bits (almost always). char guaranteed able to represent integers 0.. +127. char mostly used to store ASCII character codes. Don t use char for
More informationPDS 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 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 informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationArrays and Pointers. Lecture Plan.
. Lecture Plan. Intro into arrays. definition and syntax declaration & initialization major advantages multidimensional arrays examples Intro into pointers. address and indirection operators definition
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 informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationOperators in C. Staff Incharge: S.Sasirekha
Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C
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 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 informationC Characters and Strings
CS 2060 Character handling The C Standard Library provides many functions for testing characters in ctype.h. int isdigit(int c); // is c a digit (0-9)? int isalpha(int c); // is c a letter? int isalnum(int
More informationCS 61c: Great Ideas in Computer Architecture
Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address
More informationLecture 8: Pointer Arithmetic (review) Endianness Functions and pointers
CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment
More informationC Programming Multiple. Choice
C Programming Multiple Choice Questions 1.) Developer of C language is. a.) Dennis Richie c.) Bill Gates b.) Ken Thompson d.) Peter Norton 2.) C language developed in. a.) 1970 c.) 1976 b.) 1972 d.) 1980
More informationStrings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts
Strings in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch,
More informationPointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in
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 informationContents. A Review of C language. Visual C Visual C++ 6.0
A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source
More informationcs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).
cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion
More informationTHE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming
THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY ISA 563: Fundamentals of Systems Programming Announcements Homework 2 posted Homework 1 due in two weeks Typo on HW1 (definition of Fib. Sequence incorrect)
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 informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
More informationMultiple Choice Questions ( 1 mark)
Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s
More informationMain Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University
C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types
More informationProgramming for Electrical and Computer Engineers. Pointers and Arrays
Programming for Electrical and Computer Engineers Pointers and Arrays Dr. D. J. Jackson Lecture 12-1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements.
More informationPointers. Pointers. Pointers (cont) CS 217
Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationFloating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions
Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point
More informationSystems Programming and Computer Architecture ( )
Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1 4: Pointers Computer Architecture and Systems Programming
More informationA pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.
Lecture 9 Pointers A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. This variable may be of type int, char,
More informationCS 241 Data Organization Pointers and Arrays
CS 241 Data Organization Pointers and Arrays Brooke Chenoweth University of New Mexico Fall 2017 Read Kernighan & Richie 6 Structures Pointers A pointer is a variable that contains the address of another
More informationFundamentals of Programming Session 20
Fundamentals of Programming Session 20 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines
More informationChapter 7 C Pointers
Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character
More informationHigh 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 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 informationC Programs: Simple Statements and Expressions
.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. C Programs: Simple Statements and Expressions C Program Structure A C program that consists of only one function has the following
More informationFunction I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects
Function Input and Output Input through actual parameters Output through return value Only one value can be returned Input/Output through side effects printf scanf 2 tj Function Input and Output int main(void){
More informationI BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.
1 of 22 8/4/2018, 4:03 PM Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Reaccredited at the 'A' Grade Level by the NAAC and ISO 9001:2008
More informationOperators and Expressions:
Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence
More informationPointers 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 informationCSC 2400: Computer Systems. Arrays and Strings in C
CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays: C vs.
More information... Lecture 12. Pointers
Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-1 Lecture 12. Pointers Variables denote locations in memory that can hold values; arrays denote contiguous locations int i = 8, sum = -456;
More informationAryan 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 informationFunction I/O. Last Updated 10/30/18
Last Updated 10/30/18 Program Structure Includes Function Declarations void main(void){ foo = fun1(a, b); fun2(2, c); if(fun1(c, d)) { } } Function 1 Definition Function 2 Definition 2 tj Function Input
More informationContinued from previous lecture
The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations
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 informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
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 Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2
CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More informationBasic 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 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 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 informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationCSE2301. Arrays. Arrays. Arrays and Pointers
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 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 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 informationC Review. SWE2004: Principles in Programming Spring 2014 Euiseong Seo
C Review 1 C Program Structure #include int main ( ) { variable declarations; } statements; scanf ( %d, &var); printf (..\n ); return 0; Compilation of a program: gcc o hello hello.c -o specifies
More informationIntroduction to C Language
Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,
More informationMYcsvtu Notes LECTURE 34. POINTERS
LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationWeek 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington
Week 5 9 April 2018 NWEN 241 More on pointers Alvin Valera School of Engineering and Computer Science Victoria University of Wellington Admin stuf Exercises (Set 3): Covers Weeks 4-5 lessons Download from
More informationStrings. Steven R. Bagley
Strings Steven R. Bagley Recap Programs are a series of statements Defined in functions Functions, loops and conditionals can alter program flow Data stored in variables or arrays Or pointed at by pointers
More informationCS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their
More informationINTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from
INTRODUCTION TO C++ FUNCTIONS Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Functions: Program modules in C Function Definitions Function
More informationCOP 3223 Final Review
COP 3223 Final Review Jennifer Brown December 2, 2018 1 Introduction 1.1 Variables I. How can we store data in a program? A. Which of these are valid names for variables? i. 9length ii. hello iii. IamASuperCoolName
More informationFundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University
Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler
More informationJagannath 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 informationThe Design of C: A Rational Reconstruction: Part 2
The Design of C: A Rational Reconstruction: Part 2 1 Continued from previous lecture 2 Agenda Data Types Operators Statements I/O Facilities 3 Operators Issue: What kinds of operators should C have? Thought
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 informationC: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this
More informationComputers Programming Course 6. Iulian Năstac
Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap
More informationComputer Programming: Skills & Concepts (CP) Strings
CP 14 slide 1 Tuesday 31 October 2017 Computer Programming: Skills & Concepts (CP) Strings Ajitha Rajan Tuesday 31 October 2017 Last lecture Input handling char CP 14 slide 2 Tuesday 31 October 2017 Today
More informationChapter 8: Character & String. In this chapter, you ll learn about;
Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering
More informationThe 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 informationPointers. Introduction
Pointers Spring Semester 2007 Programming and Data Structure 1 Introduction A pointer is a variable that represents the location (rather than the value) of a data item. They have a number of useful applications.
More information