Void pointer in C

Asked

Viewed 621 times

0

Write a program in C that reads 2 numbers (integer or real) and prints their sum. The user informs what type of data will be entered. Note: Use only pointers and dynamic memory allocation to solve the problem.

I have this exercise to solve and I ended up doing it this way, the result comes out as expected, however, I do not know if it was done the way it should be done.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()  {
    setlocale(LC_ALL, "Portuguese");

    int op;
    int i1, i2;
    float r1,r2;
    void *x1, *x2;

    printf("M E N U  D E  I N F O R M A Ç Õ E S\n");
    printf("1-Inteiro\n");
    printf("2-Float\n");
    scanf("%d", &op);

    switch(op) {
    case 1 :
        printf("Digite o primeiro número inteiro: ");
        scanf("%d", &i1);
        x1 = (int *)malloc((i1 * sizeof(int)));
        x1 = &i1;
        printf("Digite o segundo número inteiro: ");
        scanf("%d", &i2);
        x2 = (int *)malloc((i2 * sizeof(int)));
        x2 = &i2;

        printf("%d", i1 + i2);
        break;
    case 2 :
        printf("Digite o primeiro número real: ");
        scanf("%f", &r1);
        x1 = (float *)malloc((r1 * sizeof(float)));
        x1 = &r1;
        printf("Digite o segundo número real: ");
        scanf("%f", &r2);
        x2 = (float *)malloc((r2 * sizeof(float)));
        x2 = &r2;
        printf("%.2f", r1 + r2);
        break;
    }
    return 0;
}
  • It does not have the slightest sense you allocate memory, put the address of the allocated memory on a pointer and then make this pointer point to the address of another variable. I also see no point in allocating a void pointer because you need to know what a pointer is pointing to (a 32-bit or 64-bit int, a float, a double, etc.), study pointer arithmetic.

  • Try to do without i1, i2, r1, nor r2 and without adding other variables, only with what remains.

1 answer

1


The code you wrote does some things that make no sense. Note for example this line:

x1 = (int *)malloc((i1 * sizeof(int)));

Here creates space for as many integers as the number that is in i1. It means if the person enters 10 is creating space for 10 integers, as if it were an array, normally reserving 40 bytes in memory.

But then there’s this:

x1 = &i1;

Here you put the pointer you just created with 40 free bytes(example) pointing to the variable i1. So you lost access to the previously allocated memory and created a memory leak.


The statement is somewhat vague, but following the letter constraint of being only able to use pointers and dynamic allocation, could do so:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()  {
    setlocale(LC_ALL, "Portuguese");

    int *op, *i1, *i2;
    float *r1, *r2;

    printf("M E N U  D E  I N F O R M A Ç Õ E S\n");
    printf("1-Inteiro\n");
    printf("2-Float\n");
    op = malloc(sizeof(int));
    scanf("%d", op);

    switch(*op) {
    case 1 :
        printf("Digite o primeiro número inteiro: ");
        i1 = malloc(sizeof(int));
        scanf("%d", i1);

        printf("Digite o segundo número inteiro: ");
        i2 = malloc(sizeof(int));
        scanf("%d", i2);

        printf("%d", *i1 + *i2);
        break;
    case 2 :
        printf("Digite o primeiro número real: ");
        r1 = malloc(sizeof(float));
        scanf("%f", r1);

        printf("Digite o segundo número real: ");
        r2 = malloc(sizeof(float));
        scanf("%f", r2);

        printf("%.2f", *r1 + *r2);
        break;
    }
    return 0;
}

Watch it work on Ideone

Note that in this version, when I read the first integer, I first allocate memory space, and I save the memory location for that number:

i1 = malloc(sizeof(int));

Then I do the reading with scanf directly to that location:

scanf("%d", i1);

As I have a pointer it was not necessary to use the operator & to get the address.

At the end I have only 5 pointers on main and all dynamically allocated with malloc. This code doesn’t make much sense in real life, but meets the requirements of the statement, which I suspect are to force pointer practice and dynamic allocation.

I don’t see where the pointers come in either void which it mentions in the title of the question, but there are numerous ways to resolve this exercise.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.