Академический Документы
Профессиональный Документы
Культура Документы
# 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
NIIT
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
NIIT
NIIT
{
printf ( \nptr is now pointing to %d, *ptr); } }
NIIT SEM Q/CPR/CR/SESSION 4/6/VER06/95
b. True
NIIT SEM Q/CPR/CR/SESSION 4/7/VER06/95
NIIT
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
Initialization
A pointer can potentially point to any portion of memory. Thus, initialization of pointers is a must
int x, &ptr;
ptr = &x;
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
NIIT
NIIT
NIIT
NIIT
gets(str)
is valid, whereas : gets(ptr) is not valid
NIIT
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
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
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
NIIT
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
NIIT
NIIT
NIIT