# Thread: looping the loopiest

1. ## looping the loopiest

heyas!

although i am beyond this section in my learning of c.. i have little things bugging me which i never really sat down and thought about..

(please excuse typing errors or little code errors, none of what is below have i compiled, it isnt really necessary )

if you could respond with answers to any of my questions i would be greatly appreciative (spelling?

with something like a for loop, the longest -and correct way -would be to have:

for(x=0; x<10; x=x+1)

the x=x+1 was then written (in my tutorials and books) as x++. is there a difference between 'x++' and '++x', and how does the actual compiler read all these simular statements as the same..? in the library does it define shortened expressions for longer, such as x=x+1 : x++, or is x++ mathimatically or logially equal the same thing as x=x+1?

heh, now thats of my chest..

with pointers, and structures..
you can have a structure, eg:

struct coordinates {
int x;
int y;
} coord;

now coord.x can be assigned a value, as can coord.y, however it is possible to assign the pointer 'p_coord' to the actual value of coord..
(i think its..)

p_coord->coord

so *p_coord.x = coord.x

however, i see little point in the pointer because your pointing to a variable, however your not shortening the expression, i would understand if you could (and im not sure if you can..)

p_xcoord->coord.x

to save typing.. but again, the whole idea of pointers (especially in structures) seems useless, UNLESS it is seen as a safety device against changing values or the type of existing variables?..

and can you make a pointer to the address of really weird stuff? im not into ports yet, but are ports etc assigned as variables, or given 'space', because they too presumably would have addresses, theretiacally you could also create pointers to those addresses..

thanks in advance, please again excuse my code errors (if any) and/or typos

-twans

2. Hi
The post increment and pre increment operators have no effect in a for loop.
for ex
for(x=0;x<10;x++)
and
for(x=0;x<10;++x)
have the same effect.

As regards ur question in structures and pointers what you are trying to do there is create a structure object and then create a pointer to the structure.Then make this pointer point to the structure object.

Hope this suffices.

Regards
Sebastiraj.

3. x = x + 1
x += 1
++x
x++
All mean the same thing, and the last time I checked some test code, produced the same code.

struct coordinates {
int x;
int y;
} coord;

To create a pointer, do this
struct coordinates *ptr_to_coord = &coord;

Then you can do
ptr_to_coord->x = 0;

If you want to point at x directly, it would be
int *ptr_to_coord_x = &coord.x;
*ptr_to_coord_x = 0;

4. ## This might help...

main()
{

int x = 10, y = 0;

y = ++x; // sets x and y to 11

y = x++; /* sets x = 11 and y = 10 because x is passed to y before it is incremented */

return 0;

}

-CDuddley

5. ++x
x++

These are _not_ the same thing.
They are the same if you're using a simplistic for() loop. Or rather, they end up evaluating as the same. However, they are not the same.
Code:
```int someFunction( int a ) { printf("%d\n", a ); return a;

for ( x = 0; x < SOMENUMBER; x = someFunction( ++x ) ) ;
for ( x = 0; x < SOMENUMBER; x = someFunction( x++ ) ) ;```
Now then, the main difference here is what exactly gets passed to the function.

++x passes "1" the first time, where as x++ passes "0" the first time. Were we doing something more vital, you could have serious problems.

Quzah.

6. For the x++ and ++x, the difference is whether x is incremented before, or after, the statement is evaluated.

Code:
```main ()
{
int x = 0;
printf ("%d\n", x);  // Prints 0, of course.
// x is 0
printf ("%d\n", ++x);  // Increments x, then prints x (prints 1).
// x is now 1.
printf ("%d\n", x++);  // Prints x, then increments x (prints 1).
// x is now 2.

/*  And now for the interesting part... */
printf ("%d\n", (x = x + 1); // Increments x, then prints x (prints 3).
// x is now 3.
return;
}```
It's really trivia, but ++x performs the same way as (x = x + 1), but this is not the same as x++. I hope that isn't confusing.

x++ is something you will come to love I assure you.

I don't know if this is still the case, but at least in the past it was that x++ was a faster instruction than (x = x + 1). Not to mention it looks neater for something that comes up an awful lot in code.

As for the point of pointers.... well, this is something that I'm pretty sure you'll learn eventually, so just remember that I am only giving you one brief example. Pointers to structures are something that are used all the time really.

Code:
```struct coordinates {
double x;
double y;
} coord;

main ()
{
struct coordinates * pCoord = &coord;
coordStuff (coord);
pCoordStuff (pCoord);
return;
}```
I changed the structure to two doubles instead of ints to make a point. Every time coordStuff is called, you pass it a coordinate, which takes 16 bytes. Every time pCoordStuff is called, you pass it a coordinate *, which takes 4 bytes. Whatever you pass a function, that function makes a copy of and stores in memory, so pointers are generally more function-friendly than actually passing whole structures.

Finally, as for pointers to really weird stuff... well, one example I think is that stdin and stdout are treaded as pointers, and on top of that, there's pointers to functions, which qualifies as rather strange.

7. > for ( x = 0; x < SOMENUMBER; x = someFunction( ++x ) ) ;
Except this code breaks the rules here, so it's hardly a fair comparison.
http://www.eskimo.com/~scs/C-faq/s3.html

And for the record, x++ == ++x ONLY when the statement is free from other side effects.

8. How does it break the rules? If I change it to this:
Code:
```int myfunPOST( int x )
{
//do stuff with x
//now increment it
x = x + 1;
return x;
}
int myfunPRE( int x )
{
x = x + 1;
//increment x
//now do stuff with it
return x;
}

for( x = 0; x < SIZE; x = myfunPOST( x ) ) ;
for( x = 0; x < SIZE; x = myfunPRE( x ) ) ;```
then it is really doing the same thing. It isn't like rule 3.2, which is what I believe you're saying, in that it's being passed to a function and _that_ value is being assigned back. It's being given the passed value, which will vary, as you are aware, but the original poster was not.

Notice how the functions work. This is what I was illustrating, as I'm sure you are aware.

Quzah.

Popular pages Recent additions