Practical9 2024 PF
Practical9 2024 PF
CO Mapping: CO4
Theory:
A pointer is a variable whose value is the address of another variable ie. direct address of the
memory location. Like any variable or constant, you must declare a pointer before you can use it
to store any variable address. The general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of
the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you
use for multiplication. However, in this statement the asterisk is being used to designate a
variable as a pointer.
Example:
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip ); /* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip )
When we pass the address of an array while calling a function then this is called function call by
reference. When we pass an address as an argument, the function declaration should have a pointer
as a parameter to receive the passed address.
Suggested Algorithm
(You can work with any type of elements and any size)
1. Create an array a
2. Call read_array (a,n) function to populate array with data
3. Call display_array(&a[n-1],n) function to display array in reverse order.
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Sample input and output
Test case 1:
Enter the number of elements: 10
Enter the elements: 1 12 45 67 89 32 43 54 76 87
Elements in reverse order are: 87 76 54 43 32 89 67 45 12 1
int main() {
char text[1000];
char *ptr;
int charCount = 0, wordCount = 0, inWord = 0;
ptr = text;
while (*ptr != '\0') {
charCount++;
if (isspace(*ptr)) {
inWord = 0;
} else if (inWord == 0) {
inWord = 1;
wordCount++;
}
ptr++;
}
return 0;
}
Sample input and output
Test case 1:
Enter String: How are you
The string is: How are you
Number of character=11
Number of words=3
The program was tested for different sets of inputs.
Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)
Evaluation:
On time Completion and Knowledge of the topic Implementation and Total (10)
Submission (2) (4) Output (4)