Vector return and matrix of a function in c!

Asked

Viewed 1,109 times

1

The problem I’m having is that my serial pointer is all null, and the rest of the code runs good, I really can’t find what the problem is! If anyone can help me I appreciate it already!! Obs.: There are some more blibiotecas for use in other codes that n has no connection with the one in question!

main:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "repository.h"

int main()
{
    int *serieoriginal;
    int *serieinversa;
    unsigned int i, j;
    serieoriginal = (int*) calloc(12, sizeof(int));
    serieinversa = (int*) calloc(12, sizeof(int));

    printf("Bem vindo ao modo de musica Dodecafonico!\n\n");

    printf("Digite os valores da serie!\n");
    Sleep(300);
    for (i = 0; i < 12; i++) {
        printf("  %d valor: ", i+1);
        scanf("%d", &serieoriginal[i]);
    }

    *serieinversa = SeInv(serieoriginal);

    printf("\nSerie original: ");
    for (i = 0; i < 12; i++) {
        printf("%d ", serieoriginal[i]);
    }
    printf("\nSerie inversa: ");
    for (i = 0; i < 12; i++) {
        printf("%d ", serieinversa[i]);
    }

    return 0;
} 

blibioteca Repository. h:

#ifndef REPOSITORY_H_
#define REPOSITORY_H_

int* SeInv(int* s);

#endif  

Repository. c:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

int* SeInv(int *s)
{
    int P[12]/*serie transposta*/, *R[12]/*serie retrogradada*/;
    int i, k, aux;

    srand( (unsigned)time(NULL) );
    k = 5;//rand()%12;
    printf("\n K: %d\n", k);
    printf("\nSerie: ");
    for (i = 0; i < 12; i++) {
        printf("%d ", s[i]);
    }
    for (i = 0; i < 12; i++) {
        aux = s[i];
        aux = (aux + k)%12;
        P[i] = aux;
    }
    printf("\nP: ");
    for (i = 0; i < 12; i++) {
        printf("%d ", P[i]);
    }
    k = 0;
    for (i = 12; i > 0; i--) {
        R[k] = P[i-1];
        k++;
    }
    printf("\nR: ");
    for (i = 0; i < 12; i++) {
        printf("%d ", R[i]);
    }
    return (*R);
}
  • Why the c++ and c tags#?

  • Young, C, C++ and C# are not the same thing.

  • I put it only to have a wider range of people who could help me!

1 answer

1

I fixed the return of the method, as for the order of the values, I do not know if it should return the exact inverse of the original, or some order in its calculations.

First on the file main.c the line:

serieinversa = (int*) calloc(12, sizeof(int));

There is no need to allocate a size to the pointer, as it will receive an address from a method later on. In this case the ideal is:

serieinversa = NULL;

On the line:

*serieinversa = SeInv(serieoriginal);

The method SeInv returns an integer pointer, its variable serieinversa is already an integer pointer, in the case of code, you are trying to store the returned address within another address. The correct one would be.

serieinversa = SeInv(serieoriginal);

In the Repository. c file, align:

int P[12]/*serie transposta*/, *R[12]/*serie retrogradada*/;

This declaring a pointer of a 12-position vector *R[12], the ideal is, to use only vector or only pointer, in the case of the code snippet, I would say that it looks more like a matrix, I switched to:

int P[12]/*serie transposta*/;
int *R = (int *) malloc(sizeof(int)*12);

Since R is the sei returns, it must be a pointer to not have incompatibility with your method and variable that will receive it.


And finally the return of SeInv:

return (*R);

You’re returning the vector that was in R, but the values are not within the vector within R, and yes in R. That’s why you must return only the R:

return R;

I suggest we revise the inversion calculation to see if it’s really correct.

  • Thanks buddy, it was really the allocation of the R pointer in the Seivn() function that was causing the problem, and referring to the inversion calculation, it’s not just an inversion, it has more stuff, but my problem was just the return of the msm function, thank you very much!

  • @Guilhermegehlen, if the answer solved your problem, you can mark the "V" on the answer side to indicate that it is solved

Browser other questions tagged

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