Thread: Please check my work...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #18
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Quote 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.

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The answer is a.
    *(datalist+2) is the third value
    (datalist+2) points to the third value
    Kurt

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    Quote Originally Posted by ZuK
    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...

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #23
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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)
    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.*

  9. #24
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote 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
    Last edited by Maragato; 12-18-2005 at 12:44 AM.

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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));
      printf("%p\n",(void*)&arraynumby[2]);//this is a memmory adress
      return 0;
    }
    [edit]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
    */
    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.*

  11. #26
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    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. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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).
    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.*

  13. #28
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    What do you mean by UB?

  14. #29
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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.
    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.*

  15. #30
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote 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 )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Code doesn't work: Check alphabetical order
    By motozarkov in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2005, 02:53 PM
  3. Shouldn't this code work?
    By DeepFyre in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2004, 01:24 PM
  4. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  5. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM