פוינטרים ומבנים. אתחול והדפסת מערך #include int * readArray(int size){ int *a, i; a= (int *) malloc (size* sizeof(int)); if (!a) return NULL; for (i=0;i<size;i++)

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Sort the given string, without using string handling functions.
C Intro.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Dynamic Memory Allocation
Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
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.
אתחול מערך void main() { int i; int ar1[5]={7,3,4,5,2}; int ar2[]={9,3,3,4,6};/*the size is 5*/ int ar3[5]={3,2};/*the other nums are 0*/ int ar4[3];/*garbage*/
רשימות. רשימה משורשרת typedef struct link{ int data; struct link * next; }link;
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
Multidimensional Arrays. Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score.
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
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);
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
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.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
 Last Lecture Reminder  Array Sorts  Exam Questions Examples  Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects  Additional.
 Structures typedef struct student{ char name[30]; long id; int grades[8]; } Student_type, *pStudent_type;
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
Linked List :: Basic Concepts
Linked List.
Linked list.
Array 9/8/2018.
מצביעים, הקצאה דינאמית ומבנים
Some examples.
הקצאות דינאמיות בשילוב מבנים
CSC 270 – Survey of Programming Languages
Introduction to Programming in C
הרצאה 6: כתובות ומצביעים הקצאה דינמית מחרוזות
Computer Programming תרגול 9 Summer 2016
Pointers and dynamic memory
מ- C++ ל- C קרן כליף.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
תכנות מכוון עצמים ו- C++ יחידה 01 מ- C ל- C++
Dynamic Memory Allocation
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
LINKED LIST.
Structures CSE 2031 Fall February 2019.
EECE.2160 ECE Application Programming
Programming Language C Language.
프로그래밍2 및 실습 Sort Code 전명중.
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Character Arrays char string1[] = “first”;
Structures EECS July 2019.
Presentation transcript:

פוינטרים ומבנים

אתחול והדפסת מערך #include int * readArray(int size){ int *a, i; a= (int *) malloc (size* sizeof(int)); if (!a) return NULL; for (i=0;i<size;i++) scanf("%d",a+i); return a; } void printArray (int * a, int size){ int i; for (i=0;i<size;i++) printf ("%d\n",*(a+i));/* a[i] */ }

שימוש ב main void main(){ int *arr, n; printf ("Enter size of array\n"); scanf("%d",&n); arr=readArray(n); if (arr==NULL) exit(0); printArray(arr,n); free(arr); }

מערך דו מימדי, האמנם? #include void main(){ char a2dim[4][3],i,j; strcpy(a2dim,"Hello world"); for (i=0;i<4;i++){ for (j=0;j<3;j++) printf("%c",a2dim[i][j]); printf("\n"); }

מערך דו מימדי באמצעות מצביעים int ** create2Dim(int rows, int cols){ int **a, i; if (!(a=(int **)(malloc (rows* sizeof (int *))))) return NULL; for (i=0;i<rows;i++) if (!(a[i]=(int *)(malloc (cols* sizeof (int))))) return NULL; return a; }

שחרור מערך דו מימדי דינאמי void free2Dim(int ** a, int rows){ int i; for (i=0;i<rows;i++) free(a[i]); free(a); }

מערך דו מימדי באמצעות מצביעים int ** create2Dim(int rows, int cols){ int **a, i; if (!(a=(int **)(malloc (rows* sizeof (int *))))) return NULL; for (i=0;i<rows;i++) if (!(a[i]=(int *)(malloc (cols* sizeof (int))))){ free2Dim(a,i); return NULL; } return a; }

מבנים

מבנה הסטודנט struct student{ char name[30]; long id; int grades[8]; };

יצירת סטודנט void main(){ struct student st1; strcpy (st1.name,"Shimon"); st1.id=1234; scanf ("%d",&st1.grades[0]); printf("name: %s, id: %d, grade: %d\n", st1.name,st1.id,st1.grades[0]); }

סוגי אתחול void main(){ struct student st1= {"Shimon",56689,{80,65,17}}; struct student st2; st2=st1; st1.name[2]='u'; st1.name[4]='a'; printf("%s\n",st1.name); printf("%s\n",st2.name); }

מבני הנקודה והמלבן typedef struct point{ double x; double y; }point; typedef struct rectangle{ struct point p1; struct point p2; }rectangle;

void main(){ point p, pt = {5.7,6.3}; rectangle rec1, rec2; p.x = 9; p.y = 17.1; rec1.p1 = pt; rec1.p2 = p; rec2.p1.x = 3.5; }

פונקציות עם מבנים point middle (point p1,point p2){ point p3; p3.x=(p1.x + p2.x)/2; p3.y=(p1.y + p2.y)/2; return p3; } void main(){ point p, pt = {5.7,6.3},pMid; p.x = 9; p.y = 17.1; pMid=middle(p,pt); }

מצביעים למבנים void main(){ struct student * p; p=(struct student *) malloc(sizeof(struct student)); if (!p) return; (*p).id=112233; p->id=112233; free(p); }

רשימה משורשרת typedef struct link{ int data; struct link * next; }link;

יצירת רשימה void main(){ link *head=NULL, *temp=NULL; int d; printf("Enter the list values:\n"); scanf ("%d",&d); while (d!=-1){ temp=(link *)malloc(sizeof(link)); if (!temp) break; temp->next=head; temp->data=d; head=temp; scanf ("%d",&d); } לא לאבד את הראש!!!

הדפסה ושחרור while(temp){ printf("%d ",temp->data); temp=temp->next; } while (head){ temp=head->next; free(head); head=temp; } לא לאבד את הראש!!!