Pointer confusion

This is a discussion on Pointer confusion within the Linux Programming forums, part of the Platform Specific Boards category; im having some trouble with pointers in linux (redhat 8.0 with gcc). i can run this code fine in windows ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Pointer confusion

    im having some trouble with pointers in linux (redhat 8.0 with gcc). i can run this code fine in windows but it just wont work right in linux

    Code:
    #include <stdio.h>
    
    int main() {
        char temp[20] = "testing";  
        char temp2[20] = {'\0'};
        int spot = 2;
        
        copy(temp, temp2, &spot, 5);
        printf("%s\n", temp2);
        
        return 0;
    }
    
    int copy(char *src, char *dest, int *start, int end) {
        int x = 0;
        
        --*start;
        while(*start < end) {
            dest[x] = src[*start];
            
            ++*start;
            ++x;
        }
        
        return 0;
    }
    copy is just supposed to take a source a dest and some posistion info. it take the start and end and changes dest to be the middle(of start and end).

    sorry about asking for help on this same dam function but i just dont get it!!!

  2. #2
    Registered User Fredd's Avatar
    Join Date
    Oct 2002
    Posts
    69
    hmm, maybe you should describe the problem aswell.
    I get "esti" from running it, seems right to me i'm to tired to think though

    furthermore you should declare the copy function by adding
    Code:
    int copy(char *src, char *dest, int *start, int end);
    and i don't really understand why you declare it to return an int when the return value is'nt used.
    "Writing software is more fun than working."

    got slack?
    http://www.slackware.com/

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    yeah, you should get "esti" from it. and with the return, it just seemed right. it doesnt sound like alot people like void functions and if i get this to work ill add some error checking to it.

    but i only get "esti" from Windows(XP Pro) and not Linux(RedHat 8), any ideas?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Couldn't see much wrong with your code. I changed it a little, try this version and see how you get on.
    Code:
    #include <stdio.h>
    
    int copy(char *src, char *dest, int start, int end);
    
    int main(void)
    {
      char  temp[20] = "testing";
      char  temp2[20] = { '\0' };
      int   spot = 2;
    
      copy(temp, temp2, spot, 5);
      printf("%s\n", temp2);
    
      return(0);
    }
    
    int copy(char *src, char *dest, int start, int end)
    {
      int x = 0;
    
      --start;
    
      while (start < end)
      {
        dest[x] = src[start];
        ++start;
        ++x;
      }
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM

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