Array parameter passing error

8

I'm trying to understand how pointers work for an array. For this, I elaborated the small program below. I made some mistake because, in the line indicated, segmentation fault is occurring.

What is the correct way to pass the pointer pointing to an array, as I tried in the example?

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

int main()
{
    int i;
    float t1[4][2];
    for (i=0;i<8;i++)
    {
        t1[i%4][i%2]=i+1;
    }
    teste1(t1);
    getchar();
    return 0;
}
void teste1(float ** t1)
{
    int i;
    for (i=0;i<8;i++)
    {
        printf("%f\t",t1[i%4][i%2]);   // Erro nesta linha ("segmentation fault")
    }
}
    
asked by anonymous 14.12.2014 / 17:26

1 answer

9

The problem is in the parameter declaration in the teste1 function. If you are passing a two-dimensional (4X2) array of type float , then the function must be of the same type in the parameter.

In fact the code did not even compile. In addition to not recognizing the teste1 function there was type mismatch in the parameter. You're probably using a compiler that does not meet language standards, so you've been able to compile. Or the actual code you've compiled is different from the one you've posted.

I have inverted the two functions so that teste1 is declared / set before being used. I could also just declare before and set it later but I think the code gets dirtier like that.

I also arranged the calculation of the second index that was wrong as indicated in the comments:

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

void teste1(float t1[4][1]) {
    for (int i = 0; i < 8; i++) {
        printf("%f\t", t1[i % 4 ][i / 4]);
    }
}
int main() {
    float t1[4][2];
    for (int i = 0; i < 8; i++) {
        t1[i % 4][i / 4] = i + 1;
    }
    teste1(t1);
    getchar();
    return 0;
}

See running on ideone .

In the comment was spoken in leave generic. This is possible with arrays if you use a compiler that conforms to the C99 standard (GCC allows until dimension sizes can be passed after the array ). The function header would look like this:

void teste1(int m, int n, float t1[m][n]) {

See running on ideone .

But if you really want to use a pointer as a parameter and you want to generalize, you have to do this:

In this case the teste1 is receiving as a pointer (not a pointer to pointer).

Element position calculation is being done manually since it is being treated with a pointer and not array .

The call also needs to casting to match the array type to pointer.

And there are also parameters to receive the total rows and columns of the array.

I've also used #define to avoid getting lost when you change the array size

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

#define linhas 4
#define colunas 2

void teste1(float * t1, int totalLinhas, int totalColunas) {
    for (int i = 0; i < totalLinhas* totalColunas; i++) {
        printf("%f\t", *((t1 + (i % totalLinhas) * totalColunas) + (i / totalLinhas)));
    }
}
int main() {
    float t1[linhas][colunas];
    for (int i = 0; i < linhas * colunas; i++) {
        t1[i % linhas][i / linhas] = i + 1;
    }
    teste1((float *)t1, linhas, colunas);
    getchar();
    return 0;
}

See working on ideone .

    
14.12.2014 / 17:36