#include <iostream>
usingnamespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
Can someone help me to explain this program line by line, especially from p=numbers; to *(p+4) = 50;
1.The p++ what exactly does does it add?
2.p=&numbers[2]; why does it &numbers[2], why cant it numbers[2]?
3.p= numbers +3; numbers is an array, so what exactly does it add??
4.(p+4); what is p? my friend said that it is an address, so why can an address be added?
Thx
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
#include <iostream>
usingnamespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers;
std::cout << "\n p = " << p << std::endl;
*p = 10;
p++;
std::cout << "\n p = " << p << std::endl;
*p = 20;
p = &numbers[2];
std::cout << "\n p = " << p << std::endl;
*p = 30;
p = numbers + 3;
std::cout << "\n p = " << p << std::endl;
*p = 40;
p = numbers;
std::cout << "\n p = " << p << std::endl;
*(p + 4) = 50;
std::cout << std::endl;
for (int n = 0; n<5; n++)
std::cout << numbers[n] << ", ";
return 0;
}
Although there is nothing wrong with your code as written I changed it for easier reference. I also added the "cout" statements to show how the address of "p" changes. The "*p" dereferences the pointer to get to the value stored there.
Run my changed program and see if that helps for now. I will pick the program apart shortly after breakfast.
I will be referring to the code in my previous message.
The general concept of this code is to show you the different ways to use a pointer.
Line 6 defines an array of type int, but you should initialize your array like int numbers[5]{ 0 }; which will set all elements of the array to zero. Not a big problem here unless you try to use an element of the array before you give the elements a value.
line 7 defines a pointer of type int and gives it a variable name of "p".
Line 8 gives the pointer "p" the address of numbers. Since "numbers" is the start of the array here it is referred to by its address, since "numbers" referees to the entire array and not a specific element.
Line 9 and the other "couts" just show you how the address that "p" holds changes. When you look at the addresses of "p" notice how many bytes it changes by. The change is by the size of an "int", generally 2 bytes, and not one byte. When you increment or decrements a pointer it is based on the type of pointer. An "int" is two bytes, a "double" is four bytes and a "char" is one byte. I believe its the compiler that will take care of the math for you.
Line 10 the "*" is the deference operator telling the program to use the value at that memory location and not the address.
Line 11 will increment the address of "p" by the size of an "int".
Line 13 is like line 10. The address is derferenced to receive the new value.
Line 14 uses the "&", address of operator, to store the address of "numbers[2]" and not the value.
Line 16 same as line 10.
Line 17 referring back to lie 8 and 14 "numbers + 3" is a way to increment the address of numbers before storing it in "p". It is the same as saying p = &numbers[3];.
Line 22 similar to the rhs of line 17. It is incrementing the pointer address, (p + 4), before it dereferences the address to store the number.
For fun copy line 9 and paste it before line 9 then change "p" to "numbers" to see the address of "numbers".
Hope that helps,
Andy
P.S. If I managed to get something wrong someone will let us know.
Can someone here please help me understand pointers and how to use them? I am trying to write a program that shows an IP address. It is supposed to use pointers but I cannot figure out how to write the pointer into the code. I am completely lost when it comes to pointer and how to use them. I have read and reread my test books and I am still completely lost on the whole concept the books talk about.