Thread: Pointers as array, copying to memory locations help

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    Question Pointers as array, copying to memory locations help

    Well, basically the program should copy the ints a, b, c to destinations x, y, z.....

    Haven't done C in a while and trying to get back on my feet.......
    Pointers are getting somewhat confusing.....
    The copy function was written way back....when I still knew C well...
    The rest was written recently......
    I put in nops as assembly as sort of, indentations in assembly so i can compare them to the C...


    When you rarely speak a language, you forget it....i guess the same is true with programming languages....

    Can someone help me debug this code........

    Here is my code
    Code:
    #include <stdio.h>
    
    int ptrcopy(int *src, int *dest, int len)
    {
    asm("nop");
        int result = 0;
            asm("nop");
        while (len > 0) {
            int val = *src++;
            asm("nop");
            *dest++ = val;
            asm("nop");
            result ^= val;
            asm("nop");
            len--;
            asm("nop");
        }
            asm("nop");
        return result;
    }
    
    
    
    int main()
    {
    asm("nop");
    
    
    /* source */
    int a = 0x123;
    int b = 0x456;
    int c = 0x789;
    
    asm("nop");
    /* destination */
    int x = 0x111;
    int y = 0x222;
    int z = 0x333;
    
    asm("nop");
    
    int  source = a;
    
    int destination = x;
    
    asm("nop");
    
    
            printf( " %d \n", ptrcopy(source,destination,3)   );
    
    asm("nop");
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Instead of a, b, c to x, y, z, you should be using arrays. It might be helpful to throw a couple printf's in to see what is going on in places of interest.
    Code:
    #include <stdio.h>
    
    int ptrcopy(int *src, int *dest, int len)
    {
       int result = 0;
       while ( len > 0 )
       {
          int val = *src++;
          *dest++ = val;
          result ^= val;
          len--;
       }
       return result;
    }
    
    int main()
    {
       int a[] = {0x123, 0x456, 0x789};
       int x[] = {0x111, 0x222, 0x333};
       int i;
       for ( i = 0; i < 3; ++i )
       {
          printf("a[%d] = %x, x[%d] = %x\n", i, a[ i ], i, x[ i ]);
       }
       printf("result = %d\n", ptrcopy(a,x,3));
       for ( i = 0; i < 3; ++i )
       {
          printf("a[%d] = %x, x[%d] = %x\n", i, a[ i ], i, x[ i ]);
       }
       return 0;
    }
    
    /* my output
    a[0] = 123, x[0] = 111
    a[1] = 456, x[1] = 222
    a[2] = 789, x[2] = 333
    result = 764
    a[0] = 123, x[0] = 123
    a[1] = 456, x[1] = 456
    a[2] = 789, x[2] = 789
    */
    The function ptrcopy copies from one array to another, calculates some sort of checksum (an LRC?), and returns the calculated result.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Replies: 2
    Last Post: 05-10-2005, 07:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM