Week 1
1 InClass Exercise: Pointer Values
Due
: Friday
March 27, 2020
(8 marks
)
Indicate what the screen output is for the following code. Show your work on how you determined the
values.
#include
<stdio.h>
void MyFunction(
char *Msg);
int main(
void )
{
char *pChar, MyString[] =
"Hello World"
;
pChar = MyString;
putchar(pChar[2]);
printf(
"\n" );
putchar(*(pChar + 5));
printf(
"\n" );
pChar = &MyString[3];
puts(pChar);
pChar = &MyString[1];
MyFunction(pChar);
MyFunction
II (pChar);
return
0;
}
// --------------------------------------------
void MyFunction(
char *Msg )
{
char *pMsg;
pMsg = &
Msg [5];
puts(pMsg);
}
// --------------------------------------------
void MyFunction(
char *Msg )
{
char *pMsg;
pMsg = &
Msg [-1];
puts(pMsg)
The answer to the above problem will be as follows-
The Final output on screen will be like this below-
l
<blank space>
lo World
World
Hello World
Now the code along with the explanation is as below-
The explanation is written as comments along with the code.
The image of explanation in the form of comments is below-
In the image below, it is mentioned how each line of code produces what output. The description is given in comments.
First, pChar is assigned the value "Hello World".
so when putchar(pChar[2]) is executed, it prints value at index 2 which is l
Now there is a change in line.
next character at index 5 is printed, which is a blank space. So nothing is displayed.
Again line is changed.
now string starting from index 3 is printed, which results in lo World
Then pChar is assigned value starting from index 1,i.e, "ello World" and the two functions are called on this value.
The first function prints this "ello World" from index 5, so it prints World
The second function prints the entire string because index mentioned is -1, so it displays ello World
This is the entire explanation also mentioned in comments along side the code below.
If this answer helps, please give an up vote and feel free to comment for any query.
Week 1 1 InClass Exercise: Pointer Values Due : Friday March 27, 2020 (8 marks )...
2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....
If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...
(Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....
I need help on this Systems review please! it's due by midnight monday. Question 1 Not yet answered Points out of 1.00 Flag question Question text Using these declarations: int * numberPointers[3]; int * pointer; int number; Which of the following statements would generate a warning or error? Select one: a. number = pointer; b. *pointer = number; c. pointer = numberPointers; d. numberPointers[2] = &number; e. a., b., and d. f. a. and c. Question 2 Not yet answered...
QUESTION 1 In order to print the members of structures in the array, what will be the contents of blanks in the printf statement of the code snippet given below? #include <stdio.h> struct virus { char signature[25]; char status[20]; } v[2] = { "Yankee Doodle", "Deadly", "Dark Avenger", "Killer" } ; void main( ) { for (int i = 0; i < 2; i++) printf( "\n%s %s", _______________ , _____________ ); } A. signature, status B. v.signature,...
Ensure the following compiles 5. Variable scope (1 mark) Some variables are only accessible while executing specific code. Global variables are often thought of as evil because the state of a system can be altered making functions execute differently when it isn't expected. There is also the issue of block scope. For an example of block scope, see the file below. Notice that you don't get an error because the code uses the correct syntax. Although the syntax is correct,...
1. In a function that gets a value from the keyboard and communicates that value to the main function via a parameter, the parameter used is considered __________. 2. In a function that receives a value from the main function via a parameter and then displays the parameter value on the screen, that parameter is considered __________. 3. In a function that receives one parameter value from the main program and passes back a changed value of the parameter, that...
3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...
Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...
Define a type which comprises a struct called "Maxima". In this struct contains two int values a and b. The purpose of this struct is to store the largest two int values among a set of integers. The value a is the largest number and the value b is the second largest number. In order to accomplish this task, you need to write the following functions: allzero( struct pointer ): This function sets a and b values in a given...