In an implementation of a CGRect, I attempt this:
BOOL didStartPressing = NO;
if ( didStartPressing) {int nmax=5;}
else{int nmax=500;}
for (int n=1; n<nmax; n=n+1){ *... working code that draws some circles ....* }
This gives yellow warnings about "unused variable nmax" in the first part above
and red warnings about "use of undeclared variable nmax." for the for loop.
If however I simply replace the first three lines above with
int nmax=500;
I get a lovely picture that I drew in CGRect.
Greatest of thanks for help, as I'm complete noob up against the hard wall of learning curve.
You've limited the scope of nmax to the braces after the if and the else. So you have two variables with that name, neither of which is visible to the for loop. To solve, move the declaration to the outer scope and simply assign to the variable in the if/else scopes:
int nmax = 0; // the = 0 is optional in this case because all code
// paths assign a value to nmax
BOOL didStartPressing = NO;
if (didStartPressing) {
nmax=5;
} else {
nmax=500;
}
for (int n=1; n<nmax; n=n+1) {
/*... working code that draws some circles ....*/
}
This is a C language thing. The scope of a variable that's declared inside a pair of braces is only the section inside the braces. That means you have two different variables, both called nmax and each limited to its section of the if/else statement.
You can make it work with:
int nmax = 500;
if ( didStartPressing) {
nmax=5;
}
Look at a book on C programming for more details.
Related
I am getting multiple declaration error in this c++ program
#include<iostream.h>
#include<conio.h>
void main ()
{ clrscr();
int a[10][10],r,q,i;
cout<<"enter how many rows and colomn you want in the matrix:";
cin>>n;
cout<<"enter the matrix \n";
for(int r=0;r<n;++r)
{
for(int q=0;q<n;++q)
{
cin>>a[r][q];
}
}
for(int i=0;i<n;i++)
{ cout<<"\n the diagnol elements are:";
cout<<a[n-i-1][i];
}
getch();
}
it is a program for finding diagnol elements in a matrix
That's because you have already declared the r, q, i as int at line 5 as below:
int a[10][10],r,q,i;
^^^^^
While your three for loops, again re-declares for e.g. like this:
for(int r=0;r<n;++r)
^^^
So it re-declares the same variable in above case it's r, while the other for loop q and i which is not allowed.
Two ways to solve the problem:
a. Either you remove int from your for loop.
b. Either you remove declaration of variables used in for loop from line 5.
The only issue i am seeing is, you are not declaring the variable "n" . Other than that, everything seems to be fine.
i was looking some examples of interactions with the keyboard and stumbled upon this code that i found interesting. But i'm having trouble understanding a certain part of it(it's marked down below).I don't get how all this whole ''boolean'' declaration, ''switch'' and ''CASE'' works, i tried to look in the reference but still. Could someone explain in a simple maner how these work?
float x = 300;
float y = 300;
float speed = 5;
boolean isLeft, isRight, isUp, isDown;
int i = 0;
void keyPressed() {
setMove(keyCode, true);
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
}
void keyReleased() {
setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {// <<<--- From this part down
switch (k) {
case UP:
return isUp = b;
case DOWN:
return isDown = b;
case LEFT:
return isLeft = b;
case RIGHT:
return isRight = b;
default:
return b; }
}
Questions like these are best answered by the reference:
Works like an if else structure, but switch() is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the expression. All remaining statements in the switch are executed unless redirected by a break. Only primitive datatypes which can convert to an integer (byte, char, and int) may be used as the expression parameter. The default is optional.
The rest of the code is setting the corresponding variable to whatever value you passed in as the b parameter, and then returning it.
You should get into the habit of debugging your code. Add print statements to figure out exactly what the code is doing.
What do parentheses do when evaluating some meaning?
I have faced this in code, when something is checked and they use
if ( (some condition that returns 1) )
{
code
}
So my question is, does this evaluate to true? I would think that it is always false since (1) does not return anything?
Edit: clarification, question is why double parenthesis in if? I know that 1 is true.
The additional parentheses are used when an assignment is used for its truth value. They allow the compiler to distinguish between
if ((var = expr))
which signals intentional combination of assignment and truth value test, and
if (var = expr)
as an unintentional misspelling of if (var == expr).
The convention, carried over from C and C++, is for the compilers to warn on if (var = expr) as a possible misspelling of if (var == expr). They don't warn on if ((var = expr)), because the extra set of parentheses signals to the compiler that the assignment was intended. As rob mayoff explains, clang has a special case not to warn for certain assignments to self, but for many coders the habit remained.
As others said, the generated code is exactly the same with and without the extra parens.
If you write,
if (self = [super init]) { // Warning
// ...
}
The compiler will give you a warning, because it thinks you might have mistyped = as ==. If you add a second set of parentheses, the warning goes away.
if ((self = [super init])) { // No warning
// ...
}
So the extra parentheses are there to make typos less likely. The parentheses do not change the value of the expression.
In general 0 equates to false, NOT 0 to true.
This link explains bool in objective-c: http://www.mindsizzlers.com/2010/04/objective-c-and-the-properties-of-bool/
true and false value of bool and int
boolean value of false is equivalent to the int value of 0.
boolean value of true is equivalent to the int value of non-zero (e.g. 1, 10, 3000, etc)
For example, consider an example of C code below:
bool bValue;
int nValue;
bValue = true;
nValue = 1;
if (bValue == nValue) {
printf("bValue and nValue are the same!\n");
}
// output: bValue and nValue are the same!
Wrapped with multiple parenthesis:
The following two snippets returns exactly the same results.
if ((((((((((((((((((((1)))))))))))))))))))) {
printf("Hello World!\n");
}
returns the same result as:
if (1) {
printf("Hello World!\n");
}
Expression in if statement
Within your given if statement, you must have an expression that resolves to either true or false value. Please refer to this page for example of expression.
I am trying to compare a CGFloat to an integer value. Based on this value, execute a conditional... pretty standard. However, this will always be true for some reason. I even print out the values and they are clearly less than 800.... I have tried a bunch of different combinations, the most recent is shown below, I thought maybe it was comparing the size of float and the size of the int based purely on its binary values, so I tried this risky little cast operation... Any ideas?
CGPoint textViewPoint = [scroller convertPoint:[textView center] toView:(UIView *)self.view];
NSLog(#"the y coord is %f", textViewPoint.y);
int size = (int)textViewPoint.y;
NSLog(#"the yint %d", size);
//move the main view, so that the keyboard does not hide it.
//if (self.scroller.frame.origin.y >= 0 && textViewPoint.y > 800.0);
if(size > 800);
{
NSLog(#"moving up");
The problem is the ; at the end of the if(size > 800); line, not the int vs. float comparison. Remove it and all should be OK.
This is because this semicolon is interpreted as the body of your if statement, and that's this NO-OP statement that is executed when the condition is true. Then, the rest of your code next to this if empty body is outside of the if body so is executed whatever the condition value. That's exactly as if you had written:
if(size > 800)
{
}
{
NSLog(#"moving up");
}
Compiler Warning Tip
The compiler generally warns you about this mistake. Be sure that you have the "Empty Loop Bodies" warning activated in your project Build Settings (compiler flag -Wempty-body): this way the next time you do this mistake, you will have a warning about it and will know what is wrong and how to fix it.
I'm looking over some code and I came across some syntax that I don't know the meaning of. What does the '->' mean?
-(void) getTransformValues:(struct transformValues_*) tv
{
tv->pos = positionInPixels_;
tv->scale.x = scaleX_;
tv->scale.y = scaleY_;
tv->rotation = rotation_;
tv->skew.x = skewX_;
tv->skew.y = skewY_;
tv->ap = anchorPointInPixels_;
tv->visible = visible_;
}
The arrow operator ('->') is used in the same place you would use the dot operator ('.'), but with a pointer to a structure instead of an object of that structure.
typedef struct _Person {
char name[200];
unsigned int age;
} Person;
If you created an object of that structure, you would use the dot operator in order to access its members:
int main()
{
Person p1;
strcpy( p1.name, "Baltasar" );
p1.age = 36;
}
However, if you a pointer to a structure, instead of the structure itself, you could only use the arrow operator, or a little bit more complex dot operator:
int main()
{
Person p1;
Person *ptrPerson = &p1; // ptrPerson points to p1
strcpy( ptrPerson->name, "Baltasar" );
ptrPerson->age = 36;
}
As I said above, you could still use the dot operator:
int main()
{
Person p1;
Person *ptrPerson = &p1; // ptrPerson points to p1
strcpy( (*ptrPerson).name, "Baltasar" );
(*ptrPerson).age = 36;
}
Of course, all of this discussion involves a lot more topics, such as pointers, the heap, etc. Hope this helps.
The -> symbol is used to access a member of a pointer type. It is the same as dereferencing the pointer and using the dot operator, i.e.,
(*tv).pos = positionInPixels_;
It's used to access a member of an object / struct pointed to by a variable.
For example tv->pos is used to access the member variable pos from the object pointed to by tv
-> is used to mean the same thing as the dot (which means to access a member of a structure, class, or union), except that -> is used when the variable is a pointer.
"->" is used in order to access a struct pointer element. In C at least...
typedef struct test {
int one;
int two;
} t_test;
t_test *foo;
/* Allocation and all the stuff */
foo->one = ...
foo->two = ...
The arrow operator (->) takes a struct pointer (to a transformValues_ in this case), dereferences it, then accesses that member variable.
IE: these are equivelant:
(* tv).pos === tv->pos
Hmmmm did you at least consider trying to find it out for yourself before posting here?
This is what I got from searching operators....