Thread: **pointer explanation, please for a C++ programmer

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    **pointer explanation, please for a C++ programmer

    I have this code, and I need to understand it better. I'm hoping one of you will explain, ESPECIALLY the part using the double pointer:

    Code:
    typedef strudt{    // I get this part, I include it because it might help with the rest
         float x, y; 
         float z;
         flozt r, g, b;
    } Vert;
    
    typedef struct {
         int n;
         Vert *verts;  //ok, this is for an array of verts
    }Poly;
    
    // ~~~~here is where I'm a little confused: ~~~
    Poly *readPoly(FILE *f){  //how do I use this in main, or what would it look like in C++?
    	int i;
    	Poly *poly = (Poly *) malloc(sizeof(Poly));  //dynamic allocation of an array?
    	fscanf(f, "%d", &poly->n);   //c version of reading in a file?
    	poly->verts = (Vert*) malloc(poly->n*sizeof(Vert));  //another dynamic allocatin of an array?
    	for(i=0; i< poly->n; i++)
    		fscanf(f, "%f %f %f %f %f %f", &poly->verts[i].x, &poly->verts[i].y /*etc?*/ ); 
    	return poly;
    }
    
    //ok, here is main:
    int main(void){
    
    	int i, numPolys;               // total number of poly's
    
    	Poly **polys;                  //array of polys   ... I don't understand pointers to pointers
    
    	scanf("%d", &numPolys);  //c stuff is so weird to me
    
    	polys = (Poly **) malloc(numPolys*sizeof(Poly*));   //why make another array here?
    
    	for (i = 0; i < numPolys; i++)  //start reading in the files, correct?
    		polys[i] = readPoly(f);  // f should be declared as the file name, correct?  because it gives an error now
    		return 0;
    
    }
    I'm trying to understand the entire part from Poly *readPoly(FILE *F){ ... on. C programmers write input files differently, and I've been looking in a C book that isn't very helpful with this particular style of writing C code. I know it looks basic for C programmers, but it doesn't look baisc for a C++ programmer.

    Thank you for your patience
    Last edited by tms43; 09-08-2007 at 11:56 AM. Reason: left some code out

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    fyi, there really isn't anything there that a c++ programmer wouldn't be expected to understand (aside from C API calls - which could be looked up with a little effort). the readPoly function reads a single polygon from the current position of a given file (look into fopen() and related functions) and returns a pointer to the allocated memory. so to have an array of them you need an array of pointers to polygons. using typedefs:

    typedef Poly * poly_p;
    typedef poly_p * poly_p_array;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    Poly **polys;
    This is a pointer to a pointer. So the first malloc() allocates an array of pointers to Poly's, then the malloc() in readPoly() allocates the actual Poly structs.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's a tutorial on standard C file I/O: http://www.cprogramming.com/tutorial/cfileio.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  3. Programmer (vs) Coder
    By Brain Cell in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-24-2005, 01:59 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM