Question

Pointer (76%) Pointer is a (historical) concept used in C (and C++), but not in any...

Pointer (76%) Pointer is a (historical) concept used in C (and C++), but not in any other computer languages. Due to C language’s being prevalent however, you need to learn about this. Pointer concept is explained in detail in the power point file of chapter 7 (slides 12, 20 etc. for example). 1. (12%) Printing a variable and its pointer. Consider the C instruction: int p = 0, *pptr = &p; Write a C program that prints the int variable p and its pointer pptr. Show the output (and turn in the C code, but not the 10M bytes project folder). 2. (16%) Show four (4) different ways that you increment the variable p by 1, where one of them uses *pptr or the dereferencing of pptr. 3. (24%) pointer to pointer: pptr is a pointer to the int variable p. Can there be a pointer to pptr? If that is doable, write code and show the output. If that is not doable since C language does not support (not because you do NOT know how to do that), explain! 4. (12%). Can there be two pointers to the same variable? You see that pptr points to the int variable p. Can there be a second pointer variable p2pptr also pointing to p as well? Explain! 5. (12%) Can the same pointer pptr be used to point to a different variable q (= 3) for example? Explain!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

C code:

#include<stdio.h>

void main(){
int p = 0;
int *pptr = &p;

printf("p = %d\n",p);
printf("pptr = %d",pptr);
}

Here p would print the value and pptr would print the address of the p.

Output:

Answer 2:

Different ways to the incerement value of p

#include<stdio.h>

void main(){
int p = 0;
int *pptr = &p;

printf("p = %d\n",p);

p = p + 1;
printf("p = %d\n",p);
p++;
printf("p = %d\n",p);
++p;
printf("p = %d\n",p);
*pptr = *pptr + 1;
printf("p = %d\n",p);
(*pptr)++;
printf("p = %d\n",p);
++(*pptr);
printf("p = %d\n",p);
}

Output:

Answer 3:

Yes pointer to pointer is doable in c, please find below code

Code:

#include<stdio.h>

void main(){
int p = 0;
int *pptr = &p;
int **ppptr = &pptr;

printf("p = %d\n",p);
printf("Address of p: %d\n\n",&p);

printf("p = %d\n ",*pptr);
printf("Address pointed by pptr: %d\n",pptr);
printf("Address of pptr: %d\n\n",&pptr);

printf("p = %d\n",**ppptr);
printf("Address pointed by ppptr which is address of pptr: %d\n",ppptr);
printf("Value pointed by ppptr which is address of p: %d\n",*ppptr);
}

You can see from the output the relation between p, *pptr and **pptr

Output:

Answer 4:

Yes, there can be two pointers that can point to the same variable. It means both of them can update the value of the variable and access.

Code:

#include<stdio.h>

void main(){
int p = 0;
int *pptr = &p;
int *p2pptr = &p;

printf("p = %d\n",p);
printf("Address of p: %d\n",&p);
printf("p = %d\n",*pptr);
printf("p = %d\n",*p2pptr);
printf("Address pointed by pptr: %d\n",pptr);
printf("Address pointed by p2pptr: %d\n",p2pptr);

}

Output:

Answer 5:

Yes, pptr can point to another variable. But at a time it can save only one address. So, once you update the address in the pointer it will update the pointing location. At a time pointer can't point to 2 locations.

#include<stdio.h>

void main(){
int p = 0;
int *pptr = &p;

printf("p = %d\n",p);
printf("Address of p: %d\n",&p);
printf("p = %d\n",*pptr);

int q = 0;
pptr = &q;

printf("q = %d\n",q);
printf("Address of q: %d\n",&q);
printf("q = %d\n",*pptr);
}

Output:

Add a comment
Know the answer?
Add Answer to:
Pointer (76%) Pointer is a (historical) concept used in C (and C++), but not in any...
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
  • Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise...

    Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise is to apply the knowledge of pointers and File input output operations. Program 1 Short description: Declare, initialize and use pointers for different data types. Detailed Description: Declare 4 variables of type's int, float, double and char respectively and initialize them to some logically correct values. Declare pointers to all these variables. Initialize the pointers to point to these variables respectively Print the values...

  • I need help with my C coding homework. If possible can you please comment to help...

    I need help with my C coding homework. If possible can you please comment to help me understand whats going on in the code. Thanks in advance. Create a C program (3 points): o At the top of the file include the following comments: Your name The purpose (core concept found below) of the program The date created o Create two int variables: Initialize each to different values. o Create three pointers: Point the first two pointers to the first...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • Write C++ code that creates an integer variable called number. Dereference a given pointer gPointer and...

    Write C++ code that creates an integer variable called number. Dereference a given pointer gPointer and places that value into the variable number. The pointer gPointer will have been declared and set to point to a value before your code runs. Your code will be placed inside the main function with all the appropriate #includes. After your code runs, the test case will execute the following code: cout << "number = " << number << endl; For example: Test Result...

  • C++ What is the output of the following program? Using a pointer diagram show the evolution...

    C++ What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory. Assume the code is embedded in a correct and complete program. 3.(8 pts) What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory. Assume the code is embedded in a correct and complete program. int array size 4, *a i a new int [array size] int *p...

  • 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...

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • 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()...

  • (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is...

    (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is a reference to the “Amperand (&)” Bug in the TAB() function. Draw a trace of the local variables of the functions Victim() and TAB() on the run-time stack as these functions are executed. Your diagram should be similar to the one provided on page 13. Your diagram should depict exactly three instances in program execution - T1: Right after the first statement of Victim(),...

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
ADVERTISEMENT