0% found this document useful (0 votes)
14 views19 pages

Lec 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Lec 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture # 9

Array of Strings
It can be declared as:

static char[MAX][LEN]

Where
MAX: is the array size,
LEN: is the maximum string length
Initialization of Array of
Strings
#define MAX 5
#define LEN 40
Static char list[MAX][LEN]={“String1”, “String2”, “String3”, “String4”,
“String5”};

In this example, we reserved 40 char for each


string and used only 8..
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
1. Return more than value from a function
2. Passing array to a function
3. 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 5‬‬
‫‪x‬‬
‫‪;int *px‬‬
‫‪px‬‬ ‫‪1500‬‬
‫‪;px = &x‬‬
‫‪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
x 7
x 5
px 1500
px 1500
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; The first value is 3 and second value is 7
*py = 7;
}
Example
#include<stdio.h>
ret2(int x,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)
int x,y;
{
x = 3;
y = 7;
} The first value is 0 and second value is 0
Thanks

You might also like