Concept explainers
Pointer:
Pointer, the name itself references the purpose of the pointer. Pointers point to a location in memory.
- Pointer is a special type of variable to store the address of the memory location, which can be accessed later.
- If an asterisk “*” operator is present before the variable, then that variable is referred as pointer variable.
- It is also called as dereferencing or indirection operator.
- Pointer is just a type of variable that stores the addresses of other variables.
- Using pointers, we can access the address of a variable; the data stored in that variable can be retrieved.
Syntax of pointer variable declaration:
<variable-type> *<variable-name>;
Adding a value to a pointer variable:
When a pointer variable is added it is similar to adding number of time the size of the pointer variable that is stored.
Example:
//declaring a pointer variable
int *a;
//incrementing the pointer value by 1
a++;
In the above snippet the pointer variable is incremented by “1” means, the size of the pointer variable is being increased in terms of their size.
1 * 2 = 2
Therefore, additional 2 bytes will be added to the pointer variable “a” after incrementing it by “1”.
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
- How may the address of a variable be added to a pointer variable? Why is the code below problematic?arrow_forward16. NULL can be assigned to a void pointer. True O Falsearrow_forwardwrite a single statement that performs the specified task. Assume that long variables value1 and value2 have been declared and value1 has been initialized to 200000 ."Assign the address of variable value1 to pointer variable longPtr .arrow_forward
- Create a program in C language that calculates the month's day from a given year and year's day. Use pointers for the month and month's day variables. Don't forget to add proper errors handling in your program. Example or errors - Invalid Input- Invalid year- Invalid year day Example of input # ./month_day <year> <yearday> # Example for Feb 2nd, 2019:\$ ./month-day 2019 33Feb 02, 2019 I have this class - month-day.c - #include <stdio.h> /* month_day function's prototype*/void month_day(int year, int yearday, int *pmonth, int *pday); int main() {return 0;} Note: I don't need the calendar, please read the instructions well!!arrow_forwardAssuming that ptr is a pointer to an int, what happens when you add 4 to ptr ?arrow_forwardExplain the difference between a pointer variable and other variables such as int, float, char.arrow_forward
- write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory. " Write two separate statements that assign the starting address of built-in array values to pointer variable vPtr ."arrow_forwardIn C Languagearrow_forward#include <stdio.h>int main() {long local;int (*mainptr)(void); /* declare a "pointer to function returning int" */mainptr = main; /* address of main */printf("%p,%p\n",&local,mainptr); /* print the two addresses */return 0;}The printf statement outputs the address of the variable “local”, which should be on the stack rightafter the return address, and then the address of the code for “main”.Start by creating a source file aslrtest.c on your VM containing the above C program. Compileit into an executable with the same name:bash$ gcc -o aslrtest aslrtest.c(As always, the “bash$” in the above represents the prompt and is not part of the command youtype.)a. Turn off ASLR with the command:echo 0 | sudo tee /proc/sys/kernel/randomize_va_spaceOnce you have ASLR disabled, run aslrtest 10 times. The following shell script will do that,(assuming your shell is bash, which it is unless you’ve changed it yourself):bash$ for i in {1..10}> do> ./aslrtest> doneWhat range of output…arrow_forward
- Create a function that can accept a pointers and array with values of 1000, 2, 3, 17, 50. Your program will display the array values and its averagearrow_forwardAssume ptr is a pointer, the following expressions has the same values *&ptr and ptr? Select one: a. True b. Falsearrow_forwardCode: //POinters #include<iostream> using namespace std; int main() { int var = 9; int *ip; double *dp; float *fp; char *ch; ip=&var; //&= address of (saves address of variable var in ip) cout<<"Variable Value "<<var<<endl; cout<<"Address saved in pointer "<<ip<<endl; cout<<"Pointer pointing to value "<<*ip<<endl; *ip=50; cout<<"\nVariable Value "<<var<<endl; cout<<"Address saved in pointer "<<ip<<endl; cout<<"Pointer pointing to value "<<*ip<<endl; } //Dynamic Memory Allocation #include<iostream> using namespace std; int main() { int size,i; int *ptr; cout<<"Enter size of an array"<<endl; cin>>size; ptr = new int[size]; cout<<"Enter Array Elements to store\n"; for(i=0;i<size;i++) { cin>>ptr[i]; } cout<<"Values of Array are\n"; for(i=0;i<size;i++) { cout<<ptr[i]<<endl; } for(i=0;i<size;i++) {…arrow_forward
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning