Statement of Pointers in a struct and calling it in function

1

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

#include<stdio.h>#include<stdlib.h>#include<math.h>structponto{intx;inty;}ponto1,ponto2;structponto*p1,*p2;p1=&ponto1;p2=&ponto2;voidler(structponto*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 it in so many different ways and it still has the same errors. 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)) ===|
    
asked by anonymous 18.05.2018 / 13:50

1 answer

2

Bruno, beauty? \ o /

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

  

Variables defined within a function are local variables.

Starting from this statement, we have that the "lifetime" of a local variable is relative to the execution time of the function where it was defined. Right. Taking your example, if the pointers to the dot1 and dot2 structures are created in the main and the read and > are performed within that same scope, we have that the respective functions will have access to the necessary information, since the pointers p1 and p2 will exist while the main is executed. In thesis, it will be possible to assign values to the structures pointed out by p1 and p2 and to use these values in calculating the distance between two points.

Before showing how the changes in your code were, I highlight some errors:

i. Incorrect statement of parameters for function

void ler(struct ponto *p1, *p2)

int calculo(struct ponto *p1, *p2)

In this step, at least in Language C, it is necessary that the list of parameter names be 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. Inappropriate type specifier

int calculo(struct ponto *p1, *p2)

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

iii. Incorrect statement of arguments for function

ler(struct ponto *p1, *p2);

When calling a function, 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. Oh, 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 looks like this:

#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 it has been clear, my friend.

A hug =]

    
18.05.2018 / 15:16