Thread: Help with shifting pointers to arrays

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Help with shifting pointers to arrays

    Hello all! I'm in a digital platform class and have the assignment to optimize a 8081 processor design.

    Currently a very small part of the code looks like this:
    Code:
     unsigned char t[3];
    t[0] = t[1];
    t[1] = t[2];
    t[2] = 0;
    It seems useless to load and save all these values from memory. Instead I would like to try:
    Code:
     unsigned char t[3], (*tptr)[3] = &t;
    *tptr = *tptr << 8;
    Which is supposed to do the same reassignment to my array.

    I get the following error codes:
    error 48: cannot assign values to aggregates
    error 43: invalid operand for shift operator
    warning 113: left & right types are unsigned-char [3] ,literal-unsigned-char
    The rest of my code works fine. I'm also allowed to use in-line ASM code instead. Where did I go wrong with my alternative implementation?
    Last edited by Karasu; 11-01-2011 at 09:27 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure you're doing this in C#?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by itsme86 View Post
    Are you sure you're doing this in C#?
    Whoops! My mind must have mixed projects after seven hours of coding. This should be in the C-programming forum ofcourse =/ Sorry!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What if you did something like this?
    Code:
    #include <stdint.h>
    ...
    unsigned char t[3];
    uint32_t *t_int = &(uint32_t *)t;
    *t_int <<= 8;
    Last edited by itsme86; 11-01-2011 at 10:33 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Moved to C
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    itsme86's solution should work for you as long as you stick to an array of 3 unsigned chars. As for why your code didn't work:
    Code:
    unsigned char t[3], (*tptr)[3] = &t;
    *tptr = *tpr << 8;
    tptr is a pointer to an array of 3 unsigned chars. Thus, *tptr is the array itself (you dereferenced the pointer). You can't assign to an array as a whole, only to individual elements, hence *tptr = is an error. Also, you can't shift arrays, thus *tptr << 8 is an error as well.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by itsme86 View Post
    What if you did something like this?
    Code:
    #include <stdint.h>
    ...
    unsigned char t[3];
    uint32_t *t_int = &(uint32_t *)t;
    *t_int <<= 8;
    Why do you want to use an integer type for this? Isn't it going to go outside of my 3 Byte wide char array?

    I'm getting the following error:
    indirections to different types assignment from type 'unsigned-long-int generic* near* ' from type 'unsigned-long-int generic* '

    EDIT: I've found a solution based on your proposal of typecasting
    Code:
    unsigned char t[4];
    unsigned long *tptr = (unsigned long *)t; // Note that & is not needed
    *tptr >>= 8;
    A few remarks:
    - Integers were apparently not always 4 Bytes wide internally. The long however was.
    - I allocate four Bytes for my array in order to make sure the fourth byte is always zero, there is no datatype of three bytes wide available and creating my own didn't work somehow
    - Unexpectedly I had to shift my array RIGHT instead of left. Is it possible that an array is stored from high to low? :S
    - There was no increase in performance since the bus width between internal memory and CPU is only one Byte wide. There is an equal amount of LOAD/STORE required and as such no decrease in required cycles.
    Last edited by Karasu; 11-04-2011 at 10:56 AM. Reason: Solution

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shifting arrays K positions without using additional memory
    By Veneficvs in forum C++ Programming
    Replies: 11
    Last Post: 03-30-2011, 02:31 AM
  2. Shifting pointers in a function
    By herakles in forum C Programming
    Replies: 2
    Last Post: 03-18-2011, 09:46 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. shifting arrays
    By ZeroG in forum C Programming
    Replies: 6
    Last Post: 06-16-2004, 10:13 PM

Tags for this Thread