English 中文(简体)
2D 阵列
原标题:2D array through pointers
  • 时间:2010-09-18 08:54:02
  •  标签:
  • c
  • pointers

我想在点人的帮助下扫描一个2D阵列,并撰写了这一法典,请告诉我,编辑者为什么会犯错误? I know How to use Double pointers to do same, i is conducted with this.

#include<stdio.h>
#include<stdlib.h>
int main(void) {
    int i,j,n,a,b;
    int (*(*p)[])[];
    printf("
	Enter the size of the matrix in the form aXb	
");
    scanf("%dX%d",&a,&b);
    p=(int (*(*p)[b])[a])malloc(b*sizeof(int (*p)[a]));
    for(i=0;i<b;i++) {
            p[i]=(int (*p)[a])malloc(a*sizeof(int));
            printf("	Enter Column %d	
");
            for(j=0;j<a;j++)
                    scanf("%d",&p[i][j]);
    }
    return 0;
}
最佳回答

你们正在使用点数阵列,因此,你应直接将其索引,因为<代码>p[i]将*(p+i),即,在页注之后的阵列,而不是第1页的内容。

在C中,ave*将转换成任何点类型,因此,你无需得出小区的结果。 如果你确实把 cast子放在一边,它就能够掩盖错误,例如,如果你试图指派给非点人(例如<代码>p[i])。

在花名册上,sizeof(int (*p) [a] 要么使用某种类型,要么使用某种表述,要么使用一种声明。 <代码>p是一系列星体的指点器,因此<代码>*p各项要素的类型为

因此,这汇编了无错误的或关于电离的警告:

#include<stdio.h>
#include<stdlib.h>
int main ( void )
{
    int i, j, n, a, b;

    int ( * ( * p ) [] ) [];

    printf ( "
	Enter the size of the matrix in the form aXb	
" );

    scanf ( "%dX%d", &a, &b );

    p = malloc ( b * sizeof ( int ( * ) [] ) );

    for ( i = 0;i < b;i++ ) {
        ( *p ) [i] = malloc ( a * sizeof ( int ) );
        printf ( "	Enter Column %d	
", i );
        for ( j = 0;j < a;j++ )
            scanf ( "%d", & ( * ( *p ) [i] ) [j] );
    }


    return 0;
}

然而,由于在使用一个阵列的点子反对使用其第一个要素的点子方面没有任何好处,但是这确实意味着,在采用该要素之前,你必须加以参照,因此使用点子更容易使用。

问题回答

Do you know what int (*(*p)[])[] is?
Try cdecl.org ... http://cdecl.ridiculousfish.com/?q=int+%28%2A%28%2Ap%29%5B%5D%29%5B%5D

a. 使用1维阵列,并预设2维阵

  1. declare a 1-dimensional object (pointer, array, whatever)
  2. malloc a rectangular size
  3. compute linear addressing value based on row, column, and column size
  4. use it
  5. free the array

......

/* Oh ... and use spaces in your code */
/* They are extremely cheap now a days */
#include <assert.h>
/* instead of asserting malloc and scanf, use proper error checking */
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i, j, n, rows, cols;
    int *p;                                            /* 1. */

    printf("Enter the size of the matrix in the form aXb
");
    n = scanf("%dX%d", &rows, &cols);
    assert((n == 2) && ("scanf failed"));
    p = malloc(rows * cols * sizeof *p);               /* 2. */
    assert((p != NULL) && "malloc failed");
    for (i = 0; i < rows; i++) {
            int rowindex = i * cols;                   /* 3. */
            for (j = 0; j < cols; j++) {
                    n = scanf("%d", &p[rowindex + j]); /* 3. and 4. */
                    assert((n == 1) && "scanf failed");
            }
    }
    free(p);                                           /* 5. */
    return 0;
}

You are unnecessarily complicating the problem of accessing array elements using pointers. Try to use a simple pointer-to-pointer p.

int **p;
...
p=malloc(a*sizeof(int *));   //create one pointer for each row of matrix
...
for(i=0;i<a;i++)
{
...
p[i]=malloc(b*sizeof(int));  //create b integers in each row of matrix
...
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签