Like Tree4Likes

Having problems with Pointers

This is a discussion on Having problems with Pointers within the C Programming forums, part of the General Programming Boards category; Code: The are many different ways to sort data . For some applications, e.g., making movies , it is better ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22

    Having problems with Pointers

    Code:
     The are many different ways to  sort data. For some applications, e.g.,  making movies, it is better to sort   pointers to the data, rather than sort the  data itself. You must write a program to demonstrate this kind of activity. The program must ... 
    
    
    • Define data types for ... (0.2%)
      • A pointer to an integer
      • An array of five integers
      • An array of five pointers to integers
    • Have an intialization function that ... (0.3%)
      • Initializes the elements of an array of five integers to random integers. Use rand to generate the random integers, using srand with the return from getpid to seed the random number generation.
      • Initializes the elements of an array of five pointers to integers to point to the corresponding elements of the array of integers. (The structure is anaolgous to the one created in this example that we discussed in class.)
    • Have a function that prints an array of five integers. (0.2%)
    • Have a function that prints the integers pointed to by an array of five pointers to integers. (0.3%)
    • Have a function that uses a bubble-sort to sort an array of five integers, in ascending order of the integers. (0.4%)
      • And to make it fun, this function may not contain any [ or ] characters. (You may want to implement it with []s first, and then work out how to remove them later :-).
    • Have a function that uses a bubble-sort to sort an array of five pointers to integers, in ascending order of the integers pointed to by the pointers (i.e., do not sort the integer array - sort the array of pointers so that when you print it, the integers come out in ascending order). (0.4%)
    • Have a main function that ... (0.2%)
      • Declares an array of five integers and an array of five pointers to integers.
      • Initializes the arrays (as described above).
      • Prints the array of integers.
      • Sorts the array of pointers (as described above).
      • Prints the integers pointed to by the array of pointers.
      • Sorts the array of integers.
      • Prints the array of integers.
      • Prints the integers pointed to by the array of pointers.
    The output from a sample run should look like this. The picture on the right shows the array of integers and array of pointers after intialization, sorting the array of pointers, and sorting the array of integers. http://web.cs.miami.edu/home/gallo/C...nterArrays.gif ---- Initialized array of integers ---- 0 : 958486403 1 : 1006139074 2 : 893180240 3 : 769601150 4 : 392522169 ---- Sorted array of pointers ---- 0 : 392522169 1 : 769601150 2 : 893180240 3 : 958486403 4 : 1006139074 ---- Sorted array of integers ---- 0 : 392522169 1 : 769601150 2 : 893180240 3 : 958486403 4 : 1006139074 ---- Array of pointers ---- 0 : 1006139074 1 : 958486403 2 : 893180240 3 : 392522169 4 : 769601150
    This is the assignment

    Code:
         1    //-----------------------------------------------------------------------------
         2    #include <stdio.h>
         3    #include <stdlib.h>
         4    
         5    #define TRUE 1
         6    #define FALSE 0
         7    const int ARRAYSIZE = 5;
         8    
         9    typedef int *IntPointer;
        10    
        11    typedef int IntArray[];
        12    
        13    typedef int *PointerArray[];
        14    
        15    //-----------------------------------------------------------------------------
        16    
        17    void initArray(IntArray array,PointerArray pointer) {
        18    
        19    int index;
        20    srand(getpid());
        21    for (index = 0;index < ARRAYSIZE;index++){
        22    array[index] = (int)rand();
        23    }
        24    
        25    for (index = 0;index < ARRAYSIZE;index++){
        26    pointer[index] = &array[index];
        27    }
        28    }
        29    //-----------------------------------------------------------------------------
        30    
        31    void printArray(IntArray array){
        32        int index;
        33        for (index = 0;index < ARRAYSIZE;index++) {
        34            printf("%d : %10d\n",index,array[index]);
        35    
        36        }
        37        printf("\n");
        38    }
        39    //-----------------------------------------------------------------------------
        40    
        41    void printArrayPointers(PointerArray pointer){
        42        int index;
        43        for (index = 0;index < ARRAYSIZE;index++) {
        44            printf("%d : %10d\n",index,*pointer[index]);
        45        }
        46        printf("\n");
        47    }
        48    //-----------------------------------------------------------------------------
        49    void bubbleSortArray(IntArray array){
        50      int a, b, temp;
        51     
        52      for (a = (ARRAYSIZE - 1); a > 0; a--)
        53      {
        54        for (b = 1; b <= a; b++)
        55        {
        56          if ((*array+(b-1)) > (*array+b))
        57          {
        58            temp = (*array+(b-1));
        59            *(array+(b-1)) = *(array+b);
        60            *(array+b) = temp;
        61          }
        62        }
        63      }
        64    }
        65    //-----------------------------------------------------------------------------
        66    void bubbleSortPointer(PointerArray pointer){
        67        int a, b;
        68        IntPointer temp;
        69     
        70      for (a = (ARRAYSIZE - 1); a > 0; a--)
        71      {
        72        for (b = 1; b <= a; b++)
        73        {
        74          if (*(pointer+(b-1)) > *(pointer+b))
        75          {
        76            temp = *(pointer+(b-1));
        77            *(pointer+(b-1)) = *(pointer+b);
        78            *(pointer+b) = temp;
        79          }
        80        }
        81      }
        82    }
        83    
        84    //-----------------------------------------------------------------------------
        85    int main(void) {
        86    IntArray array[ARRAYSIZE];
        87    PointerArray pointer[ARRAYSIZE];
        88    
        89    
        90    InitArray(array,pointer);
        91    printArray(array);
        92    bubbleSortPointers(pointer);
        93    printArrayPointers(pointer);
        94    bubbleSortArray(array);
        95    printArray(array);
        96    printArrayPointers(pointer);
        97    
        98    
        99        return(EXIT_SUCCESS);
       100    }
       101  //-----------------------------------------------------------------------------
    Here's the problem:

    ArraysAndPointers.c: In function ‘main’:
    ArraysAndPointers.c:86: error: array type has incomplete element type
    ArraysAndPointers.c:87: error: array type has incomplete element type

    Can you help me figure out what's wrong?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by eddieL91 View Post
    Can you help me figure out what's wrong?
    You found the typedef keyword. That's what's wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Quote Originally Posted by quzah View Post
    You found the typedef keyword. That's what's wrong.


    Quzah.
    Thank you... that was not helpful at all. Especially since the assignment specifically says:
    Define data types for ... (0.2%)
    • A pointer to an integer
    • An array of five integers
    • An array of five pointers to integers

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see you defining a type for "an array of five integers" anywhere, do you?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by eddieL91
    Especially since the assignment specifically says:
    Your assignment has poor requirements: using a typedef for a pointer merely makes it less obvious that we are dealing with a pointer. This is good in a certain case that you will learn about later, but generally it is a Bad Thing.
    rags_to_riches likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Quote Originally Posted by quzah View Post
    I don't see you defining a type for "an array of five integers" anywhere, do you?


    Quzah.
    const int ARRAYSIZE = 5;
    typedef int IntArray[];
    IntArray array[ARRAYSIZE-1];

    I have that in different places, is that not how you declare it? or maybe I need:
    typedef int[] IntArray[ARRAYSIZE];
    IntArray array;

    I'm going to try that now, if I'm wrong, please let me know if you can help any more. I feel like you're probably telling me the answer but I'm just not seeing it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by eddieL91
    const int ARRAYSIZE = 5;
    typedef int IntArray[];
    IntArray array[ARRAYSIZE-1];

    I have that in different places, is that not how you declare it?
    It should be:
    Code:
    #define ARRAYSIZE 5
    
    typedef int IntArray[ARRAYSIZE];
    
    IntArray array;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    My TA kept saying I needed to put typedef int IntArray[ARRAYSIZE], but every time I did that it gave me an error about defining the scope, I didn't understand. The thing I was missing was the #define ARRAYSIZE.

    It brought up a looootttt of other errors, but I'm going to work on this on my own until I can't figure it out again. Thanks!
    laserlight likes this.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    I fixed that now, but for some reason I'm getting this
    error: ‘IntArray’ undeclared (first use in this function).

    I moved some stuff around, so here's the code.

    Code:
         1    //-----------------------------------------------------------------------------
         2    #include <stdio.h>
         3    #include <stdlib.h>
         4    
         5    #define ARRAYSIZE 5;
         6    
         7    typedef int *IntPointer;
         8    
         9    typedef int IntArray[ARRAYSIZE];
        10    
        11    typedef int *PointerArray[ARRAYSIZE];
        12    
        13    //-----------------------------------------------------------------------------
        14    
        15    void printArray(IntArray array){
        16        int index;
        17        for (index = 0;index < ARRAYSIZE;index++) {
        18            printf("%d : %10d\n",index,array[index]);
        19    
        20        }
        21        printf("\n");
        22    }
        23    //-----------------------------------------------------------------------------
        24    
        25    void printArrayPointers(PointerArray pointer){
        26        int index;
        27        for (index = 0;index < ARRAYSIZE;index++) {
        28            printf("%d : %10d\n",index,*pointer[index]);
        29        }
        30        printf("\n");
        31    }
        32    //-----------------------------------------------------------------------------
        33    void bubbleSortArray(IntArray array){
        34      int a, b, temp;
        35     
        36      for (a = (ARRAYSIZE - 1); a > 0; a--)
        37      {
        38        for (b = 1; b <= a; b++)
        39        {
        40          if ((*array+(b-1)) > (*array+b))
        41          {
        42            temp = (*array+(b-1));
        43            *(array+(b-1)) = *(array+b);
        44            *(array+b) = temp;
        45          }
        46        }
        47      }
        48    }
        49    //-----------------------------------------------------------------------------
        50    void bubbleSortPointer(PointerArray pointer){
        51        int a, b;
        52        IntPointer temp;
        53     
        54      for (a = (ARRAYSIZE - 1); a > 0; a--)
        55      {
        56        for (b = 1; b <= a; b++)
        57        {
        58          if (*(pointer+(b-1)) > *(pointer+b))
        59          {
        60            temp = *(pointer+(b-1));
        61            *(pointer+(b-1)) = *(pointer+b);
        62            *(pointer+b) = temp;
        63          }
        64        }
        65      }
        66    }
        67    
        68    //-----------------------------------------------------------------------------
        69    
        70    void initArray(IntArray array,PointerArray pointer) {
        71    
        72    int index;
        73    srand(getpid());
        74    for (index = 0;index < ARRAYSIZE;index++){
        75    array[index] = (int)rand();
        76    }
        77    
        78    for (index = 0;index < ARRAYSIZE;index++){
        79    pointer[index] = &array[index];
        80    }
        81    }
        82    //-----------------------------------------------------------------------------
        83    int main(void) {
        84    IntArray array;
        85    PointerArray pointer;
        86    
        87    
        88    InitArray(array,pointer);
        89    printArray(array);
        90    bubbleSortPointers(pointer);
        91    printArrayPointers(pointer);
        92    bubbleSortArray(array);
        93    printArray(array);
        94    printArrayPointers(pointer);
        95    
        96    
        97        return(EXIT_SUCCESS);
        98    }
        99    //-----------------------------------------------------------------------------
    ArraysAndPointers.c:9: error: expected ‘]’ before ‘;’ token
    ArraysAndPointers.c:11: error: expected ‘]’ before ‘;’ token
    ArraysAndPointers.c:15: error: expected ‘)’ before ‘array’
    ArraysAndPointers.c:25: error: expected ‘)’ before ‘pointer’
    ArraysAndPointers.c:33: error: expected ‘)’ before ‘array’
    ArraysAndPointers.c:50: error: expected ‘)’ before ‘pointer’
    ArraysAndPointers.c:70: error: expected ‘)’ before ‘array’
    ArraysAndPointers.c: In function ‘main’:
    ArraysAndPointers.c:84: error: ‘IntArray’ undeclared (first use in this function)
    ArraysAndPointers.c:84: error: (Each undeclared identifier is reported only once
    ArraysAndPointers.c:84: error: for each function it appears in.)
    ArraysAndPointers.c:84: error: expected ‘;’ before ‘array’
    ArraysAndPointers.c:85: error: ‘PointerArray’ undeclared (first use in this function)
    ArraysAndPointers.c:85: error: expected ‘;’ before ‘pointer’
    ArraysAndPointers.c:88: error: ‘array’ undeclared (first use in this function)
    ArraysAndPointers.c:88: error: ‘pointer’ undeclared (first use in this function)



    A lot of these errors seem to stem from "ArraysAndPointers.c:84: error: ‘IntArray’ undeclared (first use in this function)"

    I can't seem to figure out what's up with it now. IntArray was declared as a typedef...
    Last edited by eddieL91; 03-22-2012 at 09:52 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You probably have a typo error on this line:
    Code:
    #define ARRAYSIZE 5;
    That is, you don't want the semi-colon here. A preprocessor directive is not terminated by a semi-colon.
    eddieL91 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    thank you so much. Now I'm having trouble because the sortpointers methods aren't working, but I'm going to try to figure those out. any help would be appreciated though.

    This is the page of notes I'm reading from

    Pointers

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by eddieL91
    Now I'm having trouble because the sortpointers methods aren't working
    One thing that you should brush up on is your indentation. You did pretty well for the various other functions (though being consistent in number of spaces to indent would be better), but you need to format better for initArray and main. This is especially important for initArray because you need to see clearly where your for loops start and end.

    Quote Originally Posted by eddieL91
    This is the page of notes I'm reading from

    Pointers
    Tee hee, the link to the Virgin Airlines thing made me smile
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Yeah, I understand the indentation thing, I was just trying to muscle through the thing before I fixed the style.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Also, thank you so much for all the help you gave me. Both of you.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by eddieL91 View Post
    Thank you... that was not helpful at all. Especially since the assignment specifically says:
    [/LIST]
    That doesn't require a typedef, though. FTR, I don't agree typedefing is bad, only with the how it's used.

    * A pointer to an integer

    int *int_ptr;

    * An array of five pointers to integers

    int *int_ptr_array[5];
    Last edited by Cynic; 03-23-2012 at 01:14 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with pointers
    By Poincare in forum C Programming
    Replies: 3
    Last Post: 04-26-2009, 06:18 AM
  2. new to pointers having problems
    By traxy in forum C Programming
    Replies: 2
    Last Post: 05-18-2008, 02:32 AM
  3. VC++ problems with pointers
    By GOBLIN-85 in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2007, 07:47 PM
  4. Problems with pointers
    By rhildebrand in forum C Programming
    Replies: 2
    Last Post: 10-29-2003, 06:57 PM
  5. pointers problems
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2003, 07:50 AM

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