0

I have a program here:

#include<iostream>

using namespace std;

void f1(int* x){
  for (int i = 0; i < sizeof(x); i++) {
    cout<<x[i]<<endl;
  }
}

void f2(int* x) {
  cout<<x<<endl;
}

int main() {
 int a=3;
 int a1[3]={2,3,1};
 f1(a1);
 f2(a);    
}

The function f1 allows me to pass an array as the argument to function but does not allow an integer variable to be passed as an argument.

How is this justified? Does this have anything with the fact that the pointer takes 8 bytes of memory while integer takes only 4?

I get the following error when I run the code:

invalid conversion from 'int' to 'int*'

argument of type 'int' is incompatible with parameter of type 'int'

what does this mean?

I'm fairly new to programming so m sorry if the question seems stupid.

Lukas
  • 1,320
  • 12
  • 20
Orpheus
  • 219
  • 1
  • 4
  • 9
  • 1
    `sizeof(x)` is `sizeof(int*)`, not the size of the array. – Jarod42 Jun 25 '21 at 09:10
  • *"How is this justified?"* They are different types (and they don't convert to other). – Jarod42 Jun 25 '21 at 09:11
  • It means that a **pointer to an `int`** is not the same as an **`int`**. A pointer represents an address in memory of the computer; you can get an address of an `int` using the `&` operator (it's name is *address-of-operator*): `f2(&a);`. By the way, be aware that `i < sizeof(x)` ain't does what you think it should. – rawrex Jun 25 '21 at 09:12
  • `void f1(int* begin, int* end)` and call it with `f1(a1, a1+3);` and `f1(&a, &a + 1);` – Eljay Jun 25 '21 at 11:34

2 Answers2

1

The f2() (as well as the f1()) waits for a pointer to int. While you pass it an int, the a.

The error invalid conversion from 'int' to 'int*' means that a pointer to an int is not the same as an int. A pointer represents an address in memory of the computer, an int is a variable with integral numeric value, there's no way to implicitly convert one to another.

Meanwhile, the a1 is an array, but when we use the name of an array, it will decay to a pointer to the first element of an array. Check this thread for more: What is array to pointer decay?. So you can pass the a1 to f1() without use of the address-of-operator, since the a1 will decay to a pointer (to int*) by itself.

Means that there is a way from int[] to int*.

On the other hand, you can get an address of an int (or many other types) using the & operator (it's name is address-of-operator) like so:

f2(&a); // Calls f2() passing it a pointer to a

You also need to be aware that i < sizeof(x) ain't does what you think it should. The sizeof(x) returns the size of x in bytes.

You may want to check the documentation on the sizeof(). And the documentation on pointer declaration.

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • But then how does it allow array to pass in f1...int* and int array are different right? – Orpheus Jun 25 '21 at 09:35
  • @Orpheus because of the array to pointer decay. In almost all cases, when we use the name of an array, we refer to the pointer to the first element of that array. The `int*` and `int[]` are the same thing in **almost** all of uses (your case is surely one of these). Check the **What is array to pointer decay?** link in the answer for more elaborate information on the array/pointer. – rawrex Jun 25 '21 at 09:36
  • I was just reading array decays to pointers..cool stuff...I did not know about that.....I could use this method in case or pass an array of the same length....but using a pointer is more viable...thanks a lot @rawrex...and thanks on the headsup about sizeof too – Orpheus Jun 25 '21 at 09:45
0

The function f1 accepts only pointer(address) to integer as the input; and below statement is incorrect.

The function f1 allows me to pass an array as the argument to function but does not allow an integer variable to be passed as an argument.

You are passing the address of the array a1 while you are calling f1(a1). Because a1 is the address of first element of the array a10. However in the next line you are sending an integer with f1(a).

Salvankar
  • 91
  • 6