1

The problem is to create a function that prints out n numbers of the array and their addresses. Function should be called 3 times in the main. The function can only have two parameters. 1. it will print all members of the array 2. it will print elements from 0-4 3. it will print elements 3-9

I have made a body of the function but I am struggling with following the condition of having only two parameters. One of them is already used to pass the array to the function so there is only one left.

#include <stdio.h>
#define size 10

void Input (int arr[]);
void AdressOutput (int arr[]);

int main(void)
{
    int arr[size];
    Input(arr);
    AdressOutput(arr);
return 0;
}
void Input (int arr[])
{
    int i;
    int *p=arr;
    for (i=0;i<size;i++)
    {
        printf ("Please enter a value %d - ",i);
        scanf ("%d",(p+i));
    }
    printf ("\n");

}
void AdressOutput (int arr[])
{
    int i=0;
    int *p=arr;
    for (i=0;i<size;i++)
    {
        printf ("%p,%d\n",(p+i),*(p+i));
    }
}

The only thing I was able to come up with is to create two more parameters (plus array) starting and ending variables which can be different every time I call the function, but somehow I only need to keep it only with two parameters.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
MaGK
  • 17
  • 4

2 Answers2

4

It is enough to declare the function like

void AdressOutput( const int arr[], size_t n );

and call it like

1. AdressOutput( arr, size );
2. AdressOutput( arr, 5 );
3. AdressOutput( arr + 3, size - 3 );

Here is a demonstrative program

#include <stdio.h>

#define size    10

void AdressOutput( const int arr[], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%p: %d, ", ( const void * )( arr + i ), *( arr + i ) ); 
    }

    putchar( '\n' );
}

int main(void) 
{
    int arr[size] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    AdressOutput( arr, size );
    AdressOutput( arr, 5 );
    AdressOutput( arr + 3, size - 3 );

    return 0;
}

Its output is

0x7ffc1cfd62f0: 0, 0x7ffc1cfd62f4: 1, 0x7ffc1cfd62f8: 2, 0x7ffc1cfd62fc: 3, 0x7ffc1cfd6300: 4, 0x7ffc1cfd6304: 5, 0x7ffc1cfd6308: 6, 0x7ffc1cfd630c: 7, 0x7ffc1cfd6310: 8, 0x7ffc1cfd6314: 9, 
0x7ffc1cfd62f0: 0, 0x7ffc1cfd62f4: 1, 0x7ffc1cfd62f8: 2, 0x7ffc1cfd62fc: 3, 0x7ffc1cfd6300: 4, 
0x7ffc1cfd62fc: 3, 0x7ffc1cfd6300: 4, 0x7ffc1cfd6304: 5, 0x7ffc1cfd6308: 6, 0x7ffc1cfd630c: 7, 0x7ffc1cfd6310: 8, 0x7ffc1cfd6314: 9, 

If to use two pointers as the parameters then the function definition will look like

void AdressOutput( const int *first, const int *last )
{
    for ( ; first != last; ++first )
    {
        printf( "%p: %d, ", ( const void * )first, *first ); 
    }

    putchar( '\n' );
}

and the function can be called like

1. AdressOutput( arr, arr + size );
2. AdressOutput( arr, arr + 5 );
1. AdressOutput( arr + 3, arr + size );

That is the range is specified as [first, last)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • `first != last` as `first <= last` has the advantage of being more tolerate to `last < first`. Still nice answer. – chux - Reinstate Monica Sep 16 '19 at 19:26
  • Can you please explain this "size_t n"? I don't fully understand how it works, can I just create for example a variable and change its value every time I call the function? – MaGK Sep 17 '19 at 14:06
  • @MaGK size_t it is an unsigned integer type that is used to specify sizes of arrays of strings and are used as indices. For example the size int can be too small to keep the size of a string. – Vlad from Moscow Sep 18 '19 at 10:20
0

pass pointers to the first and last element

void printrange(int *lo, int *hi) {
    while (lo <= hi) {
        printvalue(*lo);
        printaddr(lo);
        lo++;
    }
}

and call according to your needs

printrange(&arr[0], &arr[9]);
printrange(arr, arr+4);
printrange(arr+3, arr+9);
pmg
  • 106,608
  • 13
  • 126
  • 198