pointer again

This is a discussion on pointer again within the C Programming forums, part of the General Programming Boards category; 0 2 56 647 3269 Ascending Original Descending 0 0 3269 2 2 647 56 56 56 647 647 2 ...

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

    Unhappy pointer again

    0 2 56 647 3269

    Ascending Original Descending
    0 0 3269
    2 2 647
    56 56 56
    647 647 2
    3269 3269 0
    Press any key to continue


    Can anyone know how to print out my original?
    what's wrong with my pointer?


    #include<stdio.h>

    #define SIZE 5

    int *rev(int *);
    void selectionSort(int *table);

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

    p1=nnn;

    selectionSort(nums);
    p2=nums;
    p3=rev(nums);


    for(g=0; g<SIZE; g++)
    {
    nnn[g]=nums[g];
    }

    printf("Ascending\tOriginal\tDescending\n");

    for(g=0; g<SIZE; g++)
    {
    printf("%d\t\t%d\t\t%d\n", *p2++, *p1++, *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);
    }

    /**********SORT *********************/

    void selectionSort(int *table)
    {
    int col, walker, smallest, temp;

    for(col=0; col<SIZE; col++)
    {
    smallest=col;

    for(walker=col+1; walker<=SIZE; walker++)
    {
    if(table[walker] < table[smallest])
    {
    smallest = walker;
    }
    }

    temp = table[col];
    table[col] = table[smallest];
    table[smallest] = temp;
    }

    return;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    108

    p1 = nnn[0];

    I may be wrong, but the above statement should be used in the main function, not " p1 = nnn; ".

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Every pointer assigned to a declared and defined array in your code, should point to the first element in that array.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    p1 = nnn[0] will give you a pointer from int without a cast or differs in levels of blah blah blah warning. it depends on which compiler you use.

    nnn[0] is the value stored in that element of the array, not the address. the address would be &nnn[0], which is the same as just nnn.

    Crazy_C, this code:

    Code:
    for(g=0; g<SIZE; g++) 
    { 
    nnn[g]=nums[g];
    }
    should go before your calls to selectionSort and rev.

    you could also use

    memcpy(nnn, nums, sizeof(nums));

    to copy nums to nnn if youd like.
    not sure but you might have to include <string.h>.

    hope this helps.

    oh, yeah, this:

    for(walker=col+1; walker<=SIZE; walker++)

    is wrong.

    should be

    for(walker=col+1; walker<SIZE; walker++)

    remember, int nums[5] consist of elements 0 through 4.
    who knows what the value of nums[5] is.
    Last edited by CtrlAltKick; 03-01-2002 at 10:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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