I have some questions about pointers/pointer arithmetic in C++
1) Pointers and Pointer Arithmetic
a.) What is the difference between statically allocated arrays and dynamically allocated arrays (be brief)
b.) Which of the following pointers can be used for a dynamically allocated array? (Circle) char ptr; char * ptr; char ptr *; char ptr[]; char [] ptr;
c.) Show now, using that pointer, how to dynamically allocate array of characters of size 10: (don't use malloc)
d.) Which of the following will store an ‘a’ in the first element of the array: (Circle) ptr[1] = ‘a’; ptr[] = ‘a’; ptr = ‘a’; *ptr= ‘a’; *ptr[0] = ‘a’;
e.) Show how to store the word “Highlander” into this array that has already been created:
f.) How could you display just the last character of the word – if you didn’t know what element it was at?
Using the subscript operator: Using pointer arithmetic:
e.) Release the dynamic memory used on this page:
a.) What is the difference between statically allocated arrays and dynamically allocated arrays
Answer:
statically allocated arrays are stored on stack and dynamically allocated arrays are stored in heap.
b.) Which of the following pointers can be used for a dynamically allocated array?
Answer:
char*ptr
c.) Show now, using that pointer, how to dynamically allocate array of characters of size 10: (don't use malloc)
Answer:
ptr=new char[10];
d.) Which of the following will store an ‘a’ in the first element of the array:
Answer:
*ptr= ‘a’;
e.) Show how to store the word “Highlander” into this array that has already been created:
Answer:
Using strcpy function:
strcpy(ptr,Highlander);
f.) How could you display just the last character of the word – if you didn’t know what element it was at?
Using the subscript operator:
Answer:
cout<<ptr[strlen(ptr)-1];
Using pointer arithmetic:
Answer:
cout<<*(ptr+strlen(ptr)-1);
e.) Release the dynamic memory used on this page:
Answer:
delete[] ptr
I have some questions about pointers/pointer arithmetic in C++ 1) Pointers and Pointer Arithmetic a.) What...
How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() { // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic // tic tac toe board is an array of int pointers // each int pointer in the board points to a row // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int) const...
C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...
Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...
Malloc function
For the prelab assignment and the lab next week use malloc
function to allocate space (to store the string) instead of
creating fixed size character array. malloc function allows user to
allocate memory (instead of compiler doing it by default) and this
gives more control to the user and efficient allocation of the
memory space.
Example
int *ptr
ptr=malloc(sizeof(int)*10);
In the example above integer pointer ptr is allocated a space of
10 blocks this is same as creating...
3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...
solve it in c+*
Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...
In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...
IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its...
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:...
Quick C Programming Questions: I) Which of the following is false? A) To pass a structure by reference, pass the address of the structure variable. B) A way to pass an array by value is to create a structure with the array as a member then pass the name of the structure. C) To pass a structure by reference, pass the name of the structure variable. D) Passing large structures by reference is more efficient than passing large structures by...