pointers sample code
pointers sample code
1. Write a program that declares an integer variable and a pointer to that integer. Initialize the integer with a value, assign
its address to the pointer, and print:
The value of the integer using the pointer
PSEUDO CODE:
BEGIN
END
SOURCE CODE:
2. Create a program that declares an array of 5 integers and initializes it. Use a pointer to traverse the array and print each
element.
PSEUDOCODE:
BEGIN
END
SOURCE CODE:
3. Write a program that counts the number of vowels and consonants in a string entered by the user. Use a pointer to
traverse the string and check each character.
·
void countVowelsAndConsonants(char *str, int
*vowelCount, int *consonantCount)
·
int isVowel(char c)// to find if it is vowel or
not
·
isalpha() //library function
PSEUDOCODE:
BEGIN
ELSE
PRINT "Enter a proper alphabetical string."
END IF
END FUNCTION
// Main function
BEGIN MAIN
END MAIN
END
SOURCE CODE:
4. Write a function void swap(int *a, int *b) that swaps the values of two integers using pointers. Create a main() function to
test your swap() function by passing the addresses of two integer variables.
PSEUDO CODE:
BEGIN
// Main function
BEGIN MAIN
DECLARE INTEGER x, y
INITIALIZE x = 10
INITIALIZE y = 20
END MAIN
END
SOURCE CODE:
5. Write a program that allocates memory dynamically for an array of n integers using malloc(). Populate the array with
values from user input and then print the array. Finally, free the allocated
memory.
PESUDO CODE:
BEGIN
SOURCE CODE: