Tutorial #7 Summer 2005. sizeof int main() { char x; sizeof(x); sizeof(int); }

Slides:



Advertisements
Similar presentations
Part 1 Landscape Hannu Laine. Pointer parameters //prototype of swap void swap(int *a, int *b); //application void main(void) { in number1 = 1, number2.
Advertisements

Structures Spring 2013Programming and Data Structure1.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Arrays and pointers Lone Leth Thomsen. March 2006Basis-C-5/LL2 What is an array An array is a consecutive series of variables that share one variable.
Kernighan/Ritchie: Kelley/Pohl:
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Tutorial #6 Summer functions ( parameters list ) { body }
1 CSE1301 Computer Programming Lecture 16 Pointers.
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 CSE1301 Computer Programming Lecture 16 Pointers.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
1 Review of Chapter 6: The Fundamental Data Types.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Pointers CSE 2451 Rong Shi.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
CSC 172 DATA STRUCTURES.
CSI-121 Structured Programming Language Lecture 16 Pointers
Some examples.
Functions and Pointers
Introduction to Programming in C
Lecture Arrays.
C Programming Language
הרצאה 6: כתובות ומצביעים הקצאה דינמית מחרוזות
Lecture 10 Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
prepared by Senem Kumova Metin modified by İlker Korkmaz
Outline Defining and using Pointers Operations on pointers
Simulating Reference Parameters in C
CSE 303 Lecture 10 C memory model; stack allocation
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
컴퓨터 프로그래밍 기초 - 13th : 마지막 수업 -
Presentation transcript:

Tutorial #7 Summer 2005

sizeof int main() { char x; sizeof(x); sizeof(int); }

sizeof int main() { int a[10]; sizeof(a); }

Swap function void swap(int a, int b) { int tmp = a; a = b; b = tmp; }

pointers #include int main(void) { int x, *px; int y, *py ; px = &x; py = &y; scanf("%d%d", px, py); printf("x = %d, y = %d\n", *px, *py); return 0; }

pointers #include int main(void) { int *px; *px = 4; }

pointers #include int main(void) { int *px, x; px = &x; *px = 8; return 0; }

Swap function void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; }

Swap function void swap(int *a, int *b) { int *tmp = a; a = b; b = tmp; }

Swap function void swap(int *a, int *b) { int *tmp; *tmp = *a; *a = *b; *b = *tmp; }

simplify #include void swap(int*, int*); int gcd(int, int); void simplify(int*, int*); int main() { int a, b; if (scanf(“%d%d”, &a, &b) != 2) { printf(“Input error\n”); return 1; } printf(“Before simplification: ”%d / %d\n”, a, b); simplify(&a, &b); printf(“After simplification: “%d / %d\n”, a, b); return 0; }

simplify void simplify(int *num_p, int *denom_p) { int tmp = gcd(*num_p, *denom_p); *numerator_p /= tmp; *denominator_p /= tmp; } int gcd(int a, int b) { while(a != 0) { b %= a; swap(&a, &b); } return b; }

pointers #include int *p; void f() { int num = 25; p = &num; } int main(void) { f(); printf(“%d “, *p); return 0; }

pointers int main() { int *p; int x; … p = &x; p += 2; }

pointers int main() { char *p; char c; … p = &c; p += 2; }

Arrays and pointers p[k] = p + k  p + k * sizeof(type)

Arrays and pointers int a[10]; int  pa = a; /* int *pa = &a[0] */ pa = a; /* pa = &a[0] */ pa[i]   (pa + i)   (a + i)  a[i]

Arrays and pointers #include int main() { int a[10]; int  pa = a; *pa = &a[4]; pa++; a++; a = pa; return 0 ; }

Arrays and pointers #include #define N 100 int main() { int arr[N]; int *p = arr; int val;... *p++ = val; val = *--p; }

Pointer arithmetics #include int main(void) { int x = 1, y = 2, z[10]; int *ip, *iq; ip = &x; y = *ip; *ip = 0; ip = &z[0]; *ip = *ip + 10; y = *ip + 1; *ip += 1; ++*ip; (*ip)++; *ip++; iq = ip; ++*iq; }

sum_array #include #define N 10 int sum_array(int  a, int n); int main() { int array[N]; for(i = 0; i < N; ++i) scanf(  %d , &a[i]); printf(  The sum of array is : %d\n , sum_array(array, N)); return 0 ; }

sum_array int sum_array(int  a, int n) { int i, sum = 0; for(i = 0; i < n; ++i) sum += a[i]; return sum ; }

arrays for(j = 5; j < 8; ++j) printf(  %d , a[j]); printf(  \n  ); pa = &a[3]; for(j = 0; j < 3; ++j) printf(  %d , pa[j]); printf(  \n  ); for(j = 0; j < 3; ++j) printf(  %d ,  (pa + j)); printf(  \n  ); fun_c(&a[3]); return 0 ; } void fun_c(int a[]) { int j; for (j = 0; j < 3; ++j) printf(  %d , a[j]); } int main() { int a[10], j,  pa; for(j = 0; j < 10; ++j) a[j] = j + 1;