programming 1. (a) In main, define the following variables and pointer variables
ID: 3573327 • Letter: P
Question
programming
1. (a) In main, define the following variables and pointer variables and construct the following table which shows what the memory looks like
Address | Contents | Variable
C5AB2130
|
65
|
c
C5AB2131
|
5000
|
n
C5AB2135
|
12.884
|
f
A5AB2150
|
C5AB2130
|
cptr
A5AB2154
|
C5AB2131
|
nptr
A5AB2158
|
C5AB2135
|
fptr
----------------------------------------------------
(b) Verify that the * operator works as expected by printing out the results of the look-up:
*cptr: 65
*nptr: 5000
*fpt: 12.884
2. Be able to explain and write memory tables for the following: (a) Declaration of a regular struct or array variable
(b) Declaration of a pointer struct variable
(c) Initialization of a pointer struct variable by using an existing variable
(d) Initialization of a pointer struct variable by using malloc
(e) Accessing/changing the value of each member variable of a structure for both regular variables (use .) and pointer variables (use ->).
(f) Declaring an array of structures using malloc.
(g) Write code to produce the memory table for you.
C5AB2130
|
65
|
c
C5AB2131
|
5000
|
n
C5AB2135
|
12.884
|
f
A5AB2150
|
C5AB2130
|
cptr
A5AB2154
|
C5AB2131
|
nptr
A5AB2158
|
C5AB2135
|
fptr
AT&T; F 1:29 AM K Practice-Problems-for 7 Pointer Variables and Memory Tables (a) In nain, define the following variables and pointer variables and construct the fellowing table which shows what the memory looks like. (b) Verify that the operator works asexpected by printing out the results of the look up: cptr: 65 nptr: 5000 12.884 2.Be able to cxplain and write memory tables for the following of a regular struct or amay variable Declaration of a pointer struct variable Initialization of pointer struct variable by using an existing variable Initialization of a pointer struct variable by using malloc Accessing changing the value of each member variable of a structure for both regular variables use variables (use and pointer Declaring an amay of structures using malloc. Write code to produce the memory table for you. (a Write a nain function that stores an amay with the numbers: 50, 100, 150 and prints memory table for the array in the following unabreviated format. Hint Use a pointer variable with type unsigned char and loop 4 times the length ofthe array. (b) Explain the comments column. What's going on here? (e) Write similar program for a string charamay. 1 of 7 To Do Notifications Messages Courses CalendarExplanation / Answer
//Program1
#include<stdio.h>
int main()
{
//define variables
int c = 65, n = 5000;
float f = 12.884, *fptr;
int *cptr, *nptr;
//assign address of the variables to pointer variable
cptr = &c;
nptr = &n;
fptr = &f;
//memory table for variables declared
printf("Address | Contents | Variable ");
printf("%x | %d |c ", &c, c);
printf("%x | %d |n ", &n, n);
printf("%x | %f |f ", &f, f);
printf("%x | %x |cptr ", &c, cptr);
printf("%x | %x |nptr ", &nptr, nptr);
printf("%x | %x |c ", &fptr, fptr);
//Verifying the * operator works as expected
printf(" Contents of cptr = %d ", *cptr);
printf("Contents of nptr = %d ", *nptr);
printf("Contents of fptr = %f ", *fptr);
}
----------------------------------------------------------------------------------------------------
Program1 Output:
Address | Contents | Variable
50fd78 | 65 |c
50fd6c | 5000 |n
50fd60 | 12.884000 |f
50fd78 | 50fd78 |cptr
50fd3c | 50fd6c |nptr
50fd54 | 50fd60 |c
Contents of cptr = 65
Contents of nptr = 5000
Contents of fptr = 12.884000
----------------------------------------------------------------------
//Program2
#include<stdio.h>
#include<stdlib.h>
//declare struct variable
struct test
{
char c;
int n;
float f;
};
int main()
{
struct test t1;
//declare pointer variable to struct test
struct test *ptr1,*ptr2,*ptr3;
int i;
//assign t1 to ptr
ptr1 = &t1;
//assign values to t1 structure variable
t1.c = 65;
t1.n = 5000;
t1.f = 12.884;
//declare ptr2 using malloc
ptr2 = (struct test*)malloc(sizeof(struct test));
//assign values to ptr2
ptr2->c = 66;
ptr2->n = 6000;
ptr2->f = 13.884;
//declare array of struct using ptr3 with size 3
ptr3 = (struct test*)malloc(3*sizeof(struct test));
//assign values to array of struct using ptr3
for (i = 0; i < 3; i++)
{
ptr3[i].c = 67+i;
ptr3[i].n = 7000+i;
ptr3[i].f = 14.678+1;
}
//print memory table for structure variable and pointers
printf("Memory table for struct variable t1 ");
printf("Addres | Contents | Variable ");
printf("%x | %d |c ", &t1.c, t1.c);
printf("%x | %d |n ", &t1.n, t1.n);
printf("%x | %f |f ", &t1.f, t1.f);
printf("________________________________________________________ ");
//print memory ptr1 which pints to t1
printf("Memory table for pointer variable ptr1 which points to struct t1 ");
printf("Addres | Contents | Variable ");
printf("%x | %x |cptr1 ", &ptr1, &ptr1->c);
printf("%x | %x |nptr1 ", &ptr1, &ptr1->n);
printf("%x | %x |fptr1 ", &ptr1, &ptr1->f);
printf("________________________________________________________ ");
//print ptr2 which is allocated using malloc and points to t2
printf("Memory table for pointer variable ptr2 which is allocated using malloc ");
printf("Addres | Contents | Variable ");
printf("%x | %x |cptr2 ", &ptr2, &ptr2->c);
printf("%x | %x |nptr2 ", &ptr2, &ptr2->n);
printf("%x | %x |fptr ", &ptr2, &ptr2->f);
printf("________________________________________________________ ");
//print the array of struct through pointer
printf("Memory table for pointer variable which points to array of structure ");
printf("Addres | Contents | Variable ");
for (i = 0; i < 3; i++)
{
printf("%x | %x |cptr3[%d] ", &ptr3[i], &ptr3[i].c,i);
printf("%x | %x |nptr3[%d] ", &ptr3[i], &ptr3[i].n,i);
printf("%x | %x |fptr3[%d] ", &ptr3[i], &ptr3[i].f,i);
}
}
------------------------------------------------------------------
output2:
Memory table for struct variable t1
Addres | Contents | Variable
1efc00 | 65 |c
1efc04 | 5000 |n
1efc08 | 12.884000 |f
________________________________________________________
Memory table for pointer variable ptr1 which points to struct t1
Addres | Contents | Variable
1efbf4 | 1efc00 |cptr1
1efbf4 | 1efc04 |nptr1
1efbf4 | 1efc08 |fptr1
________________________________________________________
Memory table for pointer variable ptr2 which is allocated using malloc
Addres | Contents | Variable
1efbe8 | 647900 |cptr2
1efbe8 | 647904 |nptr2
1efbe8 | 647908 |fptr
________________________________________________________
Memory table for pointer variable which points to array of structure
Addres | Contents | Variable
647948 | 647948 |cptr3[0]
647948 | 64794c |nptr3[0]
647948 | 647950 |fptr3[0]
647954 | 647954 |cptr3[1]
647954 | 647958 |nptr3[1]
647954 | 64795c |fptr3[1]
647960 | 647960 |cptr3[2]
647960 | 647964 |nptr3[2]
647960 | 647968 |fptr3[2]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.