1. Originally Posted by viciousv322
So what would be needed to make #2 a true statement? How would i be able to make it loop just 5 times?
Well, here is a tutorial on loops. See if you can figure it out after going through this. After all, the whole point here is for your to understand what's going on, right?

Quzah.

2. That only works in C99. If you don't have a C99 compiler, you will have to declare your variable outside of the for loop.

Quzah.

3. Deleted my message because I thought the link to the loop tutorial tells it all.
Code:
`for ( int i = 0; i < 5; ++i ) // loop 5 times`
I'm doing that for quite a while now and never realized that this is a C99 -feature.
Kurt

4. Originally Posted by quzah
Well, here is a tutorial on loops. See if you can figure it out after going through this. After all, the whole point here is for your to understand what's going on, right?

Quzah.

Thanks for the help...and especially...the thorough explanations...

One more question regarding arrays...

Code:
```Assuming that   datalist[ ]  is a one-dimensional array of type int, which of the following refers to the value of the third element in the array?

a. *(datalist+2)
b.	*(datalist+4)
c.	datalist+4
d.	datalist+2```
I got D as my answer but its more of a guess.
My logic: Based on arrays starting w/ 0, i would think that the third element will be 2.. So my best guess is D, but only because i don't know what the * does.

*(datalist+2) is the third value
(datalist+2) points to the third value
Kurt

6. Originally Posted by ZuK
*(datalist+2) is the third value
(datalist+2) points to the third value
Kurt
That helps...but i would like to know the difference...only because they both sound like the same thing...

7. It is the difference between saying:
Code:
```int x, *ptr;
ptr = &x;

something = *ptr; /* this... */

something = ptr; /* and this. */```
If you understand pointers, you know they are two very different things.

Quzah.

8. Originally Posted by viciousv322
That helps...but i would like to know the difference...only because they both sound like the same thing...
*(datalist+2) is the same as datalist[2]
*(datalist+1) is the same as datalist[1]
*(datalist+0) is the same as datalist[0]

and

(datalist+2) is the same as &datalist[2]
(datalist+1) is the same as &datalist[1]
(datalist+0) is the same as &datalist[0] (which is the same as datalist)

9. Originally Posted by Dave_Sinkula
*(datalist+2) is the same as datalist[2]
*(datalist+1) is the same as datalist[1]
*(datalist+0) is the same as datalist[0]

and

(datalist+2) is the same as &datalist[2]
(datalist+1) is the same as &datalist[1]
(datalist+0) is the same as &datalist[0] (which is the same as datalist)
Maybe this sample can help maybe not but I will give it a chance:
Code:
```#include <stdlib.h>
#include <stdio.h>

int main(void){
int *ptr;
int arraynumby[] = {0, 1, 2, 3};
//int foo;

printf("%d\n",arraynumby[1]);
*ptr = arraynumby[3];
printf("%d\n",*ptr);
printf("%d\n",*(arraynumby+2));
printf("%lu\n",(long unsigned int)&arraynumby[2]);//this is a memmory adress
return 0;
}```
And generates the follwing output:
1
3
2
3219900408

10. Perhaps you meant this?
Code:
```#include <stdlib.h>
#include <stdio.h>

int main(void){
int *ptr;
int arraynumby[] = {0, 1, 2, 3};
//int foo;

printf("%d\n",arraynumby[1]);
ptr = arraynumby;
printf("%d\n",*ptr);
printf("%d\n",*(arraynumby+2));
return 0;
}```
Or perhaps something like this.
Code:
```#include <stdio.h>

int main(void)
{
int i, a[] = {0, 1, 2, 3}, *p = a;
for ( i = 0; i < (int)(sizeof a / sizeof *a); ++i )
{
printf("&a[%d] = %p, ",    i, (void*)&a[i]);
printf("a[%d] = %d, ",     i,         a[i]);
printf("(p + %d) = %p, ",  i, (void*)(p + i));
printf("*(p + %d) = %d, ", i,       *(p + i));
printf("p[%d] = %d\n",     i,         p[i]);
}
return 0;
}

/* my output
&a[0] = 0012FF7C, a[0] = 0, (p + 0) = 0012FF7C, *(p + 0) = 0, p[0] = 0
&a[1] = 0012FF80, a[1] = 1, (p + 1) = 0012FF80, *(p + 1) = 1, p[1] = 1
&a[2] = 0012FF84, a[2] = 2, (p + 2) = 0012FF84, *(p + 2) = 2, p[2] = 2
&a[3] = 0012FF88, a[3] = 3, (p + 3) = 0012FF88, *(p + 3) = 3, p[3] = 3
*/```

11. No no I really wanted that line as it was I was just showing him manners to acces the index of the array Ive castes to lu cause Imho it is better to look

12. Originally Posted by Maragato
No no I really wanted that line as it was I was just showing him manners to acces the index of the array Ive castes to lu cause Imho it is better to look
Well, it's UB that crashes on my system. Don't dereference an invalid pointer.

And converting to an unsigned long is not the best way to printing a pointer value (that's probably why there is a %p you know).

13. What do you mean by UB?

14. Originally Posted by Maragato
What do you mean by UB?
Undefined behavior. It could work; it could crash; it could do nothing; it could do whatever: its behavior is undefined.

15. Originally Posted by Dave_Sinkula
Undefined behavior. It could work; it could crash; it could do nothing; it could do whatever: its behavior is undefined.
Thanks a lot I shall avoid this in future code (btrw Ive never used it before )