Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c
Question
Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c
Solution
Sure, here is a simple C code that does what you asked for:
#include <stdio.h>
int main() {
// Create an array of integers
int intArray[] = {1, 2, 3, 4, 5};
int size = sizeof(intArray) / sizeof(intArray[0]);
// Create an array of pointers
int *ptrArray[size];
// Point each pointer to the corresponding integer
for(int i = 0; i < size; i++) {
ptrArray[i] = &intArray[i];
}
// Print the values using both arrays
printf("Values using integer array:\n");
for(int i = 0; i < size; i++) {
printf("%d ", intArray[i]);
}
printf("\nValues using pointer array:\n");
for(int i = 0; i < size; i++) {
printf("%d ", *ptrArray[i]);
}
return 0;
}
This code first creates an array of integers, then creates an array of pointers and makes each pointer point to the corresponding integer in the integer array. Finally, it prints the values of the integers using both the integer array and the pointer array.
Similar Questions
Write a program to add two numbers using pointers
How do you declare an array of integers in C?*1 pointint arr[10];array int[10];[10]int array;int array 10;
c program to print unique elements in an array
write a c program to print unique prime numbers in array
Write a function, which prints all numbers in common between two given arrays, with each number printed only once. The function returns number of common elements.Assume that both arrays contain each number only once.int common(int *a, int lena, int *b, int lenb);Note: the function does printing.Note: in the output shown, the last number printed is the return value of the function common.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.