Thread: Parsing and returning array pointers?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Help needed with pointing to arrays.

    I'm trying to write a function that takes as input 3 arrays, does some calculations, puts some values into a global array, then returns a pointer to that array.

    function is:

    Code:
    GLfloat calcNormal(GLfloat* B1, GLfloat* B2, GLfloat* T)
    {
        //calculations here.
        return normArray[0];
    }
    If I define 3 arrays, A(0,0,0), B(1,0,0), and C(0,1,0), then call printf("%f\n", calcNormal(A[0], B[0], C[0])[2]), compiler says arguments are of wrong type. If I try passing &A[0], &B[0] and &CP[0], compiler says subscripted value is neither array nor pointer.

    How should I be passing array pointers to the function?
    Last edited by thealmightyone; 03-26-2009 at 08:25 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's no need to return a pointer to a global array.

    Replace the ampersand & with a *, and you'll get a gold star!

    You can't define arrays like that. You need square brackets for each dimension of the array:

    int Array[][][], makes a 3D array of int's.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    I do need to return a pointer/array of some sort, due to its actual implementation:

    Code:
    GLfloat normals[8][3] = {calcNormal(B[0], B[1], T[0]), calcNormal(B[1], B[2], T[1]),...}
    I tried your suggestions, and got 3 errors (one for each argument): invalid type argument of 'unary *' (have 'GLfloat')
    Last edited by thealmightyone; 03-26-2009 at 08:58 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not an expert on pointers, but I don't think it's safe to return (and work with), a pointer to a local function's array.

    That data might still be good, but it will be overwritten, any old time since the arrays has gone out of scope.

    If I were to do this, I'd make the arrays in the calling function, pass them to the other functions as needed, and then there would be no trouble using the array's data.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    normArrays is a global array, not local.
    Probably worth clarifying that I don't want the contents of normals to point to normArray, but to be set to the values of normArray (set by the call to calcNormal).
    Last edited by thealmightyone; 03-26-2009 at 09:11 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, it's no problem returning a pointer to an array. A pointer to an array is just another type of pointer, so that's no problem. If the array is going to be destroyed as a local variable, then you're playing with fire to use that returned pointer.

    I'm no expert on pointers, however. Let me back out of here, and perhaps one of them will give you a more cogent reply.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    I'm not an expert on pointers, but I don't think it's safe to return (and work with), a pointer to a local function's array.
    No, it's not, since pointers are just variables with addresses. They don't change scope rules or anything like that.
    Once the array goes out of scope, it's destroyed and the pointer will be dangling.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Elysia, do you have any help for me?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How does the call to calcNormal look like?
    Further, why are you assigning to a global variable and then returning it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Post #3 answers your first question.

    To answer your second question, I obviously can't use a local array, as the array would be lost when calcNormal ends. This is not the issue at all though.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by thealmightyone View Post
    Post #3 answers your first question.
    Of course. Silly of me.
    But what I needed to ask was how the arrays you pass are declared?

    To answer your second question, I obviously can't use a local array, as the array would be lost when calcNormal ends. This is not the issue at all though.
    You can make a local array and pass a pointer to it to the calcNormal function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    The arrays B and T are of the form:

    Code:
    T[8][3] = { {9.0, 4.6, -1.0}, etc}
    B[8][3] = { {4.5, 1.0, -10.0}, etc}
    If I decide to improve the code, I will localise stuff. For now, I just want to get my head around using array pointers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are they of type GLfloat? If yes, then the code should work... Unless you have mixed and twisted it a little...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    The code does compile if I don't include the line in post #1 (which is not needed for program), but I'm pretty sure there's still a problem.

    When array 'normals' tries to set itself to the values in normArray, blending does not work (which requires the arrays in 'normals'). If I replace one of the calls with actual numbers (calculated by hand), blending works for that normal. I have no ides what calcNormal is putting into normals, but it's not working as it should.

    Hopefully, that makes sense.
    Last edited by thealmightyone; 03-26-2009 at 10:29 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This works and compiles (although is warns about truncation from double to float):
    Code:
    typedef float GLfloat;
    
    GLfloat calcNormal(GLfloat* B1, GLfloat* B2, GLfloat* T)
    {
        //calculations here.
        //return normArray[0];
    	return 0.0f;
    }
    
    int main()
    {
    	float T[2][3] = { {9.0, 4.6, -1.0}, { 0.0f, 0.0f, 0.0f } };
    	float B[2][3] = { {4.5, 1.0, -10.0}, { 0.0f, 0.0f, 0.0f } };
    	calcNormal(T[0], T[1], B[0]);
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coverting array to string
    By do_kev in forum C Programming
    Replies: 5
    Last Post: 03-13-2008, 03:15 PM
  2. Struct/parse returning wrong information
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 01:39 PM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM