190 likes | 316 Views
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
E N D
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 Static char list[MAX][LEN]={“String1”, “String2”, “String3”, “String4”, “String5”};
String Functionsدوال السلاسل الحرفية • We will deal here with: • strlen function • strcomp function • strcpy function
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);
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);
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”);
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
Chapter 7 Pointers المؤشرات
Objective • What is the pointers • The function of the pointer • How to use Pointers
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;
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.”
Pointer constant and Pointer Variableالثابت المؤشر والمتغير المؤشر • يمكن تعريف الثابت المؤشر بأنه عنوان للذاكرة بينما يعرف المتغير المؤشر بانه مكان أو موقع لتخزين العناوين و لتعريف المتغير المؤشر يجب اضافة * بعد نوع المتغير الذي سيشير إليه كمثال float *pf • int x = 5; • int *px; • px = &x; 5 x 1500 px
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
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
Example *px = 7 7 x 5 x 1500 px 1500 px
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
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