0

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);
  }
  
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please copy-paste text (both assignment and errors) *as text* into your question. Then add comments on the lines where you get the errors. – Some programmer dude Oct 11 '21 at 12:39
  • Edit to include your problem description into the body of the post. – ryyker Oct 11 '21 at 12:40
  • You should understand that the statement `my_array[i][j];` which follows the declaration of `ncolumns` has no effect. You should either change it or delete it. – Tom Karzes Oct 11 '21 at 12:43
  • Is `int arrayAddress(int my_array, int i, int j, int ncolumns);{...}` supposed to be a function declaration? – ryyker Oct 11 '21 at 12:44
  • I am in the process of editing but yeah, int arrayAddress is a function – Meaningless Code Oct 11 '21 at 12:46
  • To summarize, there's many problematic things in the code, making it seem like you're trying to get ahead of your actual knowledge. Please slow down, perhaps even take a few steps back, and go back to the early and basic chapters of your text-books. – Some programmer dude Oct 11 '21 at 12:47
  • None of this code makes any sense. You can't program by typing random stuff through trial & error until something seems to work. You actually have to know what every line you type does. – Lundin Oct 11 '21 at 12:48

2 Answers2

0

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);
}
TFC
  • 59
  • 10
  • How is teaching someone to use fragmented malloc for very small arrays helpful? Though this code here isn't correct for any 2D array no matter size - this is merely how you'd allocate a 1D array of strings. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). Though a VLA might actually be a much better solution here if the dimensions are known to be small. – Lundin Oct 11 '21 at 13:16
0

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,

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • well the requirements said " "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 " so ncolumns was the total amount of columns. but your coding has given me a lot of insight on what I did wrong – Meaningless Code Oct 11 '21 at 14:03
  • @MeaninglessCode - Okay, glad to help with that. To return address of an array element, prototype should be `int * arrayAddress(int i, int j, int my_array[i][j]);`. then in function create a pointer variable: `int *pAddr = NULL;`. Assign the address to the pointer: `pAddr = &(my_array[i][j]);`. Finally, return the pointer value (address): `return pAddr;` See edit in post. – ryyker Oct 11 '21 at 14:54