Question

l Grant P. Microsoft PowerPoi.. Mail - elijah joshlin.. Tokyo Ghoul 3 (Sub.. How to Watch The... OS EJ Purpose The purpose of
using C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have used C++ as mentioned in the question , so I have used cout and cin , and header file used is iostream.

Pointer is a variable that stores the address of another variable. Suppose we have a variable int a=10; Let the address of variable a be 0xabcde0 , and let we have a pointer variable int *ptr , so if we do ptr = &a then ptr will be having the value 0xabcde0 , i.e the address of variable "a", we say ptr points to a. This is how pointer works.

Now coming to arrays and pointers :

Suppose we have a array of 5 elements , i.e int arr[5] = {1,2,3,4,5};

Now let we have a pointer variable ptr , i.e int *ptr;

ptr = arr ; // this statement means that now we have assigned base address of array arr to ptr , now the ptr points to the start index of array arr . That is it stores the address of arr[0].

So if we perform a increment like ptr++ or ptr+ 1 , then ptr will point to next index of array. The value will increase by 4 bytes. This is shown below in the code.

So if we do cout<< *(ptr+1) ; // it will print second element of array , i.e arr[1] . Similarly *(ptr+3) means 4th element of array.

First element of the array will be *ptr , i.e arr[0].

This above concept has been used in the code below.

Your code goes here :

#include<iostream>
using namespace std;
int main(){
int i,j,*p;
i=10;
j=20;
p=&i;
cout<<"i: "<<i<<"\t\t\t\t Address of i : "<<&i<<endl;
cout<<"j: "<<j<<"\t\t\t\t Address of j : "<<&j<<endl;
cout<<"p: "<<p<<"\t\t\t Address of p : "<<&p<<endl;
p++;
cout<<endl;
cout<<"i: "<<i<<"\t\t\t\t Address of i : "<<&i<<endl;
cout<<"j: "<<j<<"\t\t\t\t Address of j : "<<&j<<endl;
cout<<"p: "<<p<<"\t\t\t Address of p after increment : "<<&p<<endl;
cout<<"\nInitializing and printing array using pointer p : "<<endl;
int arr[10]; /*Integer array of 10 elements*/
p=arr; /*assigning base address of array to p */
for(i=0;i<10;i++){
*(p+i)=0; /* Accessing each index of array using pointer and initialize to 0*/
}
for(i=0;i<10;i++){
cout<<*(p+i)<<endl; /*Printing the array values at each index*/
}
return 0;
}

Screenshots :

Output :

In the above output , notice how the address stored in the pointer variable p changed by 4 bytes when we incremented the pointer using p++ . This happens because the size of the int is 4 bytes and by incrementing address stored in p by 1, we actually increased it by 4 bytes.

Add a comment
Know the answer?
Add Answer to:
using C++ l Grant P. Microsoft PowerPoi.. Mail - elijah joshlin.. Tokyo Ghoul 3 (Sub.. How...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2,...

    QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2, 3};         int *p = a;         int **r = &p;         printf("%p %p", *r, a); A. Different memory addresses printed B. 1 2 C. Same memory address printed twice D. 1 1 2 points    QUESTION 10 What will be the output of following code snippet? int arr[4] = {1, 2, 3, 4};         int *p;         p = arr + 3;         *p = 5;         printf("%d\n", arr[3]); A....

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Modify the following C code to the changes listed below. Please format code correctly. #include <stdio.h>...

    Modify the following C code to the changes listed below. Please format code correctly. #include <stdio.h> #include <stdlib.h> #define SIZE 5 void displayData(int [], int s); void getData(int array[], int size) {     for(int i=0; i<SIZE; i++)     {         scanf("%d", &array[i]);     } } int main() {     int grades[SIZE];     for(int i=0; i<SIZE; i++)     {         grades[i] = 0;     }     getData(grades,SIZE);     displayData(grades, SIZE); } void displayData(int grades[], int size) {     for(int i=0; i<size;...

  • How do you do the commented part of this question (its the part that is bolded)?...

    How do you do the commented part of this question (its the part that is bolded)? #include <stdio.h> #include <string.h> main(){ int *p, in=10; p = &in; printf("%d\n", *p ); char arr[]="hello"; char *ptr = arr; // different ways to pass the array, at "pointer" level. printf("%s %s %s\n", arr, &arr[0], ptr); //different ways to access arr[0] printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]); //different ways to access arr[1] printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] ); //different...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator...

    State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator moves it from the back toward the front of a list b) The body of a do-while loop is always executed at least once. c) If p points to the array element a[j], then p + i points to a[i+j]. d) Any number of pointer variables may point to the same object. In terms of both execution speed and memory usage usually iterative solutions...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT