I am coding in C. I want to create an array in C dynamically using malloc (I am
ID: 3624620 • Letter: I
Question
I am coding in C. I want to create an array in C dynamically using malloc (I am not allowed to use [ ] in this program). I want to fill the array with 1's. I must do this with pointers, and pointer arithmetic.When I check the value at the address a[2], I should get a 1. How do I print to screen the 1 for the contents, and 2 for the address?
My output:
I enter: 100
This is *p: 14448120
This is &p: 2686756
/* Program prompts user to input upper limit of primes to compute. We use the
Sieve of Eratosthenes to generate prime numbers. Then we display the prime
numbers.
*/
#include <stdio.h>
#include <stdlib.h>
/*global variables*/
int upperLimit = 0; //upper limit of primes to compute
/*functions*/
void runSieve(void);
int main()
{
runSieve();
system("PAUSE");
return 0;
}
/*function for generating prime numbers*/
void runSieve(void)
{
while(upperLimit <= 1){ //loops until upperLimit >= 2
printf("Enter the upper limit for the number of primes that you "
"would like to compute. Must be an integer: ");
scanf("%d", &upperLimit);
}
int *a; //creates pointer that will store first address in array
a = malloc(upperLimit * sizeof(int)); //creates array dynamically
int *p; //pointer for working on array
p = a; //pointer is set to first address in array
p = p + (2 * sizeof(int)); //we want to start array at 2
if(a){
while(*p <= upperLimit){
*p = 1;
p += (sizeof(int));
}
}
else{
printf("Memory allocation failure. ");
exit(1); //terminates program
}
*a += *a + (2 * sizeof(int)); //we want to start array at 2
printf("This is *a: %d ", *a); //checking contents of array
printf("This is &a: %d ", &a); //checking contents of array
free(a); //free memory allocation
}
Explanation / Answer
// dude u have to know how to do pointer airthematic. ...
// to start the array at 2 we have just add 1 to pointer.... not sizeof(int) ?
// p represents start of array 1st element
// p+1 represents 2nd element of array ....
// after that.. u have to start at array of 2 menas u have to take jsut a+1
// compiel in DEV C++ and RUN ....ENJOY , CHEERS .:)
// modified code shown in red colour....
#include <stdio.h>
#include <stdlib.h>
/*global variables*/
int upperLimit = 0; //upper limit of primes to compute
/*functions*/
void runSieve(void);
int main()
{
runSieve();
system("PAUSE");
return 0;
}
/*function for generating prime numbers*/
void runSieve(void)
{
while(upperLimit <= 1){ //loops until upperLimit >= 2
printf("Enter the upper limit for the number of primes that you "
"would like to compute. Must be an integer: ");
scanf("%d", &upperLimit);
}
int *a,i=0; //creates pointer that will store first address in array
a = (int *)malloc(upperLimit * sizeof(int)); //creates array dynamically
int *p; //pointer for working on array
p = a; //pointer is set to first address in array
p = p + 1; //we want to start array at 2
if(a){
while(i<upperLimit-1){
*p = 1;
p = p+1;
i++;
}
}
else{
printf("Memory allocation failure. ");
exit(1); //terminates program
}
a = a+1; //we want to start array at 2
printf("This is *a: %d ", *a); //checking contents of array
printf("This is &a: %d ", &a); //checking contents of array
free(a); //free memory allocation
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.