Hey guys am having an exam on Monday in c++ so i was wondering if i can get some help , because the questions are multiple choices about the output of programs and if the program has any errors , so is there any sites that would help ??
It helps to write code so that you get used to checking for errors and syntax. After looking at code you've written and running into an error over and over, you learn what to look for... How much actually coding have you done? That will help you to recognize what to look for. Try writing some simple things: a for loop that iterates an array, a while loop that pulls from a file and spits to a console, an if statement that changes the variable depending on the condition... You could also look at peoples code that has been posted here and try and figure stuff out, but ultimately you don't really learn until you start typing it in yourself.
Okay craniumonempty thanks I think am gonna start looking in the codes posted here... and try codes , and hanst99 thank u I already have the tutorial and it's helpful .
You cannot legally modify and access a variable more than once between sequence points (ie: a semicolon)
for example:
1 2 3 4 5
int a = 0;
b = a++ + ++a;
cout << a; // this will print 2
cout << b; // ?? no way to know what this will print
printing b might get you 1, 2, 3, or something else. The result is not defined by C++, so the compiler is free to do whatever it wants.
General rules of thumb:
- don't needlessly and overly compound statements. Even if the result was defined, it's super confusing. Trying to cram as much code as you can into one line is pointless and makes your code a mess.
- if you write/modify a var, that var should not exist anywhere else in the same line/sequence point. For example:
1 2 3 4 5 6 7
b = a++ /*now that we're modifying 'a', it shouldn't exist anywhere else in
this line of code */ ;
b = a++; // OK
b = a++ + a; // BAD
b = a + a++; // BAD
^ This ^ Your professor shouldn't even be showing you code like this.
Take a look at line 6:
b=a++ + ++a;
The compiler is free to do any of these operators in whatever order... as long as it follows rules of operator precedence.
In this case, it seems the compiler is doing this (but again note it doesn't HAVE to do it this way, it could do it another way, producing different results):
1 2 3 4
a += 1; // ++a
b = a; // using the post-increment value of ++a
b += a; // using pre-increment value of a++
a += 1; // a++
This results in a = 12 and b=22.
Line 7 has a similar problem:
cout<<b<<" "<<a++<<" "<<a<<" "<<++a;
The compiler seems to be doing this (but again, it doesn't have to -- it could print something different):
1 2 3 4 5 6
a += 1; // ++a (a now = 13)
cout << b << " "; // prints 22
cout << a << " "; // prints 13 (a++) -- using pre-increment value of a++
a += 1; // a++
cout << a << " "; // prints 14 (a)
cout << a; // prints 14 (++a) -- using post-increment value of ++a
In intconst *x; and constint *x; the int is constant. In int * const x; the pointer is constant. In intconst * const x both are constant.
Once again, modifying a variable more than once in a statement is undefined behavior. The compiler is allowed to do anything, including send hate mail to all your friends, eat your face off, or burn your processor to a crisp.
is it that the first one is a const itself and second one should point to a const , and what about the 3rd one ?? really thanks guys for ur help ...
Read them from right to left will help you work them out.
1, p is a pointer to a constant integer.
2, p is a pointer to an integer constant (same as 1)
3, p is a constant pointer to an integer constant.