Which of these function correctly defines the function g(), which should return a pointer to a dynamically allocated array?
a. int g(int size) { int *ptr = new int [size]; return *ptr;}
b. int* g (int size) { int data [size]; int *ptr =data; return ptr;}
c. int* g(int size) { int *ptr = new int[size]; return ptr;}
d.int g(int size { int *ptr = new int [size]; return ptr;}
e. int g (int size) { int data[size]; int *ptr =data; return *ptr;}
Which of the following dynamically decals an array length 20?
a.int x = 20; int* dPtr = data;
b.int data[20]; int* dPtr =data;
c.int* dPtr =new int(20);
d. int* dPtr =new int[20];
e, int* dPtr = new int;
Answer:
1) The correct answer should be c)
Explanation:
ptr is the name.of the pointer and its type is int *
So.the return type of the function should be int *
And the return statement should return ptr. So a and d are wrong.
B is also wrong as the array is not dynamically allocated in B
2) The answer is d
Explanation:
Dynamic allocation is done using.new keyword and size should be defined in square brackets and not in parantheses.
That's why other options are incorrect and d is correct .
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
Which of these function correctly defines the function g(), which should return a pointer to a...
Some pointer arithmetic is allowed. Which of the following arithmetic operators is allowed? (can have multiple answers) a) pointer + integer b) pointer - pointer c) pointer - integer d) integer + pointer e) integer * pointer Which statement correctly defines a vector object for holding Dealer objects? a.Dealer vector v; b.Dealer<vector> v; c.vector<Dealer> v; d.vector v<Dealer>; One of the following statements dynamically allocates an array of floats having size SIZE. a.float array; b.float *array[SIZE]; c.float array = new float[SIZE];...
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:...
If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...
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...
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...
Write a function to add two one-dimensional matrices of integer values. The matrices are represented as arrays of int and the function must return the result as a pointer to a new dynamically allocated array of int. You can assume the parameters are always of length greater than 0 and that no errors can occur.
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...
C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...
C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...
Please answer in C++Design and implement a program that stores the following numbers in an array named kilometers: 21, 23, 27, 18, 16, 22, and 19.The program should use a loop that displays the contents of the array.Next, create a pointer variable called kiloPointer and assign it the value of the address of the first element in the array. Use a loop that displays the array using pointer notation. The loop should display two columns of numbers, the value of the pointer and the...