C - 无法打印动态数组值

C - Can't print dynamic array values

对于学校(是的,学校项目)我需要改编一个 C 程序... 我需要用 txt 文件中的值创建一个数组(我认为它是正确完成的)。 现在我想打印这些值,这就是问题所在!我尝试了很多方法,但我总是看到内存地址。 这是代码:

int* init_dados(char *nome,int *m, int *n, int *iter)
{
        FILE *f;
        int *p, *q;
        int i, j,k,contador=0,lixo=0,aux=0,flag=0;

        f=fopen(nome, "r");
        if(!f)
        {
                printf("Erro no acesso ao ficheiro dos dados\n");
                exit(1);
        }

        fscanf(f, " %d %d", m,n);

        p = malloc(sizeof(int)*(*m)*(*n));
        if(!p)
        {
            printf("Erro na alocacao de memoria\n");
            exit(1);
        }

        q=p;

        for (i = 0; i < *m; i++)
        {
                for (j = 0; j<*n; j++)
                { 
                        //se ainda nao leu nada
                        if (flag == 0)
                        {
                                for (contador = 0; contador < *n; contador++)
                                {
                                        fscanf(f, "%d", &lixo);
                                }
                                flag = 1;
                                break;
                        }

                        if (flag == 1)
                        {
                                fscanf(f, " %d", &k);
                                break;
                        }

                        for (contador = 0; contador < k; contador++)
                        {
                                fscanf(f, " %d", q++);
                        }
                }
        }

        //PRINTING CODE
        for (i = 0; i < *m; i++)
        {
                printf("\n");
                for (j = 0; j < *n; j++)
                {
                        printf("%d   ", &q[j]);
                        q++;
                }
        }

        fclose(f);

        return p;
  }

等待您的想法,谢谢!

编辑: @iharob 我已经改变了这个:

for (contador = 0; contador < k; contador++)
            {
                fscanf(f, " %d", q++);
            }

for (i = 0; i < *m; i++)
    {
        printf("\n");
        for (j = 0; j < *n; j++)
        {
            printf("%d   ", p[j]);
            p++;
        }
    }

仍然无法正常工作

编辑2: 文件:

10  10
1   1   1   1   1   1   1   1   1   1   
2
1   8   
2
5   6   
4
1   2   3   4   
1
1   
4
1   2   5   8   
2
6   10  
1
9   
4
1   2   3   5   
1
8   
1
7   

到目前为止打印的结果:

这是错误的

printf("%d   ", &q[i]);

将其更改为 p[i] 而不是 &q[i]

printf("%d   ", p[i]);

当你到达printf("%d ", q[i])时,q指向数组的末尾,所以q[0] == q[lengthOfQ]也就是过去q,你赋值q = p;保留p 指向数组的开头,因此您应该在 printf("%d ", q[i]); 中使用 p 而不是 q.

我想这段代码一定是你需要的

int *init_dados(char *nome,int *m, int *n, int *iter)
{
    FILE *f;
    int *p, *q;
    int i, j, k, contador = 0, lixo = 0, flag = 0;

    f = fopen(nome, "r");
    if (f == NULL)
    {
        printf("Erro no acesso ao ficheiro dos dados\n");
        exit(1);
    }
    fscanf(f, " %d %d", m, n);

    p = malloc(sizeof(int) * *m * *n);
    if (p == NULL)
    {
        fclose(f);
        printf("Erro na alocacao de memoria\n");

        exit(1);
    }

    q = p;
    for (i = 0; i < *m; i++)
    {
        for (j = 0 ; j <* n ; j++)
        {
            //se ainda nao leu nada
            if (flag == 0)
            {
                for (contador = 0 ; contador < *n ; contador++)
                    fscanf(f, "%d", &lixo);
                printf("----\n");
                flag = 1;

                break;
            }
            else if (flag == 1)
            {
                fscanf(f, " %d", &k);
                flag = 2;
                break;
            }
            else if (flag == 2)
            {
                for (contador = 0 ; contador < k ; contador++)
                    fscanf(f, " %d", q++);
                }
                flag = 1;
            }
        }
    }

    //PRINTING CODE
    for (i = 0; i < *m; i++)
    {
        for (j = 0; j < *n; j++)
            printf("%d   ", p[j]);
        printf("\n");
    }
    fclose(f);

    return p;
}

此代码:

for (contador = 0; contador < k; contador++)
{
    fscanf(f, " %d", q++);
}

永远不会被执行。

是一个循环代码块,驱动力为'flag',flag只设置为0和1,0和1的情况都退出了整个'for'循环.

您是否转储了生成的 'p' 数组以确保值正确?

当运行这个程序的时候,你有没有注意到这段代码从来没有执行过?

this code:

for (i = 0; i < *m; i++)
{
    printf("\n");
    for (j = 0; j < *n; j++)
    {
        printf("%d   ", p[j]);
        p++;
    }
}

has the problem that 'p' should not be incremented.
for two reasons:
1) need to keep pointer to malloc'd memory
2) the variable 'j' is indexing off of 'p' so no need to increment 'p'
the following code compiles, but does raise a compiler warning about unused paramter
the code implements the OPs requirements

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

// the 'iter' parameter is not used, raises compiler warning,
// suggest adding code to use it or remove that parameter
int* init_dados(char *nome,int *m, int *n, int *iter)
{
    FILE *f = NULL;
    int *p = NULL; // ptr to malloc'd memory
    if( NULL == (p = malloc(1) ) )
    { // then, malloc failed
        perror( "malloc failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    int *q = p; // steps into malloc'd memory

    int j; // group loop index
    int k; // group data size
    int contador=0; // read loop counter
    int lixo=0;   // read and discard work area



    if(NULL == (f=fopen(nome, "r") ) )
    { // then, fopen failed
        perror("fopen failed" );
        printf("Erro no acesso ao ficheiro dos dados\n");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    if( 2 != (fscanf(f, " %d %d", m,n) ) )
    { // then, fscanf failed
        perror( "fscanf failed for first line of file" );
        free(p);
        exit( EXIT_FAILURE );
    }

    // implied else, fscanf for m, n successful

    //se ainda nao leu nada
    for (contador = 0; contador < *n; contador++)
    {
        if( 1 != fscanf(f, " %d", &lixo) )
        { // then, fscanf failed for throwaway data
            perror("fscanf failed for throwaway data line" );
            free(p);
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful
    } // end for


    // for each data group
    for (j = 0; j<(*n); j++)
    {
        if( 1 != fscanf(f, " %d", &k) )
        { // then, fscanf failed
            perror( "fscanf failed for count of data in group" );
            free(p);
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        // input data from data group, with echo
        printf("\nGroup Number: %d with data count: %d\n", j, k);
        for (contador = 0; contador < k; contador++, q++)
        {
            if( 1 != fscanf(f, " %d", q) )
            { // then, fscanf failed
                perror( "fscanf failed for data entry in data group" );
                free(p);
                exit( EXIT_FAILURE );
            }

            // implied else, fscanf successful

            printf("%3d   ", *q);
        } // end for
    } // end for

    fclose(f);

    return p;
} // end function: init_dados