3. Pointers Memory. (30 Points) (a) Demonstrate what is going on in memory below
ID: 3809693 • Letter: 3
Question
3. Pointers Memory. (30 Points) (a) Demonstrate what is going on in memory below. Use abbreviated problem is. form. If an error will result (or will likely result), write Error and explain what the Assume that regular variables g put in memory starting location 212Aoo00 that pointer variables get put in memory starting at address C5770000. char c; //Line 1 char Cptr i Line 2 int nptr //Line 3 Address Contents Variable 12 A 0000 10 Line 4 cptr &C; Variable Contents Address 12 A 00 o 0 C 77 o o 212 A 0000 *cptr 'A' //Line 5 Variable Contents Address 112. A o o o C s 77Explanation / Answer
Please find below explaination:
char c;
character type of variable is created and the memory is allocated for this cvariable c, size of memory would be the sizeof data type.
Address: Memory address of variable c.(212A0000)
Content: Content of that memory address.(empty)
Variable:(c)
char *cptr;
A pointer type varible is created and the memory is allocated for this variable, size of memory is the size of memory address.
Address: Memory address of pointer variable.(C53A0000)
Content: Content of that memory address.(some junk address)
Variable:(cptr)
int *nptr;
A pointer type varible is created and the memory is allocated for this variable, size of memory is the size of memory address.
Address: Memory address of pointer variable.(A53C0000)
Content: Content of that memory address.(some junk address)
Variable:(nptr)
nptr = (int*) malloc(3*sizeof(int));
nptr pointer variable is initialoze with the starting address of the memory allocated by malloc(dynamically), the size is the thrice the size of int data type.
Address: Memory address of pointer variable.(A53C0000)
Content: Content of that memory address.(D83C0000) (Starting address of allocated memory)
Variable:(nptr)
for(int i = 0; i<3;i++)
{
*(nptr + i) = 10*i;
}
value has been initialized at the memory occupied by the nptr varible, all the three address starting from (D83C0000) will have the values (0,10,20).
Address: Memory address of pointer variable.(A53C0000)
Content: Content of that memory address.(D83C0000) (Starting address of allocated memory)
Variable:(nptr)
Program to print the values and addresses:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char c;
char *cptr;
int *nptr;
printf("Address=%p ",(void*)&c);
printf("Content=%c ",c);
printf("Address=%p ",&cptr);
printf("Content=%p ",&(*cptr));
printf("Address=%p ",&nptr);
printf("Content=%p ",&(*nptr));
nptr = (int*) malloc(3*sizeof(int));
for(int i = 0; i<3;i++)
{
*(nptr + i) = 10*i;
}
printf("Address=%p ",&nptr);
printf("Content=%p ",&(*nptr));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.