Question
Question 1: Pointers You are given the following C code and memory diagram. The contents column of the memory diagram shows the value of the
Question 1: Pointers
- You are given the following C code and memory diagram. The contents column of the memory diagram shows the value of the variable. Update the contents column of the memory diagram for each assignment in main().
long *pt; long data; long buffer[4]; void main(void){ pt = &buffer[1]; *pt = 1234; data = *pt; }
address contents variable 0x20000000 0x00000000 pt 0x20000004 0x00000000 data 0x20000008 0x00000000 buffer[0] 0x2000000C 0x00000000 buffer[1] 0x20000010 0x00000000 buffer[2] 0x20000014 0x00000000 buffer[3]
- Consider the following code.
The function GetFifo returns two parameters. The return parameter is a boolean specifying whether or not the request was successful, and the actual data removed from the queue is returned via the call-by-reference parameter. The calling program InChar passes the address of its local variable data. Normally GetFifo does not have the scope to access local variables of InChar, but in this case InChar explicitly granted that right by passing a pointer to GetFifo.
#define FifoSize 10 /* Number of 8-bit data in FIFO */ unsigned char PutI; /* Index of where to put next */ unsigned char GetI; /* Index of where to get next */ unsigned char Size; /* Number currently in the FIFO */ char Fifo[FifoSize]; /* FIFO data */
int GetFifo (char *datapt) { if(Size == 0 ) return(0); /* empty if Size=0 */ else{ *datapt=Fifo[GetI++];
Size--; if (GetI == FifoSize) GetI = 0; return(-1); } }
char InChar(void){
char data=0; if(GetFifo(&data)){}; return (data);
}
Questions:
- Sketch a simple diagram that illustrates the different scope of variables in the code. Use any notation you want to show global versus local variables and pointer variables. You are not given information about actual memory addresses, so do not try to set up a memory diagram.
- What would happen if GetFifo is called instead with the following parameter GetFifo(data)? Will rate thanks!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started