1

I am a C++ beginner and my task is as follows:

Define and initialise a single-dimensional integer array. Next, define a pointer that points to the first element in the array and passes the pointer to a function.

Using only pointer variables (and looping constructs), print only the array values that are exact multiples of 7 from start to finish to standard output. The only program output should be the numbers, one per line with no white space.

I have tried:

void print_sevens(int* nums, int length)
{
    int array[5] = { 5,8,21,43,70 };
    int* ptr = array;
    int* num = array;
    for (int i = 0; i < length; i++)
    {
        *num++;
        if (num[i] % 7 == 0) {
            cout << num[i] << endl;
        }
    }
}

int main()
{
    int array[5] = { 5,8,21,43,70 };
    int* ptr = array;
    print_sevens(ptr, 5);
}

It compiles but does not output anything.

I am also confused about passing the pointer to a function. Should this be done in the main file or in the function file?

7
  • 3
    Why do you create a new array inside the function? Commented Aug 21, 2019 at 23:36
  • @Aziz the function doesn't allow me to pass an array to it as a parameter, so I wasnt sure where to put the array Commented Aug 21, 2019 at 23:39
  • You are actually passing an array (as a pointer) as a parameter to the function print_sevens. You don't need to create a new array inside the function. Commented Aug 21, 2019 at 23:45
  • @Aziz ohh okay thanks Commented Aug 21, 2019 at 23:49
  • 1
    Which question are you asking: the one about no output or the one about passing the pointer to the function? Commented Aug 22, 2019 at 0:07

2 Answers 2

3

You are creating an additional array in the print_sevens function, which is unnecessary as you already passed the pointer to the first element of the array created in the main()(i.e. array).

Removing that unnecessary array and related codes from the function will make the program run perfectly. (See online)

void print_sevens(int* nums, int length) 
{
    for (int i = 0; i < length; i++) 
    {
        if (nums[i] % 7 == 0) 
            std::cout << nums[i] << std::endl;
    }
}

and in the main you only need to do the follows, as arrays decay to the pointer pointing to its first element.

int main() 
{
    int array[5]{ 5,8,21,43,70 };
    print_sevens(array, 5);
}

Note that,

Sign up to request clarification or add additional context in comments.

Comments

-1

You are modifying the contents of your array when you do *num++.

3 Comments

i tried allocating a pointer to the *num variable then incrementing and printing that out but it just prints memory addresses
*num++ is just defeference pointer, then increment pointer. Since nothing is being done to the dereferenced pointer the asker could discard the defererence and not notice a thing. All that code's doing is advancing the pointer. Demo: ideone.com/z19w0Q
That is not causing the problem, but it is a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.