0

How to write this C program pass by pointers and pass by value? I created this program that generates a 10x10 array from -100 to 100 and the main diagonal is read from bottom to top, but I do not know how to create two other programs that are pass by pointers and value I have to write two more programs with the same input and output only through these two methods. I am a beginner in C and I do not know how to make this.

#include <stdio.h>
#include <stdlib.h>

#define N 10
#define M 10

int my_rand(int max, int min)
{
    return (min + rand() / (RAND_MAX / (max - min + 1) + 1));
}

void generate_array(int(*array)[M])
{
    printf("Table:");
    for (int i = 0; i < N; i++)
    {
        printf("\n");
        for (int j = 0; j < M; j++)
        {
            array[i][j] = my_rand(-100, 100);
            printf("%4d ", array[i][j]);
        }
    }
}
void print_diagonal(int(*array)[M])
{
    printf("\n\nThe main diagonal read from bottom to top:\n");
    for (int i = N - 1; i >= 0; i--)
    {
        printf("%4d ", array[i][i]);
    }
}

int *create_array(int(*array)[M])
{
    static int new_array[M] = { 0 };
    for (int i = 0; i < N; i++)
        new_array[i] = array[i][i];
    return new_array;
}

void reverseArray(int arr[], int start, int end)
{
    int temp;
    while (start < end)
    {
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

void printArray(int arr[])
{
    int i;
    for (i = N; i < N; i++)
        printf("%4d ", arr[i]);
    printf("\n");
}

int main(void)
{
    int array1[N][M] = { 0 }, *aux_array;
    generate_array(array1);
    print_diagonal(array1);
    aux_array = create_array(array1);
    reverseArray(aux_array, 0, N - 1);
    printArray(aux_array);
}

Any help would be much appreciated.

Iuliana
  • 1
  • 1
  • 1
    C *only* have pass by value. Pass by reference can be *emulated*. – Some programmer dude Apr 01 '18 at 15:06
  • 2
    And *please* try to format your code properly. Indentation is really good for us humans to understand your code. – Some programmer dude Apr 01 '18 at 15:07
  • As John said, in C you always pass by value. But you can make a distinction between passing the value itself or the pointer to the value. In your question you say you have your code and you need to write two other versions of the code passing arguments by value and by pointer. First, is important to notice that you have to be already using one if these two methods. Your question makes me think you don't understand that. Then, you have to know that in C you always pass the arrays by passing a pointer to its first element (the value of the pointer). Then you will know better what you want. – myradio Apr 02 '18 at 07:45

1 Answers1

0

C only supports pass by value. The formal argument in the function definition is always a different object in memory from the actual argument in the function call (assuming the actual argument is itself an object, which it doesn't have to be).

We can fake pass-by-reference semantics by passing a pointer (which is passed by value). Here's a typical example:

void swap( int *a, int *b )
{
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

Instead of receiving to integer objects, we receive pointers to two integer objects. We use the unary * operator to dereference the pointers and access the pointed-to objects. If we call that function like so:

int main( void )
{
  int x = 1;
  int y = 2;

  ...
  swap( &x, &y );
  ... 
  return 0;
}

Given the above code, the following are true when we enter the swap function:

 a == &x        // int *
*a ==  x  == 1  // int

 b == &y        // int *
*b ==  y  == 2  // int

After we execute the body of the swap function, the following are true:

 a == &x        // no change
*a ==  x  == 2

 b == &y        // no change
*b ==  y  == 1

Where things get weird is when you pass array expressions as function arguments. C has a rule that when an array expression is not the operand of the sizeof or unary & operators, or is not a string literal used to initialize a character array in a declaration, the expression is converted ("decays") from an array type to a pointer type, and the value of the expression is the address of the first element.

IOW, given the code

int arr[10];

foo( arr );

Since it's not the operand of the sizeof or unary & operators, the expression arr in the call to foo is automatically converted from type "10-element array of int" to "pointer to int", and the value that gets passed to foo is the address of the first element; IOW, that call is identical to writing

foo( &arr[0] );

and the function definition would be written as

void foo( int *a ) // or int a[], which means the same thing
{
  // do something with a[i]
}

IOW, you cannot pass arrays by value in C.

So, looking at your code:

int my_rand(int max, int min)

Both max and min are being passed by value

void generate_array(int(*array)[M])

void print_diagonal(int(*array)[M])

These are receiving a pointer to the first element of a 2D array, so we're faking pass-by-reference.

void reverseArray(int arr[], int start, int end)

We're receiving a pointer to first element of the array (T a[] and T a[N] are both interpreted as T *a in a function parameter declaration), start and end are being passed by value.

void printArray(int arr[])

Again, we're receiving a pointer to the first element of the array.

Community
  • 1
  • 1
John Bode
  • 119,563
  • 19
  • 122
  • 198