1 / 19

Lecture # 9

Lecture # 9. Array of Strings . It can be declared as: Where MAX: is the array size, LEN: is the maximum string length. static char[MAX][LEN]. Initialization of Array of Strings. In this example, we reserved 40 char for each string and used only 8. #define MAX 5 #define LEN 40

akina
Download Presentation

Lecture # 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 9

  2. Array of Strings • It can be declared as: • Where • MAX: is the array size, • LEN: is the maximum string length static char[MAX][LEN]

  3. Initialization of Array of Strings • In this example, we reserved 40 char for each string and used only 8.. #define MAX 5 #define LEN 40 Static char list[MAX][LEN]={“String1”, “String2”, “String3”, “String4”, “String5”};

  4. String Functionsدوال السلاسل الحرفية • We will deal here with: • strlen function • strcomp function • strcpy function

  5. strlen Function • Return the length of string array • Input : Start of the string array • Output : the number of characters in the string array. • Example: • N = strlen(name);

  6. strcmp Function • Compare two strings and return 0 if they are identical else the value either bigger of smaller than Zero • Input : two string arrays • Output : Compare result as integer • 0 if they are identical • the value either bigger of smaller than Zero • Example: • N = strcomp(str1,str2);

  7. strcpy Function • Copy a string array in another string array • Input : two string array pointer • destination string array, • sourse string array • Examples: • strcpy(&str(n),&str(n+1)); • strcpy(str1,”Ahmed”);

  8. Example #include<stdio.h> #include <string.h> strde(char str[],int n); main() { char str[81]; int position; printf("Type String and position\n"); gets(str); scanf("%d",&position); strde(str,position); puts(str); } strde(str,n) char str[]; int n; { strcpy(&str[n],&str[n+1]); } Ex_40

  9. Chapter 7 Pointers المؤشرات

  10. Objective • What is the pointers • The function of the pointer • How to use Pointers

  11. What is the pointer? • A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. • Ex: int *pointer;

  12. The Function of the Pointers • We can use Pointers to • Return more than value from a function • Passing array to a function • Exchange data in the memory • “Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers.”

  13. Pointer constant and Pointer Variableالثابت المؤشر والمتغير المؤشر • يمكن تعريف الثابت المؤشر بأنه عنوان للذاكرة بينما يعرف المتغير المؤشر بانه مكان أو موقع لتخزين العناوين و لتعريف المتغير المؤشر يجب اضافة * بعد نوع المتغير الذي سيشير إليه كمثال float *pf • int x = 5; • int *px; • px = &x; 5 x 1500 px

  14. Indirect Operator مؤشر الوصول غير المباشر • إذا أضيفت علامة المؤشر لمتغير اثناء عملية التخصيص فهذا يعني انه مؤشرالوصول غير المباشر • مؤشرالوصول غير المباشر : يخصيص القيمة للمتغير التي يشير إلية المتغير • *x = 7; • This means that the x is a pointer and the value 7 will be assigned to the variable that the x point to

  15. Example • #include<stdio.h> • main() • { • int x; • int *px; • x = 5; • px = &x; • printf("the variable x address is %u, and the value in it is%d\n“ ,&x ,x); • printf("the variable x address is %u, and the value in it is%d\n", px ,*px); • *px = 7; • printf("the variable x address is %u, and the value in it is%d\n“ ,&x ,x); • printf("the variable x address is %u, and the value in it is%d\n", px ,*px); • } Ex_41.c

  16. Example *px = 7 7 x 5 x 1500 px 1500 px

  17. Example Ex_41 #include<stdio.h> ret2(int *px,int *py); main() { int x = 0; int y = 0; ret2(&x,&y); printf("The first value is %d and second value is %d",x,y); } ret2(px,py) int *px,*py; { *px = 3; *py = 7; } The first value is 3 and second value is 7

  18. Example #include<stdio.h> ret2(intx,int y); main() { int x = 0; int y = 0; ret2(x,y); printf("The first value is %d and second value is %d",x,y); } ret2(x,y) intx,y; { x = 3; y = 7; } The first value is 0 and second value is 0

  19. Thanks

More Related