Thread: pointers

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Unhappy pointers

    #include<stdio.h>

    #define SIZE 5

    int *rev(int *);

    int main(void)
    {
    int nums[5]={56, 0, 647, 3269, 2};
    int g, *p2, *p3;

    p2=nums;
    p3=rev(nums);

    for(g=0; g<SIZE; g++)
    {
    printf("%d\t%d\n", *p2++, *p3++);
    }
    return 0;
    }

    /***************************** rev **********************/

    int *rev(int *pRev)
    {
    int i, numPrint;

    for(i=SIZE-1, numPrint=0; i>=0; i--)
    {
    printf("%d ", pRev[i]);
    }
    printf("\n\n");

    return(pRev);
    }

    /*
    2 3269 647 0 56

    56 56
    0 0
    647 647
    3269 3269
    2 2
    Press any key to continue
    */

    What's wrong in my program?

    Howcome it won't print out my reverse order?

    my answer should :

    56 2
    0 3269
    647 647
    3269 0
    2 56

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>printf("%d ", pRev[i]);
    %d is an int which is 4bytes wide
    and pRev is an int pointer, in no way is to an array
    Code:
    int RevInt(int iNum)
    {
    char sTemp[5]={'\0'},pFinal[5]={'\0'};
    int  i,j;
    
    sprintf(sTemp,"%d",iNum);
    
    for(i=0,j=3;i<sizeof(int);i++,j--)
      sFinal[i]=sTemp[j];
    
    return atoi(sFinal);
    }
    or similar
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Unregistered
    Guest

    re

    #include<stdio.h>

    #define SIZE 5

    int *rev(int *);

    int main(void)
    {
    int nums[5]={56, 0, 647, 3269, 2};
    int g, *p2, *p3;

    p2=nums;
    p3=rev(nums);

    for(g=0; g<SIZE; g++)
    {
    printf("%d\t%d\n", *p2++, *p3--);
    }
    return 0;
    }

    /***************************** rev **********************/

    int *rev(int *pRev)
    {
    int i, numPrint;

    for(i=SIZE-1, numPrint=0; i>=0; i--)
    {
    printf("%d ", *pRev++);
    }

    printf("\n\n");

    return(--pRev);
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Quote Originally Posted by Crazy_C
    #include<stdio.h>

    #define SIZE 5

    int *rev(int *);

    int main(void)
    {
    int nums[5]={56, 0, 647, 3269, 2};
    int g, *p2, *p3;

    p2=nums;
    p3=rev(nums);

    for(g=0; g<SIZE; g++)
    {
    printf("%d\t%d\n", *p2++, *p3++);
    }
    return 0;
    }

    /***************************** rev **********************/

    int *rev(int *pRev)
    {
    int i, numPrint;

    for(i=SIZE-1, numPrint=0; i>=0; i--)
    {
    printf("%d ", pRev[i]);
    }
    printf("\n\n");

    return(pRev);
    }

    /*
    2 3269 647 0 56

    56 56
    0 0
    647 647
    3269 3269
    2 2
    Press any key to continue
    */

    What's wrong in my program?

    Howcome it won't print out my reverse order?

    my answer should :

    56 2
    0 3269
    647 647
    3269 0
    2 56


    Problem with ur code is that p2 and p3 are pointing to the same starting address of the array nums[].

    In function rev(),you are not modifying the pointer pRev and just returning it .

    If u have to get the o/p u r expecting,then create another array and pass it as a sec arg to the function rev.

    assign the reverse elements of the array nums[] to that temp array and then print the array.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Oh wow nice bump dude!!!!!!
    Woop?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by novacain
    >>printf("%d ", pRev[i]);
    %d is an int which is 4bytes wide
    and pRev is an int pointer, in no way is to an array
    No. An int does not have to be four bytes. The following
    requirements have been set:

    Type short must be at least 16 bits.
    Type long must be at least 32 bits.

    Type int has only the following restrictions:

    INT_MIN -(2^15-1) or simply put, -32767
    INT_MAX 2^15-1 or simply put, 32,767

    Since these requirements are met by the standard in 16 bits, it is allowed. There
    is nothing which states that int must be 4 bytes.

    We like to be pedantic here.

    Quote Originally Posted by novacain
    Code:
    int RevInt(int iNum)
    {
    char sTemp[5]={'\0'},pFinal[5]={'\0'};
    int  i,j;
    
    sprintf(sTemp,"%d",iNum);
    
    for(i=0,j=3;i<sizeof(int);i++,j--)
      sFinal[i]=sTemp[j];
    
    return atoi(sFinal);
    }
    or similar
    And what happens when you're passed the number "10000"? Oops.

    [edit]
    Doh. Didn't even look at the post date. I guess I should have posted this two years ago!
    [/edit]

    Quzah.
    Last edited by quzah; 09-08-2004 at 12:21 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 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