Pointer Statement in a struct and calling it in function

Asked

Viewed 211 times

1

I must solve the following problem by creating a function for reading, one for calculation and using pointers.

inserir a descrição da imagem aqui

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

struct ponto
{
    int x;
    int y;
}ponto1, ponto2;
struct ponto *p1, *p2;
p1 = &ponto1;
p2 = &ponto2;

void ler(struct ponto *p1, *p2)
{
    printf("Digite o valor de X1: ");
    fflush(stdin);
    scanf("%d", &p1 -> x);
    printf("Digite o valor de Y1: ");
    fflush(stdin);
    scanf("%d", &p1 -> y);
    printf("Digite o valor de X1: ");
    fflush(stdin);
    scanf("%d", &p2 -> x);
    printf("Digite o valor de Y1: ");
    fflush(stdin);
    scanf("%d", &p2 -> y);
}

int calculo(struct ponto *p1, *p2)
{
    int a, b, d;
    a = (*p1).x - (*p2).x;
    b = (*p1).y - (*p2).y;

    d = sqrt(pow(a,2)+pow(b,2));

    printf("%d", d);
}

int main()
{
    ler(struct ponto *p1, *p2);
    calculo(struct ponto *p1, *p2);
    return 0;
}

but I’ve tried so many different ways and keep presenting the same mistakes. It’s them;

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
|11|warning: data definition has no type or storage class|
|11|warning: type defaults to 'int' in declaration of 'p1' [-Wimplicit-int]|
|11|error: conflicting types for 'p1'|
|10|note: previous declaration of 'p1' was here|
|11|warning: initialization makes integer from pointer without a cast [-Wint-conversion]|
|12|warning: data definition has no type or storage class|
|12|warning: type defaults to 'int' in declaration of 'p2' [-Wimplicit-int]|
|12|error: conflicting types for 'p2'|
|10|note: previous declaration of 'p2' was here|
|12|warning: initialization makes integer from pointer without a cast [-Wint-conversion]|
|14|error: expected declaration specifiers or '...' before '*' token|
|30|error: expected declaration specifiers or '...' before '*' token|
||In function 'main':|
|43|warning: implicit declaration of function 'ler' [-Wimplicit-function-declaration]|
|43|error: expected expression before 'struct'|
|44|warning: implicit declaration of function 'calculo' [-Wimplicit-function-declaration]|
|44|error: expected expression before 'struct'|
||=== Build failed: 6 error(s), 8 warning(s) (0 minute(s), 0 second(s)) ===|
  • What ways you tried?

1 answer

2


Bruno, beauty? the/

Your logic is excellent. Except that the way you defined some things caused certain type incompatibilities. Before we proceed, you really need to work with global variables? If not, let’s recall a cool concept of function:

Variables defined within a function are local variables.

Starting from this statement, we have that the "life time" of a local variable is relative to the time of execution of the function where it was defined. Right. Taking your example, if the pointers to the structures point 1 and point 2 are created in the main and calls from functions read() and calculation() are carried out within this same scope, we have that the respective functions will have access to the necessary information, because the pointers P1 and P2 will exist as long as the main is executed. In theory, it will be possible to assign values to the structures indicated by P1 and P2 and use these values in the calculation of the distance between two points.

Before showing how the modifications in your code were, I highlight some mistakes made:

i. Incorrect declaration of parameters for the function

void ler(struct ponto *p1, *p2)

int calculo(struct ponto *p1, *p2)

At this stage, at least in the C language, it is necessary that the list of parameter names is preceded by their type. In this case, you only did this for the P1 pointer. Even if P2 is of the same type as P1, it is necessary to do this code redundancy.

ii. Unsuitable type specifier

int calculo(struct ponto *p1, *p2)

The function calculation(), at least in its logic, does not return anything to the function that performed its call. Thus, the most appropriate type specifier is the void. If you intend to return the value of the distance between the two points, use the Return.

iii. Incorrect statement of arguments for the function

ler(struct ponto *p1, *p2);

When a function call is performed, it is not necessary to specify the type of arguments. This is only necessary in the declaration of the parameters, which are variables that will receive the copy of the arguments. Ah, and since P1 and P2 are pointers, there is no need to use the * operator in this case.

So now that the bugs have been exposed, your code has gone this way:

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

struct ponto
{
    int x;
    int y;
}ponto1, ponto2;

void ler(struct ponto *p1, struct ponto *p2)
{
    printf("Digite o valor de X1: ");
    fflush(stdin);
    scanf("%d", &p1 -> x);
    printf("Digite o valor de Y1: ");
    fflush(stdin);
    scanf("%d", &p1 -> y);
    printf("Digite o valor de X1: ");
    fflush(stdin);
    scanf("%d", &p2 -> x);
    printf("Digite o valor de Y1: ");
    fflush(stdin);
    scanf("%d", &p2 -> y);
}

void calculo(struct ponto *p1, struct ponto *p2)
{
    int a, b, d;
    a = (*p1).x - (*p2).x;
    b = (*p1).y - (*p2).y;

    d = sqrt(pow(a,2)+pow(b,2));

    printf("%d", d);
}

int main()
{
    struct ponto *p1, *p2;
    p1 = &ponto1;
    p2 = &ponto2;

    ler(p1, p2);
    calculo(p1, p2);
    return 0;
}

I hope I made myself clear, buddy.

A hug =]

  • I got it, thank you very much, you were very clear and objective. I didn’t know you could leave the pointers on main, you expanded my mind. i am since Saturday trying to solve this problem, thank you very much!!!

Browser other questions tagged

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