Click here to Skip to main content
15,896,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
//when 
int *p; 
int array[5]; 
p = array;     
//what does p = array do? 
//and 
for(int i = 0; i<5; i++){
cin>>*p; 
*p++;
} 
//whats does *p++do and why is it being used


What I have tried:

tried stack overflow but didnt help
Posted
Updated 9-Dec-16 22:10pm
v2

Quote:
p = array;

this is the same of
C++
p = &array[0];

that is: make p point to the first item of the array.



Quote:
*p++;
THis is equivalent to
C++
*p; 
p = p + 1;
The first statement returns the value of the item currently pointed by p. Anyway such value is unused.
The second statement increments p.
 
Share this answer
 
int *p; 
int array[5]; 
p = array;  
The specification says that the name of an array is a pointer to the first element, so when you assign that name to a variable you are setting the variable to point to the first element of the array: is the equivalent of saying:
int *p; 
int array[5]; 
p = &(array[0]);  
 
Share this answer
 
See Array Declarations[^] and Pointer Declarations[^] and all other sections.
 
Share this answer
 
When you learn a language, the debugger is an excellent tool to help you learn it because, thanks to the debugger you see things happen in your code.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

-----
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900