【问题标题】:Initializes an integer array issues初始化整数数组问题
【发布时间】:2021-10-11 12:34:51
【问题描述】:

我有一个项目需要以下东西。

用 C 和 MIPS 汇编语言编写一个程序:

  • 初始化一个3行5列的整数数组:

       1   2   3   4   5
       6   7   8   9   10
      11 12 13 14 15
    
  • 输入用户的行号和列号

  • main 调用一个子函数来计算所选行和列的内存地址,如下所示:

      int arrayAddress( int row, int col, int ncolumns, array); //returns address of array[row][col]
    
  • 在适当的地方使用移位而不是乘法

  • 打印所选数组元素的地址和值。

问题是我不知道如何执行以下操作-

获取 int ncolumns,因为键入 int ncolumns = my_array[][5] 会产生错误

删除第二张图片中显示的以下错误

Errors

警告:从不兼容的指针类型“int ()[5]”分配给“int ” [-Wincompatible-pointer-types]

arrayAddress_A = my_array;

警告:格式“%x”需要“unsigned int”类型的参数,但参数 2 的类型为“int *”[-Wformat=]

printf("Memory Address : %x\n", arrayAddress_A);

警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“int *”[-Wformat=]
printf("值: %d", arrayAddress_A);

printf("Value : %d", arrayAddress_A);

可能还有其他我不知道的错误。

我的代码:

#include <stdio.h>
 
int main()
{
  // array declaration and initialization
  int my_array[3][5] = {
    {1 ,2 ,3, 4, 5},      //row 1
    {6 ,7 ,8, 9, 10},      //row 2
    {11, 12, 13, 14, 15},      //row 3
  };
  
  {
      int i = 0;
      int j = 0;
      int ncolumns = 5;
      my_array[i][j];
      printf("Enter row : \n");
      scanf("%d",&i);
      printf("Enter column : \n");
      scanf("%d",&j);
    
  int arrayAddress(int my_array, int i, int j, int ncolumns);
  {
      int* arrayAddress_A;
      arrayAddress_A = my_array;
      arrayAddress_A += i * ncolumns + j;
      printf("Memory Address : %x\n",arrayAddress_A);
      printf("Value : %d", arrayAddress_A);
  }
  
  }
}

【问题讨论】:

  • 请将文本(作业和错误)作为文本复制粘贴到您的问题中。然后在出现错误的行上添加 cmets。
  • 编辑以将您的问题描述包含在帖子正文中。
  • 您应该明白,ncolumns 声明后面的声明my_array[i][j]; 无效。您应该更改或删除它。
  • int arrayAddress(int my_array, int i, int j, int ncolumns);{...} 应该是函数声明吗?
  • 我正在编辑,但是,int arrayAddress 是一个函数

标签: c multidimensional-array


【解决方案1】:

您的帖子中有几个问题需要解决,包括使用正确的方法来传递数组以及在哪里以及如何在其中声明和调用函数。下面是对您的原始代码的改编,并进行了一些修改以说明这些和其他项目。未编译的或本插图不需要的项目被注释掉。

#define ROWS 3//array dimensions 
#define COLS 5

void arrayAddress(int i, int j, int my_array[i][j]);

int main(void)
{
  // array declaration and initialization
  int my_array[ROWS][COLS] = {
    {1 ,2 ,3, 4, 5},      //row 1
    {6 ,7 ,8, 9, 10},      //row 2
    {11, 12, 13, 14, 15},      //row 3
  };
  

    int i = 0;
    int j = 0;
    //int ncolumns = 5;//unknown what purpose was in original
    //my_array[i][j];  //and not essential for this illustration
    printf("Enter Row : \n");
    scanf("%d",&i);
    printf("Enter Column : \n");
    scanf("%d",&j);

    arrayAddress(ROWS, COLS, my_array);
    return 0;
}

//Definition of function prototype above
//illustrates accessing all values in array passed as argument
//via printf statement
void arrayAddress(int rows, int cols, int my_array[rows][cols])
{
    //int* arrayAddress_A;
    //arrayAddress_A = my_array;
    //arrayAddress_A += i * ncolumns + j;
    for(int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {       
            printf("Memory Address : %p\n",&my_array[i][j]);//note use of & (address of operator)
            printf("Value : %d", my_array[i][j]);
        }
    }
}

编辑(仅显示返回指针值(地址))

   int * arrayAddress(int i, int j, int my_array[i][j])
    {
        int *pAddr = NULL;
        pAddr = &(my_array[i][j]);
        return pAddr;          
        
    }

还需要进一步修改 printf 语句,

【讨论】:

  • 好吧,要求说“”main调用一个子函数来计算所选行和列的内存地址,如下所示: - int arrayAddress( int row, int col, int ncolumns, array); //返回数组的地址[row][col] |在适当的地方使用移位而不是乘法“所以 ncolumns 是列的总数。但是你的编码让我对我做错了什么有了很多了解
  • @MeaninglessCode - 好的,很高兴为您提供帮助。要返回数组元素的地址,原型应该是int * arrayAddress(int i, int j, int my_array[i][j]);。然后在函数中创建一个指针变量:int *pAddr = NULL;。将地址分配给指针:pAddr = &amp;(my_array[i][j]);。最后,返回指针值(地址):return pAddr;见帖子编辑。
【解决方案2】:

我不知道它是否对你有帮助,但你可以使用 malloc 函数:

int main ()
{
  int **my_array = malloc(sizeof(int*)*3);
  for(int i=0; i!=3; i++) {
    my_array[i] = malloc(sizeof(int)*5);
  }
}

并且不要忘记在使用您的数组后使用相同的 for 循环“释放()”您的分配! :)

int main()
{
...
  for(int i=0; i!=3; i++) {
    free(my_array[i];
  }
  free(my_array);
}

【讨论】:

  • 教别人使用碎片化的 malloc 对非常小的数组有什么帮助?尽管这里的代码对于任何二维数组都不正确,无论大小如何——这仅仅是分配一维字符串数组的方式。见Correctly allocating multi-dimensional arrays。虽然如果已知尺寸很小,VLA 实际上可能是一个更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 2011-05-25
  • 2012-11-08
相关资源
最近更新 更多