Thread: pointers

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    pointers

    Hello all,
    Is there a way to move numbers in a circular motion using pointers? For example, if I wanted the numbers 12345 to shift and be 23451? Is it valid? Thank you!

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Shift how? Shift where? Move inside an array? Move to annother array? Are they stored in an int or float? Really not quite sure what you mean.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    You could use a linked list, and make it circular by linking the tail node back to the head node.

    In your case, each node would contain an integer between 0 and 9, and a pointer to the previous and next nodes. You could then start on any node, move in either direction through the list and concatenate the individual numbers into a single string until you got back to where you started, then convert the combined string to an integer for use.

    Only if you enjoyed doing that sort of thing though!!
    There is no such thing as a humble opinion

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    You can move an int variable with >> and << but that will not give you the desired effect since an int variable is a binary number but you want to shift the decimal representation of that number.

    You could do it like this:
    Code:
    int a=12345;
    int b,i;
    
    printf("%d",a);
    b=a%10;
    a/=10;
    for(i=1; i<=a; i*=10);
    a+=b*i;
    printf("%d",a);

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    So your trying to isolate the 'units' digit, remove it from the original number, then multiply it to the order of the original number and add it back on?

    Nice idea, but I don't think your code is quite right.

    This worked though...

    Code:
    #include <stdio.h>
    
    int main()
    {
      int a=12345;
      int b=0, i=0;
    
      printf( "\n%d", a );
    
      b = a%10;
      a = a/10;
    
      printf( "\na:%d;b:%d", a, b );
    
      for( i=1 ; i <=a ; i=i*10 );
    
      b=b*i;
      a+=b;
      printf( "\n\n%d\n\n", a );
    
      return 0;
    }
    There is no such thing as a humble opinion

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Sorry DrZoidberg, I've been a dope. Didn't read your code properly...
    There is no such thing as a humble opinion

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Also your code is identical to mine except that you didn't forget the linefeeds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM