+ 1
How can i pass a pointer to a function - C?
i have a homework about dynamic allocation of memory, and one of the most important parts is to create a vector of numbers until the user wanna stop adding numbers, one by one, and i have to do it in a function but when i try pass the parameters to te function and run the program it crashes, so i wanna know how to pass and use a poniter in a function
2 Réponses
+ 4
Can you maybe post your code here?
Usually, to pass a pointer as an argument, you would do it like this:
void doSomething(int *pointer) {}
Simply specifiy a pointer as an formal argument, so the function accepts a pointer as its argument upon call.
+ 3
The easiest way to get an address is with address operator see line 6. You can also use new see 7, which is my first choice. Post code here and I can point out errors.
1 #include <iostream>
2 using namespace std;
3 void test(int *adr) {}
4 int main() {
5 int data;
6 test(&data);
7 int *ptr = new int;
8 test(ptr);
9 return 0;
0}