Multidimension array, array of strings

Size: px
Start display at page:

Download "Multidimension array, array of strings"

Transcription

1 1 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) 1 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]; strcpy(message[0], hello ) message[0][2] = L ; H e l l o \0 H i \0 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) 2

2 2 Pointers K&R Ch 5 Basics: Declaration and assignment Pointer to Pointer Pointer and functions Last week 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 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 Last week

3 3 Some example of Pointers int *p1, *p2, x = 8, y = 9; p1 = &x; p2 = &y; x = y Precedence 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

4 4 ++ * ptr * ptr; * ptr = * ptr + 1 * ++ ptr ptr = ptr +1; *ptr; (* ptr) ++ * ptr; * ptr = * ptr + 1 * ptr ++ * ptr; ptr = ptr +1 7 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

5 5 Pointer to pointers another example int x = 12; int *ptr; ptr = &x; ptr_to_ptr 1006 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 x ptr, &ptr, *ptr, **ptr ptr-to-ptr, &ptr_to_ptr *ptr-to-ptr, 9 ** prt-to-ptr, Multiple indirection d * 1006 * * c b 1000 a Consider the following code: int a = 3; int *b = &a; int **c = &b; int ***d = &c; Here are how the values of these pointers equate to each other: *d == c; **d == *c == b; ***d == **c == *b == a == 3; 10

6 6 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) two questions 1. How to pass a structure such as array? 2. How to modify the argument? Tackle the swap() problem How to return structure, and/or more than one values Passing structure and modify by passing addresses/pointers Efficient. Input / output Possibly modify passed arguments strcpy (char[] dest, char[] source) 12 strcpy (char * dest, char * source)

7 7 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 call 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? 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: Firstly, we don t take your original manuscript (not call by reference). We always photocopy things (call by value), and work on copies. Secondly, 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 call 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!

8 8 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 Call by value px = &a; py = &b; void main( ) { int a=2, b=40; swap(&a, &b); 15 printf( %d %d, a, b); The Correct Version I am expecting int pointers void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; px = pa = &a; py = pb = &b Call by value px = pa = &a; py = pb = &b; void main( ) { int a=2, b=40; int *pa=&a; int *pb=&b; swap(pa,pb); printf( %d %d, a, b); 16 The other way to pass address

9 9 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) 17 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

10 10 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] 19 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; 20 pi + 1? address =100 psh + 2? address = 97. pi++? pc++? psh++? pi = pc = psh =

11 11 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 - 1 = 96 4 = 92 p p+1 p *p *(p+1) *(p+2) 21 How would we need to move pointer? 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

12 12 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 24

13 13 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 ptr e.g. if x is an integer ptr+1 array int x[3]; ptr arr[0] arr[1] arr[2] int arr[3]; int *ptr; ptr = &arr[0]; /* 92 */ ptr + 1?; /* 96 &arr[1] */ ptr + 2?; /* 100 ptr = &arr[2] */ *(ptr +2 ) =. /* *&arr[2] arr[2] */ ptr + i == & arr[i] *(ptr + i) == arr[i]

14 14 Sum of an int array #define N 10 int a[n], sum, i; sum = 0; for (i=0; i<n; i++) sum += a[i]; #define N 10 int a[n], sum, i,*p; p = &a[0]; sum = 0; for (i=0; i< N; i++) sum += *(p + i); #define N 10 int a[n], sum, i,*p; p = &a[0]; sum = 0; for (i=0; i< N; i++){ sum += *p; p++; 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

15 15 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 + 2) == *(&arr[2]) == arr[2] int arr[20]; int * p; ptr = arr; /* == (ptr = &arr[0]) */ ptr + 8 == & arr[8] *(ptr + 8) == arr[8] 29 Pass (copy) to a function (arr) -- the address of arr[0] 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 30 ptr[4] Pass (copy) to a function (arr) -- the address of arr[0]

16 16 Sum of an int array #define N 10 int a[n], sum, i; sum = 0; for (i=0; i<n; i++) sum += a[i]; #define N 10 int a[n], sum, i,*p; p = a; /* p=&a[0] */ sum = 0; for (i=0; i< N; i++) sum += *(p + i); 31 #define N 10 int a[n], sum, i,*p; p = a /* p=&a[0] */ sum = 0; for (i=0; i< N; i++) { sum += *p; p++; 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. 32 p = arr; /*valid*/ arr = p; /*invalid*/ p++; /*valid*/ arr++; /*invalid*/ arr = hi ; /*invalid*/ arr + 2 /* valid */

17 17 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 result is an new address/pointer 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 is false (0) otherwsie p1 p2 34

18 18 Pointer arithmetic on arrays (revisit) Adding an Integer to a Pointer Assume that the following declarations are in effect: int a[10], *p, *q, i; Example of pointer addition: p = &a[2]; q = p + 3; p += 6; Assume int array. p= &a[2] = 96 Then q = =108 = &a[5] 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]; q = p - 3; Assume int array. p= &a[8] = 120 Then q = =108 = &a[5] p -= 6;

19 19 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]; // i = p - q; /* i is 4 (108-92)/4 = 4 */ i = q - p; /* i is -4 */ 37 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. 38 p = &a[5]; q = &a[1]; p <= q is 0 false p >= q is 1. true

20 20 Sum of an int array #define N 10 int a[n], sum, i; sum = 0; for (i=0; i<n; i++) sum += a[i]; #define N 10 int a[n], sum, i,*p; p = a; /* p=&a[0] */ sum = 0; for (i=0; i< N; i++) sum += *(p + i); 39 #define N 10 int a[n], sum, i,*p; p = a; /* p=&a[0] */ sum = 0; for( i=0;i< N; i++) { sum += *p; p++; #define N 10 int a[n], sum, i,*p; p = a; /* p=&a[0] */ sum = 0; while( p < a+n ) { sum += *p; p++; 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) if (p1 == p2) assigning or comparing to zero (NULL) p = NULL p==null Illegal: p1+p2; p1*p2; p1*3 add two pointers, multiply or divide two pointers, integers shift or mask pointer variables p >> 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 40

21 21 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 New Pass sub_array today Arrays of pointers Command line argument Pointer to arrays and two dimensional arrays Pointer to functions Pointer to structures Memory allocation file IO Review Last lecture Arrays Passed to a Function Arrays passed to a function are passed by starting addresses. The name/identifier of the array passed is treated as a pointer to its first element. arr = &arr[0]; Thus, function expecting a char array can be declared as either strlen(char s[]); or strlen(char * s); Actual prototype man 3 strlen The call to this function does not copy the array in the function call, just a address (starting address -- a single value) to it. char a[20] = Hello ; char * ps = a; strlen(a); /* strlen(&a[0]) */ decay 42 strlen(ps); scanf( %s, a); scanf( %s, ps); Pass by value: s = a = &a[0] s = ps = a = &a[0] printf( %s, ps);

22 22 Arrays Passed to a Function Arrays passed to a function are passed by starting address. The name/identifier of the array passed is treated as a pointer to its first element. arr = &arr[0]; Thus, function expecting a char array can be declared as either strcpy(char A[], char B[]); or strcpy(char * pa, char * pb); Actual prototype man 3 The call to this function does not copy the array in the function call, just a pointer (starting address -- a single value) to it: char a[20]; char b[20] = hi ; char * ptra = a; char * ptrb = b; decay 43 strcpy(a, b) /* copy_array(&a[0], &b[0])*/ strcpy(ptra, ptrb); printf ( %s %s, ptra, b) Arrays Passed to a Function Arrays passed to a function are passed by starting address. The name/identifier of the array passed is treated as a pointer to its first element. arr = &arr[0]; By passing an array by a pointer 1. Array can be passed (efficiently) 2. Argument array can be modified decay 44

23 23 Computing String Lengths /* strlen: return length of string s */ int strlen(char *s) /* or (char s[]) */ { Examples using prior knowledge int n=0; while ( *(s+n)!= '\0') { n++; return n; Function does not know/care if it an array or not int strlen(char s[]) { int n=0; while ( s[n]!= '\0') { n++; return n; Callers: strcpy(arr, "hello, world"); /* string constant */ char * ptr = arr; strlen(arr); 45 /* char array; */ strlen(ptr); /* char *ptr; */ Computing String Lengths -- another version /* move the pointer s */ int strlen(char *s) /* or (char s[]) */ { Examples using prior knowledge int n =0; while (*s!= '\0 ) { s++; n++; return n; Function does not know/care if it an array or not int strlen(char s[]) { int n; for(n=0; s[n]!= '\0 ; n++) ; return n; Callers: strcpy(arr, "hello, world"); /* string constant */ char * p = arr; strlen(arr); /* char array; */ s=arr strlen(ptr); 46 /* char *ptr; */ s=prt=arr

24 24 Examples using prior knowledge Computing String Lengths -- cool version Don t need n /* strlen: return length of string s */ int strlen(char *s) /* or (char s[]) */ { char *p = s; while ( *p!= \0 ) p++; return p - s; // how far are they apart? Callers: strcpy(arr, "hello, world"); /* string constant */ char * p = arr; strlen(arr); /* char array[100]; */ strlen(ptr); 47 /* char *ptr; */ Examples using prior knowledge Computing String Lengths -- cool version Don t need n /* strlen: return length of string s */ int strlen(char *s) /* or (char s[]) */ { char *p = s; while ( *s!= \0 ) s++; return s - p; // how far are they apart? Callers: strcpy(arr, "hello, world"); /* string constant */ char * p = arr; strlen(arr); /* char array[100]; */ strlen(ptr); 48 /* char *ptr; */

25 25 copy two strings Function does not know/care if it an array or not /* strcpy: copy two strings */ void strcpy(char *dest, char *src) /* or (char s[]) */ { while (1){ *dest = *src; if (*src = \0 ) *(dest+i)=*(src+i) return; src ++; dest ++; void strcpy(char *dest, char *src) { while ( (*src = *dest)!= \0 ) { src++; dest++; 49 int main(){ char aa [] = "23544"; int siz = sizeof(aa) / sizeof(char) -1; printf("size: %u\n", siz); /* 6/1-1 = 5 */ a = my_atoi(aa); /* convert an array of digit characters into a decimal int */ int my_atoi (char c[]){ int u = sizeof(c) / sizeof(char) -1; /* 8/1-1=7 */ while ( u >= 0 ). 50

26 26 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 Passing Sub-arrays to Functions It is possible to pass part of an array to a function, by passing a pointer to the beginning of the sub-array. my_func( int ar[ ] ) {... or my_func( int *ar ) {... caller my_func(&a[5]) or my_func(a + 5) 52

27 27 Passing Sub-arrays to Functions It is possible to pass part of an array to a function, by passing a pointer to the beginning of the sub-array. char arr[20] = hi world ; char * pa = arr; strlen (&arr[4]); strlen (arr + 4); strlen (pa + 4); 4 &arr[4] arr + 4 arr pa h i w o r l d \o 53 pa subarray Passing Subarrays to Functions -- Recursion int length (String s) // Java if (s contains no letter) return 0; return 1 + length(substring(s,1)); length( ABCD ) = 1 + length( BCD ) = 1 + ( 1 + length( CD )) = 1 + ( 1 + ( 1 + length( D ))) = 1 + ( 1 + ( 1 + (1+ (1+length( ) ))) 54 = 1 + ( 1 + ( 1 + (1+ (1+0) ))) = 4

28 28 Passing Subarrays to Functions -- Recursion int length (String s) // Java if (s contains no letter) return 0; return 1 + length(substring(s,1)); length( ABCD ) = 1 + length( BCD ) = 1 + ( 1 + length( CD )) = 1 + ( 1 + ( 1 + length( D ))) = 1 + ( 1 + ( 1 + (1+ (1+length( )))) = 1 + ( 1 + ( 1 + (1+ (1+0) ))) = 4 55 Array Arguments (Summary) The fact that an array argument is treated as a pointer has some important consequences. decay Consequence 1: Due to pass by value, when an ordinary variable is passed to a function, its value is copied; any changes to the corresponding parameter don t affect the variable. In contrast, by passing array by pointer, argument array can be modified 56 strcpy (message, hello ); scanf ( %s, message);

29 29 Pointers and arrays (Summary) Consequence 2: The time required to pass an array to a function doesn t depend on the size of the array. There s no penalty for passing a large array, since no copy of the array is made. Consequence 3: An array parameter can be declared as a pointer if desired. strlen (char * s) 57 Consequence 4: A function with an array parameter can be passed an array slice substring strlen (&a[6]), strlen(a + 6) General array as function argument Pass an array / string by only the address / pointer of the first element strlen( Hello ); decay You need to take care of how long it is, the function does not know if it is an array or just a pointer to char/int Two possible approaches: 1. Special token at the end (case of string ) 2. Pass the length as another parameter Function: arraylen(int *) arraysum(int *) 58 Caller: int a[20]; arrlen(a); arraysum(a);

30 30 strlen(char *) arraylen(int *, int n) 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: Well, then you also need to tell us where to stop fetching. Either 1) tell us how many lockers to fetch, or, 2) put a special token in the last locker Ms function: Write down the locker number (starting 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: My manuscripts are in multiple (consecutive) lockers 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

31 31 Written midterm next week in class Midterm page on course website 61 K&R Ch.2,4 Types and sizes Basic types, their size and constant values (literals) char: x >= a && x <= z ; x >= 0 && x <= 9 x>48 && x<=57 int: 122, 0122, 0x12F convert between different bases not good Arrays (one dimension) and strings (Ch1.6,1.9) hello has size 6 byes, length 5 h e l l o \0 Expressions Basic operators (math, relational and logical) %, y=x++; y=++x; if (i =2) Type conversion and promotion 9/2* *9/2 int i= 3.4 Other operators (bitwise, bit shifting, compound assignment, conditional) Bit:, &, ~, ^, << >> Compound: x += 10; x >>= 10; x += y + 3 Precedence of operators Functions and Program Structure (Chapter 4)

32 32 K&R Ch 4 C program structure Functions call by value swap() Categories, scope and life time of variables (and functions) Global vs. local variable, Scope, life time, initialization static C Preprocessing #include, #define SQUARE (x) (x)*(x) FILE LINE 63 K&R Ch 4, Appendix B Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations o sscanf,sprintf, o fgets, fputs 64

33 33 K&R Ch 5 Pointers 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

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib. 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]

More information

Quick review pointer basics (KR ch )

Quick 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 information

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

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

More information

Arrays and Pointers (part 1)

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

More information

Arrays and Pointers (part 1)

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

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

More information

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

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

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 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 information

Dynamic memory allocation (malloc)

Dynamic 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 information

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C: 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 information

Arrays, Pointers and Memory Management

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

More information

CS 61c: Great Ideas in Computer Architecture

CS 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 information

MIDTERM 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, 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 information

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C: 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 information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. 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 information

EM108 Software Development for Engineers

EM108 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 information

First of all, it is a variable, just like other variables you studied

First 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 information

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 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 information

Programming for Electrical and Computer Engineers. Pointers and Arrays

Programming 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 information

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 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 information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. 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 information

Memory, Arrays & Pointers

Memory, 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 information

C-LANGUAGE CURRICULAM

C-LANGUAGE CURRICULAM C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer 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 information

Goals of this Lecture

Goals 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 information

Operators in C. Staff Incharge: S.Sasirekha

Operators 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 information

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

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

More information

CSE2301. Arrays. Arrays. Arrays and Pointers

CSE2301. 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 information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C 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 information

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS113: 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 information

Lecture 04 Introduction to pointers

Lecture 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 information

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

I2204 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 information

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

C: 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 information

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses Chapter 16 Pointers and Arrays Based on slides McGraw-Hill Additional material 200/2005 Lewis/Martin Pointers and Arrays We've seen examples of both of these in our LC- programs; now we'll see them in

More information

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

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

More information

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Sir 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 information

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Arrays 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 information

Pointers as Arguments

Pointers as Arguments Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)

More information

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M4.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 information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

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

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

More information

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 12 Pointers and Arrays 1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements. This leads to an alternative way of processing arrays in which pointers

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Array Initialization

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

More information

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down CS61C L3 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 More C intro, C Strings, Arrays, & Malloc Albert Chae Instructor 2008-06-25 Number Review Sign and magnitude

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

Continued from previous lecture

Continued 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 information

ECE 15B COMPUTER ORGANIZATION

ECE 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 information

COP 3223 Final Review

COP 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 information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 Introduction to C (pt 2) 2010-06-23! C help session: Tonight 7:00-9:00pm @ 306 Soda!!!Instructor Paul Pearce! The typical! development

More information

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB! CS61C L03 Introduction to C (pt 2) (1)! inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 Introduction to C (pt 2) 2010-06-23!!!Instructor Paul Pearce! The typical! development cycle!

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Pointers and File Handling

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

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how

More information

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)! Reference slides! You ARE responsible for the material on these slides (they re just taken from the reading anyway). These were the slides that generated the fewest questions in years past (i.e., those

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 C Strings, Arrays, & Malloc 2007-06-27 Scott Beamer, Instructor Sun announces new supercomputer: Sun Constellation CS61C L3 C Pointers

More information

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

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

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = abc;! How do you tell how long a string is?! CS61C L04 Introduction to C (pt 2) (1)! Reference slides! C Strings! You ARE responsible for the material on these slides (they re just taken from the reading anyway). These were the slides that generated

More information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review.  1 CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

More information

SYSC 2006 C Winter 2012

SYSC 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 information

Characters, Character Strings, and string-manipulation functions in C

Characters, Character Strings, and string-manipulation functions in C Characters, Character Strings, and string-manipulation functions in C see Kernighan & Ritchie Section 1.9, Appendix B3 Characters Printable characters (and some non-printable ones) are represented as 8-bit

More information

CS201- 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. 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 information

Lectures 5-6: Introduction to C

Lectures 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 information

Introduction to Scientific Computing and Problem Solving

Introduction 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 information

Fundamental of Programming (C)

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

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 4: Character strings & formatted I/O Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture To explain the input/output functions printf()

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

The Design of C: A Rational Reconstruction: Part 2

The 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 information

CSCI 171 Chapter Outlines

CSCI 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 information

... Lecture 12. Pointers

... 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 information

arrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review

arrays 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 information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut dthiebaut@smithedu Outline A Few Words about HW 8 Finish the Input Port Lab! Revisiting Homework

More information

Advanced C Programming Topics

Advanced C Programming Topics Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8

More information

[0569] p 0318 garbage

[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 information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

Operators and Expressions:

Operators 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 information

Introduction to C Language (M3-R )

Introduction to C Language (M3-R ) Introduction to C Language (M3-R4-01-18) 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in OMR answer sheet supplied with the question paper, following

More information

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.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 information

Arrays and Pointers. Lecture Plan.

Arrays 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 information

Week 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 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 information

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Lecture 3 COP 3014 Spring January 16, 2018 Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

More information

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

Lecture 2: C Programm

Lecture 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 information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 C Pointers 2004-09-08 Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Cal flies over Air Force We re ranked 13 th in the US and

More information

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSC209H Lecture 4. Dan Zingaro. January 28, 2015 CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a

More information

Basic and Practice in Programming Lab7

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

More information

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

Computer Organization & Systems Exam I Example Questions

Computer Organization & Systems Exam I Example Questions Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C

More information

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C C strings (Reek, Ch. 9) 1 Review of strings Sequence of zero or more characters, terminated by NUL (literally, the integer value 0) NUL terminates a string, but isn t part of it important for strlen()

More information