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?Originally Posted by viciousv322
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?Originally Posted by viciousv322
Quzah.
Hope is the first step on the road to disappointment.
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.
Hope is the first step on the road to disappointment.
Deleted my message because I thought the link to the loop tutorial tells it all.
I'm doing that for quite a while now and never realized that this is a C99 -feature.Code:for ( int i = 0; i < 5; ++i ) // loop 5 times
Kurt
Originally Posted by quzah
Thanks for the help...and especially...the thorough explanations...
One more question regarding arrays...
I got D as my answer but its more of a guess.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
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.
The answer is a.
*(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...Originally Posted by ZuK
It is the difference between saying:If you understand pointers, you know they are two very different things.Code:int x, *ptr; ptr = &x; something = *ptr; /* this... */ something = ptr; /* and this. */
Quzah.
Hope is the first step on the road to disappointment.
*(datalist+2) is the same as datalist[2]Originally Posted by viciousv322
*(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)
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Maybe this sample can help maybe not but I will give it a chance:Originally Posted by Dave_Sinkula
And generates the follwing output: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; }
1
3
2
3219900408
Last edited by Maragato; 12-18-2005 at 12:44 AM.
Perhaps you meant this?
[edit]Or perhaps something like 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)); printf("%p\n",(void*)&arraynumby[2]);//this is a memmory adress return 0; }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 */
Last edited by Dave_Sinkula; 12-18-2005 at 11:19 AM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.Originally Posted by Maragato
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).
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.Originally Posted by Maragato
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Thanks a lot I shall avoid this in future code (btrw Ive never used it before )Originally Posted by Dave_Sinkula