I need some explanation

I have written.I have a confusion in this code .I assign the value to an integer pointer without the * it gives me error message invalid conversion from int to int .While when we use this pointer to access and assign the values of an array we don'nt need to put a * before the pointer name .Why it is so and what is the meaning of invalid conversion from int to int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include<iostream>
using namespace std;




int main(){

int *p;
p=new int ;
p=10;//pointer without star gives error why?
cout<<*p;//*p used to acced the value of the pointer//
 p=new int [10];//pointer in used without star why?
 for(int i=0;i<=10;i++){
 	cin>>p[i];
 	
 }
for(int i=0;i<=10;i++){
	cout<<"The element of the array at the index"<<i<<"="<<p[i]<<endl;
	
}








}
.
line 11 needs to be:

*p = 10

Arrays work similarly to pointers and so [] effectively acts as a dereferencing operator.
Hello rafiq01,

I do not profess my-self an expert on pointers, but I will try.

The following code will show many things that you could do. The comments should help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>

using namespace std;

int main()
{
	int *p{ nullptr };  // <--- Defines a pointer to hold an address.

	cout << "\n Address of 'p' " << p << "\n";   //*p used to acced the value of the pointer//

	p = new int;  // <--- Returns an address to a memory location created on the heap.

	cout << "\n Address of 'p' " << p << "\n";   //*p used to acced the value of the pointer//

	*p = 10;//pointer without star gives error why?  // <--- Trying to store a value into a variable that holds an address.

	cout << "\n Address of 'p' " << p << '\n';   //*p used to acced the value of the pointer//
	cout << "\n Value of 'p' " << *p << '\n';  //*p used to acced the value of the pointer//

	p = new int[10];  //pointer in used without star why?  // <--- Stores a new address to an array of 10 elements.

	cout << "\n Address of 'p' " << p << "\n\n";   //*p used to acced the value of the pointer//

	for (int i = 0; i < 10; i++)
	{
		std::cout << " Enter number " << i + 1 << ": ";  // <--- Needs a prompt.
		cin >> p[i];
	}

	std::cout << "\n\n";

	for (int i = 0; i < 10; i++)
	{
		cout << "The element of the array at the index " << i << " = " << p[i] << endl;
	}

	return 0;  // <--- Not required, but makes a good break point.
}

int *p{ nullptr }; This defines a pointer to type "int" and sets it to zero. Basically an unusable address the you can use later to check if the pointer is good or empty. Always a good idea to initialize your variables when defined. This eliminates the garbage value at that bit of memory which can be a problem later in the program.

p = new int; "new" returns an address to memory that is created on the heap. Since "p" is defined as a pointer you can just store the returned value of "new" in the variable.

*p = 10 In this case the "*" means to de-reference the address to get to the value stored there.

The use of the "*" depends on the context in which it is used.

p = new int[10]; Here you are creating an array of 10 elements and storing the value in "p". The down side to this is that the original address of "p" has now changed and "p" no longer has a value of 10.

The for loops will not work as you have them for(int i=0; i <= 10; i++). First add some spaces to make it easier to read. The array has 10 elements numbered 0 - 9. The for loop will loop 11 times. In the first for loop the last iteration is writing to "p[i]" where "i" = 10 and 10 is beyond the end of the array. You are writing to memory that the array does not own and you have no idea what that memory belongs to. When the array is created on the stack it is worse because you could be writing to another variable or something that controls the program.

For the second for loop it is reversed. You are reading unknown memory and will print a garbage value.

Some notes on style:
There are several different styles for using the {}s
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements
You are free to choose whichever style you want to use. Personally I like the "Allman" style and along with proper indenting I find it the easiest to read and work with. IMHO.

In some places in your code you have to many blank lines and not enough where they are needed. One point of the code is to show you how the blank lines make it easier to read. The biggest goal here is to make your code as easy to read as you can. Mostly for your-self, but it also helps when posting here. The easier it is to read the quicker you may receive a reply. Also comments for the less obvious helps.

Andy
it gives me error message invalid conversion from int to int

In addition to the helpful advice others have given, I'll say this:

You really should read your error messages more carefully. What it actually says is:

invalid conversion from int to int*


i.e. from an integer, to a pointer-to-integer. Those are not the same types. If you'd read that error message more carefully, you might have found it easier to figure out the problem yourself.
Topic archived. No new replies allowed.