- Objective
- Create a program that will initialize an array, initialize a pointer to that array, and output the contents and address of the array in various ways.
- Create a call-by-reference function to modify a variable that's been declared in the main() function by passing the variable's address to the function.
- How to create, output, and dereference a pointer.
- How to use a dereferenced pointer to change the value of a variable.
- Instructions
- Create a program named LastnameFirstname13.c, that does the following:
- Note: This program has no user input.
- Declare and initialize an integer array of size 5. Initialize each element with a value other than 0.
- Declare and initialize an integer pointer that points to the address of the integer array above.
- Declare and initialize an integer variable. Use an initial value other than 0.
- Later in the program, this variable will be used in a call-by-reference function that modifies the value without returning a value.
- Output the last element in the array in the following four ways:
- Using array subscript notation with the array name
- Using pointer/offset notation with the array name
- Using array subscript notation with the pointer
- Using pointer/offset notation with the pointer
- Output the contents of the array in the following four ways:
- Using array subscript notation with the array name
- Using pointer/offset notation with the array name
- Using array subscript notation with the pointer
- Using pointer/offset notation with the pointer
- Output the addresses of each element in the array using array subscript notation with the array name
- NOTE: The C compiler on UH UNIX has been updated to automatically add the 0x prefix when using the format specifier %p. You do not need to manually type the 0x prefix.
- Create the following custom function given its prototype and description:
Declare the function prototype above main and the function definition below main.- void increaseIntViaPointer(int *);
- A call-by-reference function that will increase the value of the integer that the parameter pointer points to.
- At the beginning of the function, output a message to tell the user that execution is inside the function and that the parameter is about to be modified by 100.
- Increase the integer value that the pointer points to by 100. In other words, dereference the parameter pointer and increase the integer value by 100.
- At the end of the function, output another message to show the new integer value by dereferencing the pointer.
- void increaseIntViaPointer(int *);
- Back in the main() function, output a message showing the initial state of the variable before calling the custom function.
- Call increaseIntViaPointer() and pass in the address of the integer variable that was created at the beginning of the program.
- Output another message showing the final state of the variable after calling the custom function.
- Use the Example Output to format your output the same way.
- All output should be clear in what and how it is being displayed.
- Be sure to have a program description at the top and in-line comments.
- Be clear with your comments and output to the user, so I can understand what you're program is doing.
- Create a program named LastnameFirstname13.c, that does the following:
- attached is an example output
Answers:-
Code:-
#include<stdio.h>
void increaseIntViaPointer(int * intParam)
{
printf("Inside the increaseIntViaPointer() function, increasing parameter by 100...\n");
*intParam = *intParam+100;
printf("*intParam = %d\n",*intParam);
}
int main ()
{
int intArray[5] = {13,72,88,100,17};
int *intArrayPointer = intArray;
int intVariable = 10;
printf("Print last element in the array\n");
printf("intArray[4] = %d\n",intArray[4]);
printf("*(intArray+4) = %d\n",*(intArray+4));
printf("intArrayPointer[4] = %d\n",intArrayPointer[4]);
printf("*(intArrayPointer+4) = %d\n",*(intArrayPointer+4));
printf("Print contents of the array:\n");
int i;
printf("Using the array subscript notation with array name...\n");
for(i=0;i<5;i++)
{
if(i==4)
{
printf("%d ",intArray[i]);
continue;
}
printf("%d, ",intArray[i]);
}
printf("\n");
printf("Using the pointer/offset notation with array name...\n");
for(i=0;i<5;i++)
{
if(i==4)
{
printf("%d ",*(intArray+i));
continue;
}
printf("%d, ",*(intArray+i));
}
printf("\n");
printf("Using the array subscript notation with pointer...\n");
for(i=0;i<5;i++)
{
if(i==4)
{
printf("%d ",intArrayPointer[i]);
continue;
}
printf("%d, ",intArrayPointer[i]);
}
printf("\n");
printf("Using the pointer/offset notation with pointer...\n");
for(i=0;i<5;i++)
{
if(i==4)
{
printf("%d ",*(intArrayPointer+i));
continue;
}
printf("%d, ",*(intArrayPointer+i));
}
printf("\n");
printf("Print addresses of each element in the array:\n");
for(i=0;i<5;i++)
{
printf("&intArray[%d] = %p\n",i,intArray+i);
}
printf("\n");
printf("Integer variable BEFORE function call...\n");
printf("intVariable = %d\n",intVariable);
increaseIntViaPointer(&intVariable);
printf("Integer variable AFTER function call...\n");
printf("intVariable = %d",intVariable);
return 0;
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- To what is the term “base address of an array” refer, and how is it used in a function call?arrow_forwardGive an example of a function with two arguments and statements that are executed when the function is called. -Provide details about the return statement and the purpose of your function. -Provide an example of an array and a vector. -Describe your experience using GitHub, GitLab, Git, or other versional control software in general. Explain what aspects of the GitHub process you are comfortable with and what areas you need more practice or help.arrow_forwardChoose if each of the following is true or false:Dynamically binding data to virtual functions is limited to pointers and references.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education