Return of vector and matrix of a function in c!

1

The problem I'm having is that my serial-inverter pointer gets all null, and the rest of the code works fine, I really do not see what the problem is! If somebody can help me, I thank you right away! Note: You have a few more libraries to use in other codes that have no connection with this one!

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);
}
    
asked by anonymous 06.04.2016 / 00:19

1 answer

1

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

First of all in the file main.c line:

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

You do not need to allocate a size for the pointer, since it would have been given a forward method address. In this case the ideal is:

serieinversa = NULL;

On line:

*serieinversa = SeInv(serieoriginal);

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

serieinversa = SeInv(serieoriginal);

In the repository.c file, align:

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

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

As R is the return value, it must be a pointer to have no incompatibility with its method and variable that will receive it.

And finally the return of *R[12] :

return (*R);

You are returning the vector that was in SeInv , but the values are not within the vector within R , but in R . This is why you should only return R :

return R;

I suggest giving a revision in the inversion calculation to see if it is really correct.

    
06.04.2016 / 01:14