Home > Software engineering >  Initializes an integer array issues
Initializes an integer array issues

Time:10-12

I have a project that requires the following things.

Write a program in C and in MIPS assembly language program that:

  • Initializes an integer array with 3 rows and 5 columns:

       1   2   3   4   5
       6   7   8   9   10
      11 12 13 14 15
    
  • Inputs a row and column number from the user

  • main calls a child function that calculates the memory address of the chosen row & column like this:

      int arrayAddress( int row, int col, int ncolumns, array); //returns address of array[row][col]
    
  • use shift instead of multiply where appropriate

  • print the address and the value of the chosen array element.

The problem is I don't know how to do the following -

Get int ncolumns since typing int ncolumns = my_array[][5] produces errors

Remove the following errors shown in the second image

Errors

warning: assignment to ‘int ’ from incompatible pointer type ‘int ()[5]’ [-Wincompatible-pointer-types]

on

arrayAddress_A = my_array;

Warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]

on

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

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Value : %d", arrayAddress_A);

on

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

And there might be other errors I am not aware of.

The code I have:

#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);
  }
  
  }
}

CodePudding user response:

I don't know if it helps you, but you can use the malloc function :

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

And don't forget to "free()" your alloc using the same for loop after using your array ! :)

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

CodePudding user response:

There are several problems to address in your post including using a proper method to pass an array and where and how to declare and call a function among them. Below is an adaptation of your original code with some modifications that illustrate these, and other items. Those items that do not compile, or are not needed for this illustration are commented.

#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]);
        }
    }
}

Edit (only to show returning a pointer value (address))

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

Modify further printf statement are needed as well,

  • Related