H
H
HentaiEtoIskusstvo2017-09-03 19:26:16
C++ / C#
HentaiEtoIskusstvo, 2017-09-03 19:26:16

Why can't I pass a pointer to a function?

#include<iostream>
#include<time.h>

using namespace std;

void Function1(int* ptr, int& size);

void main() {
  setlocale(LC_ALL, "Rus");
  int* ptr = nullptr;
  int size = 10;
  Function1(ptr, size);
  cout << endl;
}
void Function1(int* ptr, int& size) {
  for (int i = 0; i < size; ++i) {
    ptr[i] = rand() % (20 - 0 + 1) - 0;
    cout << ptr[i] << ' ';
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2017-09-03
@HentaiEtoIskusstvo

You forgot to allocate memory and put the pointer to the allocated area in ptr
Before calling function1, add
ptr = new int[size]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question