Вы находитесь на странице: 1из 30

REVIEW QUIZ

# 1 Consider a float variable, floatvar, stored at memory location 2205 and having the value 1408.65. Also assume that a pointer variable, ptr currently stores the address 2205. Fill in the blanks : a. ___________ or floatvar equals ________. b. ___________ or ptr equals _________. # 2 State whether true or false. A pointer variable, when declared, should be pointing to a specific data type. # 3 State whether true or false. It is best to initialize a pointer variable as soon as it is declared.
NIIT SEM Q/CPR/CR/SESSION 4/1/VER06/95

REVIEW QUIZ (Contd.)


# 4 Is the following declaration valid ? Char *great_runners [ ] = { Emil Zatopek, Paavo Nurmi, Valery Borzov, Carl Lewis };

NIIT

SEM Q/CPR/CR/SESSION 4/2/VER06/95

REVIEW QUIZ (Contd.)


# 5 In the set of statements below: int x y, *ptr1, *ptr2; x = 420;

y = 1408;
ptr1 = &x; ptr2 = &y; *ptr2 = 806; *ptr2 = *ptr1; ptr2 = ptr1; *ptr2 = 522;
NIIT

/* statement A */ /* statement B */

/* statement C */
SEM Q/CPR/CR/SESSION 4/3/VER06/95

REVIEW QUIZ (Contd.)


What will be the values of x and y after: a. statement A is executed? b. statement B is executed? c. statement C is executed?

# 6. State whether true or false.


In the declaration: char str [10], *ptr; both str and ptr are pointers.

NIIT

SEM Q/CPR/CR/SESSION 4/4/VER06/95

REVIEW QUIZ (Contd.)


# 7 State whether true or false. In the declaration in # 6. a. str is a pointer which can point to any portion of the memory. b. ptr is a pointer which can point to any portion of the memory. # 8 Consider a pointer ptr pointing to an int and currently containing the address 1009. When the statement: ptr+ + ; is executed, ptr is pointing to a value stored in memory location _____.

NIIT

SEM Q/CPR/CR/SESSION 4/5/VER06/95

REVIEW QUIZ (Contd.)


# 9 What is the output of the program below? # include < stdio.h> int one_d [ ] = { 1, 2, 3 }; main () { int counter, *ptr; for (counter = 1, ptr = one_d; counter < 5; counter+ +, ptr+ + )

{
printf ( \nptr is now pointing to %d, *ptr); } }
NIIT SEM Q/CPR/CR/SESSION 4/6/VER06/95

SOLUTIONS TO REVIEW QUIZ


# 1 a. *ptr or floatvar equals 1408.65 b. &floatvar or ptr equals 2205 # 2 True # 3 True. Remember the dangers of un-initialized pointers. # 4 Yes # 5 a. x = 420 & y = 806 b. x = 420 & y = 420 c. x = 522 & y = 420 # 6 True # 7 a. False. str is a pointer with a constant address.

b. True
NIIT SEM Q/CPR/CR/SESSION 4/7/VER06/95

SOLUTIONS TO REVIEW QUIZ (Contd.)


# 8 1013. Remember that the size of an int is 4 bytes. # 9 The output is as below: ptr is now pointing to 1 ptr is now pointing to 2 ptr is now pointing to 3 ptr is now pointing to (some junk value) Note that when the fourth value is getting printed, ptr is pointing to some junk value in memory.

NIIT

SEM Q/CPR/CR/SESSION 4/8/VER06/95

SPL SESSION
Objectives At the end of this session, you will be able to : Use pointers in C programs in terms of : Declaring pointers Initializing pointers Manipulating values of data items using pointers Printing values of data items using pointers Use pointers for declaring and manipulating onedimensional and two-dimensional character arrays State the differences between arrays and pointers Use pointer arithmetic in C programs Write string-handling functions using pointers
NIIT SEM Q/CPR/CR/SESSION 4/9/VER06/95

POINTERS
Variables which contain the addresses of other variables (of any data type) in memory

Are declared in same way as other variables are declared


int *ptr; /* ptr is pointing to an int */

Initialization
A pointer can potentially point to any portion of memory. Thus, initialization of pointers is a must

int x, &ptr;
ptr = &x;

/* an int and a pointer */


/* ptr pointing to x */

& returns address of variable x


NIIT SEM Q/CPR/CR/SESSION 4/10/VER06/95

MANIPULATION OF POINTERS
Pointers can be manipulated like variables The * gives value of the variable a pointer is pointing to Example int x, y, /* 2 ints and */ *ptr1, *ptr2; /* 2 pointers to int */ x = 786; /* set x */ y = 969; /* and y */ ptr1 = &x; /* ptr1 points to x */ ptr2 = &y; /* ptr2 points to y */ *ptr2 = 405; /* set what ptr2 is pointing to */ /* (I.e., y) to 405 */
NIIT SEM Q/CPR/CR/SESSION 4/11/VER06/95

MANIPULATION OF POINTERS (Contd.)


*ptr2 = *ptr1; /* set what ptr2 is pointing to */ /* (I.e., y) to whatever ptr1 */ /* is pointing to (I.e., x) */ /* Thus, y is set to x */ ptr2 = ptr; /* now ptr2 also points to x */ *ptr2 = 147; /* set what ptr2 is pointing to */ /* (now x) to 147 */ Note the difference between : *ptr2 = *ptr1; and ptr2 = ptr1;

NIIT

SEM Q/CPR/CR/SESSION 4/12/VER06/95

PRINTING USING POINTERS


* prefixed to a pointer can be used to print value of variable the pointer is pointing to printf (\nAt the moment, ptr is pointing to %d, *ptr); ptr is pointing to variable x; so value of x is printed

NIIT

SEM Q/CPR/CR/SESSION 4/13/VER06/95

HANDLING ONE-DIMENSIONAL CHARACTER ARRAYS USING POINTERS


Name of character type array is a pointer to first element In the declaration: char some_string [ ] = Mackennas Gold; some_string is a pointer Using pointers, the same can be declared as: char *some_string = Mackennas Gold; The fact that some_string is a pointer to a character is more apparent now To print a string, only pointer name has to be specified printf (\nA giant of a movie is : %s, some_string);
NIIT SEM Q/CPR/CR/SESSION 4/14/VER06/95

HANDLING TWO-DIMENSIONAL CHARACTER ARRAYS USING POINTERS


Declaration can be done in the following manner: char *charlies_movies [5]; This declares an array of 5 pointers to characters Each individual string can be initialised charlies_movies [0] = The kid; charlies_movies [1] = Gold rush;

NIIT

SEM Q/CPR/CR/SESSION 4/15/VER06/95

HANDLING TWO-DIMENSIONAL CHARACTER ARRAYS USING POINTERS (contd.)


Declaration-cum-initialization can be made outside main(): char *charlies_movies [ ] = { The Kid, Gold Rush, Limelight, The Great Dictator, Modern Times }; Index (5) has been left out for a more flexible declaration New entries can be typed straight away within the { }
NIIT SEM Q/CPR/CR/SESSION 4/16/VER06/95

HANDLING TWO-DIMENSIONAL CHARACTER ARRAYS USING POINTERS (contd.)


Printing of third movie is done by : printf (\n%s, charlies_movies [2] ); /* 2, NOT 3 */

NIIT

SEM Q/CPR/CR/SESSION 4/17/VER06/95

DIFFERENCE BETWEEN ARRAYS AND POINTERS


In the declaration: char str [10], *ptr;

str and ptr are both pointers


However, str is a pointer with a constant address while ptr is pointing to nowhere in particular :

gets(str)
is valid, whereas : gets(ptr) is not valid

NIIT

SEM Q/CPR/CR/SESSION 4/18/VER06/95

POINTER ARITHMETIC
Pointers can also be subjected to arithmetic ptr+ + ;/* Incrementing the pointer variable ptr */ makes ptr point to another memory location The new memory location ptr is pointing to is computed by using : New address of ptr = Old address of ptr + Scaling factor where : scaling factor is the size of variable ptr is pointing to

NIIT

SEM Q/CPR/CR/SESSION 4/19/VER06/95

CLASSROOM EXERCISE
# 1 Modify the program that determines the length of a string so that it allows input of the string from the user and displays its length. (Maximum size of the input string is 80). # include < stdio.h> char *mesg = The one earmarked for the count; main () { int count; char *ptr; for ( ptr = mesg, count = 0; *ptr != \0; ptr+ + ) { count+ + ; }
NIIT SEM Q/CPR/CR/SESSION 4/20/VER06/95

CLASSROOM EXERCISE
printf ( Length of the string is : %d\n, count);

NIIT

SEM Q/CPR/CR/SESSION 4/21/VER06/95

SOLUTION TO CLASSROOM EXERCISE


# 1 # include < stdio.h> # define SIZE_OF_STRING 81 main () { inp count; char inp_str [SIZE_OF_STRING], *ptr; printf (\nPlease input the string :); scanf (%80s, inp_str ); fflush ( stdin ); for ( ptr = inp_str, count = 0; *ptr != \0; ptr+ + , count+ + ) ; printf (\nLength of the string is : %d, count); }
NIIT SEM Q/CPR/CR/SESSION 4/22/VER06/95

CLASSROOM EXERCISE
# 2 Modify the string comparison program so that it allows input of strings and reports on whether they are the same or not. # include < stdio.h> char *mesg1 = Message 1, *mesg2 = Message 2; main () { char *ptr1, *ptr2; /* two pointers to char */ /* Note the initialisations of ptr1 and ptr2 in the for loop */ for ( ptr1 = = *ptr2 ) && ( *ptr1 != \0 ) && ( &ptr2 != \0 );
NIIT SEM Q/CPR/CR/SESSION 4/23/VER06/95

CLASSROOM EXERCISE (Contd.)


ptr1+ _ , ptr2+ + ) ; if ( ( *ptr1 = = \0 ) && ( *ptr2 = = \0) ) { printf ( The two strings are identical); } else { printf ( The two strings are NOT identical); } }

NIIT

SEM Q/CPR/CR/SESSION 4/24/VER06/95

SOLUTION TO CLASSROOM EXERCISE


#2 # include < stdio.h> # define SIZE_OF_STRING 81 main () { char inp_str1 [SIZE_OF-STRING], inp_str2 [SIZE_OF_STRING], *ptr1, *ptr2; printf ( \nPlease input the first string : ) ; scanf (%s, inp_str1 ); fflush ( stdin ); printf ( \nPlease input the second string : ) scanf ( %s, inp_str2 ); fflush ( stdin );
NIIT SEM Q/CPR/CR/SESSION 4/25/VER06/95

SOLUTION TO CLASSROOM EXERCISE


printf ( \nPlease input the second string : ) scanf ( %s, inp_str2 ); fflush ( stdin ); for ( ptr1 = inp_str1, ptr2 = inp_str2; (*ptr1 = = *ptr2 ) && ( *ptr1 != \0 ) && ( *ptr2 != \0 ) ; ptr1+ +, ptr2+ + ) ; if ( ( *ptr1 = = \0 ) && ( *ptr2 = = \0 ) ) { printf ( \nThe two strings are identical); } else { printf ( \nThe two strings are NOT identical): }}
NIIT SEM Q/CPR/CR/SESSION 4/26/VER06/95

CLASSROOM EXERCISE
# 3 A must for any program to be easily understood is that it be properly indented. This has one drawback, in terms of the size of the program on the disk. One simple way of cutting down the size significantly is described here. Assume that there is a piece of code as below : 123456789012345678901234567890 (indicates column) printf (This is the last straw); The 15 blanks at the start of the line can be replaced by the number 15 so that the line of code is now : 123456789012345678901234567890 (indicates column)
NIIT SEM Q/CPR/CR/SESSION 4/27/VER06/95

CLASSROOM EXERCISE (Contd.)


15 printf (This is the last straw); Note that a saving of 13 bytes (15 - 2) has been accomplished at one shot. Write a program which takes in a line of code from the user (maximum length of 80 characters) and prints out the condensed string (with the number of spaces occupying the first two characters in the condensed string).

NIIT

SEM Q/CPR/CR/SESSION 4/28/VER06/95

SOLUTION TO CLASSROOM EXERCISE


#3 # include < stdio.h> # define SIZE_OF_STRING 81 # define SPACE main () { int space_count; char inp_str [SIZE_OF_STRING], *ptr; printf ( \nPlease input the string : ); scanf ( %s, inp_str); fflush ( stdin );

NIIT

SEM Q/CPR/CR/SESSION 4/29/VER06/95

SOLUTION TO CLASSROOM EXERCISE (Contd.)


for ( ptr = inp_str, space_count = 0; *ptr = = SPACE; ptr+ + , space_count+ + ) ; printf ( \nThe condensed string is : ); printf ( \n%2d%s, space_count, ptr ); }

NIIT

SEM Q/CPR/CR/SESSION 4/30/VER06/95

Вам также может понравиться