如何使用指针添加2d数组 [英] how to add 2d arrays using pointers

查看:74
本文介绍了如何使用指针添加2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
void main()
{
    int numArray[10];
    int i,sum=0;
    int *ptr;
    printf("\nEnter 10 elements:");
    for(i=0;i<10;i++)
    scanf("%d",&numArray[i]);
    ptr=numArray;
    for(i=0;i<10;i++)
    {
        sum=sum+ *ptr;
        ptr++;
    }
    printf("The sum of array elements:%d",sum);
    getch();
}



i已经完成了这个,但我不知道如何使用2d数组...请帮助..


i have done this but i dont know how to do it with 2d array...please help..

推荐答案

除了你有两个坐标以外,大致相同。根据行和列来考虑它。因此,具有三行五列的数组将类似于:

Much the same except you have two co-ordinates. Think of it in terms of rows and columns. So an array with three rows and five columns would be something like:
#define NUM_ROWS 3
#define NUM_COLS 5
int array[NUM_ROWS][NUM_COLS];
int row, column;
for (row = 0; row < NUM_ROWS; row++)
{
    for (column = 0; column < NUM_COLS; column++)
    {
        array[row][column] = /* add value to this row and column of array */
    }
}


这篇关于如何使用指针添加2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆