c programming day#2

26
With/Mohamed Fawzy C-Programming Language-2015 1 Day #2

Upload: mohamed-fawzy

Post on 14-Jul-2015

242 views

Category:

Engineering


0 download

TRANSCRIPT

With/Mohamed Fawzy

C-Programming Language-2015

1 Day #2

2

Lecture Notes:

Set your phone to vibration mode.

Ask any time.

During labs feel free to check any materials or internet.

Contents

3

Arrays.

Control statements.

Pointers.

Functions.

Dynamically memory allocation

4

Arrays.

which can store a fixed-size sequential collection of elements of the

same type.

All arrays consist of contiguous memory locations (Single Block).

The size of array, once declared, is fixed and cannot be modified.

Single Dimension Array.

Multi Dimension Array (Array of Arrays).

EX:

char a[5]; //Array of 5 characters.

EX:

char a[3][4];

/*Array of three arrays

and each array has 4 characters.*/

5

Examples:

double d[100]={1.5,2.7};

//first two elements initialized and remaining ones set to zero.

short num[]={1,2,3,4,5,6};

//compiler fixes size at 7 elements.

short num[]={1,2,3,4,5,6};

//compiler fixes size at 7 elements.

6

Take Care !!!!

#define size 10

int a[size];

char size=10;

int a[size];

const char size=10;

int a[size];

int a[5.3];

//size must be an integer

int a[5];

a[5]=50;

//in this case it will overwrite some data.

a[-1]=5;

//the index must be an integer.

7

Control Statements.

For Statements.

for (initial value;condition;update)

{

//statements

}

OR

initial value;

for(;condition;)

{

//statements

update;

}

8

Take Care !!!!

char x;

for(x=0;x<200;x++)

{

printf(“c programming”);

}

for(;;)

{

printf(“c programming”);

}

9

while loop.

while (condition)

{

//statements

}

do while loop.

do

{

//statements

}

while (condition)

Control Statements.

10

Break & Continue.

• Break and continue are used to modify the execution of loops.

break. When the break statement is encountered inside a loop, the loop is immediately

terminated and program control resumes at the next statement following the loop.

Continue. continue forces the next iteration of the loop to take place, skipping

any code in between.

11

Examples:

//program to print even numbers between(0:50)

int main()

{

char x=0;

while (x<=50)

{

if(x%2){

x++;

continue;

}

printf("%d\n",x++);

}

return 0;

}

12

Pointers.

Why pointers?

• Achieve call by reference with functions.

• Arrays and structures are difficult without pointers.

• Create linked list, trees and graph.

Note:

We must take care in using pointers since, there are no safety features.

13

Declaring pointers.

• Pointers are declared using “*”.

Int x; //declaring an integer

Int* x; //declaring pointer to an integer

char* m; //declare pointer to character

Notes:

• Pointers may only point to variables of the same type as the pointer

has been declared.

• A pointer to an int may only point to int.

• A pointer to a double may only point to double not float or long double.

• (&) stands for “address of….”

• (*) stands for “content of…”

14

Example:

15

Take Care !!!!

16

Pointers and arrays.

• The name of array is a pointer to the 0th place of array.

• You cannot apply increment or decrement on array name.

• Pointer is useful for passing array to function and safe stack memory.

EX#1: double *ptr;

double arr[10];

Ptr=arr; //ptr=&arr[0]

arr++; //not allowed

ptr++; //allowed

EX#2: void print_arr(char *ptr){

printf(“%d\n”,*(ptr++));

}

. . . . . . . . . . .

char arr[10];

Print_arr(arr);

Take Care !!!!

char *ptr[5]; //array of five pointers to char

char (*ptr)[5]; //pointer to array of 5 elements

17

EX#3: char (*ptr)[5];

char arr[5] {5,6,7,8,9};

Ptr=arr;

ptr++;

5

0

1

2

3

4

5

6

7

8

9

Garbage value

18

Functions.

• Functions are blocks of code that perform a number of pre-defined commands to

accomplish something productive. You can either use the built-in library functions

or you can create your own functions.

• Functions that a programmer writes will generally require a prototype.

• It tells the compiler what the function will return, what the function will be called,

as well as what arguments the function can be passed.

return-type <function-name> (arg_type arg1, arg_type arg2,…);

Exs.

void fun (void); //function which take nothing and return nothing

void fun (int x,int y,…); //function take arguments and return nothing

int fun (void); //function take nothing and return int

int fun (int x,int y,…); //function take arguments and return int

19

Ex#1. #include <stdio.h>

int mult ( int x, int y ); //prototype of function

int main()

{

int x,int y,int result;

printf( "Please input two numbers to be multiplied: " );

scanf( "%d", &x );

scanf( "%d", &y );

result= mult(x,y); //calling the function

printf( "The product of two numbers is %d\n",result);

return 0;

}

//implementation of function

int mult (int x, int y)

{

return x * y;

}

Functions. cont‟d

Pointer to function.

20

What happened in calling function?

Save some data in memory segment called stack.

• the value of PC (Program Counter).

• A copy of parameters passed to function.

• The value which returned from function.

Note:

If size of data stored on stack is larger than whole stack size it will cause

Common error called “Stack Overflow”.

Problem:

What if we need to pass a huge data to function to be processed.

Solution:

We can pass by reference because any pointer only occupy 4 bytes.

Functions. cont‟d

Calling function.

21

Write a c program to calculate the largest number in passed array. #include <studio.h>

char calc_largest (char *ptr,char siz)

{

char largest=*ptr;

char i=0;

for (i=0;i<siz;i++)

{

if (*(ptr+i) >largest)

largest=*(ptr+i);

else

continue;

}

return largest;

}

int main(){

char arr[]={45,12,5,44,6,8,60}

printf(“the largest value is %d”,calc_largest(arr,sizeof(arr));

}

Functions. cont‟d

22

Functions. cont‟d

Pointer to function.

• Pointer to function allow programmers to pass a function as a

parameter to another function.

• Function pointer syntax.

<return data_type> (* pointer_name)(arguments passed to function);

EX#1: void (*ptr)(int arg1,int arg2);

/*

Pointer to function which return nothing and take two

integers parameters.

*/

Note:

Don't be confused between pointer to function and pointer to array.

23

Functions. cont‟d

Initializing Pointer to function.

EX#2: void my_int_func(int x)

{

printf( "%d\n", x );

}

int main()

{

void (*ptr)(int);

/* the ampersand is actually optional */

ptr = &my_int_func;

(*ptr)( 2 ); //or ptr(2);

return 0;

}

24

Hands ON

Email: [email protected]

Phone: 01006032792

Facebook: [email protected]

Contact me:

25

26