首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

高手们帮忙看下异常的地方。

2013-07-01 
高手们帮忙看下错误的地方。。。。。。以下为代码:#includestdio.hint main(void){ void move(int *array) in

高手们帮忙看下错误的地方。。。。。。
以下为代码:
#include<stdio.h>
int main(void)
{
 void move(int *array);
 int i,j,a[3][3],*p=a[0];
 printf("input 9 numbers:");
 for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
 scanf("%d",*(p+i)+j);
 }
 for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
 printf("%d",*(*(p+i)+j));
 printf("\n");
 }
  move(p);
  printf("now array:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d",*(*(p+i)+j));
    printf("\n");
}
return 0;
}
void move(int *array)
{
 int i,j,t;
 for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
 {  
 t=*(*(array+i)+j);
 *(*(array+i)+j)=*(*(array+j)+i);
 *(*(array+j)+i)=t;
 }
 }
}
错误提示:
c(15) : error C2100: illegal indirection
c(23) : error C2100: illegal indirection
c(35) : error C2100: illegal indirection
c(36) : error C2100: illegal indirection
c(36) : error C2100: illegal indirection
c(36) : error C2106: '=' : left operand must be l-value
c(37) : error C2100: illegal indirection
c(37) : error C2106: '=' : left operand must be l-value
我是新手,不知道怎么改,帮帮谢谢!!!!!! C
[解决办法]
1. 用指针实现对3*3矩阵的访问时,前一个下标要乘以3;
2. 如果函数move是为了实现矩阵转置,那么for(j=0;j<3;j++)要改为for(j=i+1;j<3;j++)。

#include<stdio.h>
int main(void)
{
    void move(int *array);
    int i,j,a[3][3],*p=a[0];
    printf("input 9 numbers:");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            scanf("%d", (p+i*3)+j);//scanf("%d", (p+i)+j);


    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d",*((p+i*3)+j));//printf("%d",*((p+i)+j));
        printf("\n");
    }
    move(p);
    printf("now array:");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d",*((p+i*3)+j));//printf("%d",*((p+i)+j));
        printf("\n");
    }
    return 0;
}

void move(int *array)
{
    int i,j,t;
    for(i=0;i<3;i++)
    {
        for(j=i+1;j<3;j++)//for(j=0;j<3;j++)
        {
            t=*((array+i*3)+j);//t=*((array+i)+j);
            *((array+i*3)+j)=*((array+j*3)+i);//*((array+i)+j)=*((array+j)+i);
            *((array+j*3)+i)=t;//*((array+j)+i)=t;
        }
    }
}


[解决办法]
仅供参考
//在堆中开辟一个4×5的二维int数组
#include <stdio.h>
#include <malloc.h>
int **p;
int i,j;
void main() {
    p=(int **)malloc(4*sizeof(int *));
    if (NULL==p) return;
    for (i=0;i<4;i++) {
        p[i]=(int *)malloc(5*sizeof(int));
        if (NULL==p[i]) return;
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            p[i][j]=i*5+j;
        }
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            printf(" %2d",p[i][j]);


        }
        printf("\n");
    }
    for (i=0;i<4;i++) {
        free(p[i]);
    }
    free(p);
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19

热点排行