Thread: Confused with double pointers

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Confused with double pointers

    I have been trying for hours to create a specific prototype program that determines a pascal's triangle for a give number of rows. However, prototype must have the return type of int**. I just recently learnt about pointers and I would really appreciate it if anyone could give me a hint as to why my attempt of the function doesn't work.
    As well, i am not sure how I can check if my return value actually points to the pascal triangle.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int **getPascalTriangle(int n)
    {
        int i, j, value;
        int *c;
        int ** pp;
        int k, gap;
    
    
        c=(int*)malloc(n*sizeof(int));
    
    
        printf("\n");
        gap=n;
    
    
        for(i=0;i<=n-1;i++)
        {   value=1;
    
    
            for(k=gap;k>=0;k--)
                printf(" ");
                gap--;
    
    
                for(j=0;j<=i;j++)
                    {
                    c=&value;
                    value=(*c*(i-j)/(j+1));
                    }
    
    
            printf("\n");
    }
        *pp=c;
        return pp;
    }
    
    
     int main(void)
    {
        int n;
    
    
        printf("Please enter value for number of rows in PAscal's triangle:");
        scanf("%d",&n);
    
    
        getPascalTriangle(n);
    
    
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't seem to have allocated space for the dynamic 2D array. You did allocate space for a dynamic 1D array, but this assignment doesn't work as there was no space allocated for the dynamic 2D array:
    Code:
    *pp=c;
    even if you did allocate the space, it still wouldn't do what you want since it only copies a pointer. Furthermore, you wrote:
    Code:
    c=&value;
    which overwrites C, thus resulting in a memory leak since c pointed to what was returned by malloc. Speaking of that, you should not be printing anything in getPascalTriangle. Rather, call it in the main function, then use its return value to print the Pascal's triangle. Finally, free the memory allocated.

    By the way, although you do appear to have made some effort to indent your program properly, it still needs more work in that area.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Pointers
    By JoshD75 in forum C Programming
    Replies: 7
    Last Post: 03-28-2011, 09:43 PM
  2. A little lost and confused with my pointers :/
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2009, 01:32 PM
  3. Still confused with pointers
    By desmond5 in forum C Programming
    Replies: 8
    Last Post: 02-23-2008, 09:32 PM
  4. confused with pointers
    By Mahdi123 in forum C Programming
    Replies: 2
    Last Post: 04-25-2007, 01:08 PM
  5. Very confused with pointers
    By killerasp in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2002, 06:44 PM