Java to C problems

This is a discussion on Java to C problems within the C Programming forums, part of the General Programming Boards category; I am trying to convert code from Java to C. I am sure I have done something wrong but I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Java to C problems

    I am trying to convert code from Java to C. I am sure I have done something wrong but I cannot figure out; can anyone see the problem?

    Code:
    // Java
    
    public void rotate(int newSize)
    {
     int i;
     int position = size - newSize;
    
     char temp = charArray[position];
    
     for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
     
     charArray[i - 1] = temp;
    }
    
    // C (Attempted)
    
    void rotate(int newSize) 
    {
     int i;
     int position = size - newSize;
    
     char temp = *(myArray + position);
        
     for (i = position + 1; i < size; i++)
      *(myArray + (i-1)) = *(myArray + i);
      
     *(myArray + (i-1)) = temp;
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    58
    I don't see anything wrong.

    But why did you use

    Code:
    *(myArray + (i-1)) = temp;
    when you could have used:

    Code:
    myArray[i-1] = temp;

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    Apart from figuring out what charArray is, I figure it's a class member in Java right?, the syntax is exactly the same.

    Given a suitable array, the body of the function is identical in both languages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What exactly is wrong with the code above ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    I am new to C...

    I tried to declare a global char array and initialize it in the main method but the compiler wouldn't let me.

    The only way around it (that I could think of) was using a char pointer (globally).

    I would rather use

    Code:
    myArray[i-1] = temp;
    When I run both versions (the C and Java) I get different results for some reason.

    How can I make the char array global... possible?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    You could always post the code you tried and the error messages. Simply saying "it doesn't work" is no use.

    Rather than make the array global, pass it as a parameter.
    Code:
    void rotate( char charArray[], int newSize) {
    }
    
    int main ( ) {
      char arr[10];
      rotate( arr, 2 );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  3. Multiple Java files for one Java project
    By doubleanti in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 01:06 AM
  4. Tasty Java
    By The Brain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-12-2004, 10:44 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21