Question

If void * is a pointer to void is a "generic" pointer type, and a void...

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 copy of char array, 
 * int array, and double array. Then print out three arrays
 * to check the result
*/
#include <stdio.h>
#include <stdlib.h>

// This function will copy any type of array
// and return a pointer to it
void* myarrcopy(void *arr, size_t num_elmts, size_t size_elmt)
{
        // allocate memory as char pointer
        size_t total_bytes = num_elmts * size_elmt;
        char* result = malloc(total_bytes);
        char* c = arr;
        // copy over the array memory char by char
        for(int i = 0; i < total_bytes; i++)
                result[i] = c[i];
        // return a memory pointer 
        return (void *) result;
}  

int main(int argc, char *argv[]){
        int i;
        char charArr[] = {'A', 'B', 'C', 'D', 'E'};
        int intArr[] = {1, 2, 3, 4, 5};
        double doubleArr[] = {1.1, 2.22, 3.333, 4.4444, 5.55555};
        
        /*copy and print out the char array */
        char* cptr = myarrcopy(charArr, 5, sizeof(char));
        for(i = 0; i < 5; i++)
                printf("%c\t", cptr[i]);
        printf("\n"); 
        
        /*copy and print out the int array */
        int* iptr = myarrcopy(intArr, 5, sizeof(int));
        for(i = 0; i < 5; i++)
                printf("%d\t", iptr[i]);
        printf("\n");
        
        /*copy and print out the double array */
        double* dptr = myarrcopy(doubleArr, 5, sizeof(double));
        for(i = 0; i < 5; i++)
                printf("%f\t", dptr[i]);
        return EXIT_SUCCESS;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please pay much attention to the fact that, there are data and pointers type.. The data types are int, float, char, boolean etc.. Which the pointer types point to some kind of data.. 
The data types have varying sizes like int is of 4 bytes, boolean of 1 byte and so on.. While the pointers are essentialy a address, which just tell that some memory location is a starting address for some data which we kept. Now what kind of data is kept on the memory is determined by type of pointer.. Like int pointer will point to data in memory which is of integer type, the long pointer points to the data which is of type long.. and so on.. But essentially if you look, all pointers are nothing but a reference to the start of the memory, Due to which their sizes are always same.. no matter what kind of data they are pointing to..

Hence, Either we allocate memory for storing char pointers, or void pointers, or long pointers, they all are going to take the same memory.. Also, malloc just allocates the memory.. you are free to store anything in that memory.. So You are not bound to store only character pointers if you are allocating with sizeof(char*)



hope it clarifies your doubts.. feel free to ask if anything is not clear.
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
If void * is a pointer to void is a "generic" pointer type, and a void...
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
  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • 1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int...

    1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int start, int end, int *results) { int length = end - start; results = malloc(sizeof(int) * length); for (int i=start; i<end; i++) { *results = i; results++; } } void printArray(int *arr, int length) { for (int i=0; i<length; i++) { printf("%d ", arr[i]); } } int main() { int *x; int s = 2; int e = 11; range(s, e, x); printArray(x, e...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...

    Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...

  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    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 * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

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

  • How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...

    How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity = 8 0 5 10 15 20 Capacity = 16 0 5 10 15 20 25 30 35 40 Capacity = 32 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125...

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