简体   繁体   English

代码似乎正确,但是程序在打印数组的第一行后挂起

[英]Code seems correct but the program hangs after printing the first row of the array

I am trying to implement matrix using pointer. 我正在尝试使用指针实现矩阵。 The code seems correct but the program hangs after printing the first row of the array. 该代码似乎正确,但是程序在打印数组的第一行后挂起。

int main ()
    {
        int **a;
        int i,j;
        int count = 1;

        for (i = 0; i < 4; i++)
        {
            for(j = 0; j < 4; j++)
            {
                *(*(a + i) + j) = count;
                count += 1;
                printf("%d\t", *(*(a + i) + j));
            }
        }
    }

This will work! 这将起作用!

#include<stdio.h>
#include<stdlib.h>
int main ()
{
    int **a;
    int i,j;
    int count = 1;
    // Allocate memory and populate 
    a = malloc(4*sizeof(int*));
    for (i = 0; i < 4; i++)
    {
        a[i] = malloc(4*sizeof(int));
        for(j = 0; j < 4; j++)
        { 
            *(*(a + i) + j) = count;
            count += 1;
            printf("%d\t", *(*(a + i) + j));
        }
        printf("\n");
    }
    //Now free the allocated memory
    for ( i = 0; i < 4; i++)
    {
        free(a[i]);
    }
    free(a);
    return 0;
}

**a is just a pointer to pointer, however in your code you have not specified where this pointer should point to. **a只是一个指向指针的指针,但是在您的代码中,您尚未指定该指针应指向的位置。

First you need to allocate memory before using it and then perform the desired operation (populate, read, write etc) and once you no longer require it, free the allocated memory. 首先,您需要在使用内存之前分配内存,然后执行所需的操作(填充,读取,写入等),然后,当不再需要它时,请释放已分配的内存。

For the code to stand a chance of working, you must allocate space for the array, and space for the pointers. 为了使代码有机会工作,您必须为数组分配空间,并为指针分配空间。 For example: 例如:

#include <stdio.h>

int main(void)
{
    int   m[4][4];
    int  *p[4] = { m[0], m[1], m[2], m[3] };
    int **a = p;
    int count = 1;

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            *(*(a + i) + j) = count++;
            printf("%d\t", *(*(a + i) + j));
        }
        putchar('\n');
    }
    return 0;
}

There are other ways of specifying m and p . 还有其他指定mp For example: 例如:

int m[16];
int *p[4] = { &m[0], &m[4], &m[8], &m[12] };

What is missing from your code is the allocation of memory. 您的代码缺少的是内存分配。 When you say int** a; 当你说int** a; you only get the double pointer variable, no space for the pointer that it should point to, and no space for the integer that the intermediate pointer should point to. 您只会得到双指针变量,它应该指向的指针没有空格,中间指针应该指向的整数也没有空格。 Dereferencing such an uninitialized pointer is undefined behavior. 取消引用此类未初始化的指针是未定义的行为。

Now, the easiest way to allocate a matrix in C, is to allocate an array of arrays like this: 现在,在C中分配矩阵的最简单方法是分配这样的数组数组:

int main() {
    int n = 4, count = 1;
    int (*matrix)[n] = malloc(n*sizeof(*matrix));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            matrix[i][j] = count++;
        }
    }
    free(matrix);
}

malloc() allocates the memory, free() gives it back so that it can be reused. malloc()分配内存, free()返还内存,以便可以重用。 matrix is a pointer to a line array of the matrix, and the malloc() call allocates enough memory for n elements of whatever matrix points to (that is n line arrays of n elements in this case). matrix是指向matrix的行数组的指针, malloc()调用为matrix指向的n元素分配足够的内存(在这种情况下, n元素的n n行数组)。 You can't do the allocation more concise. 您不能更简洁地进行分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM