The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 03-15-2008, 01:34 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Icon6 Returning Arrays

I'm not sure how to explain it so erm :3

I'm trying to calculate the normal of a face and I'm using a simple vector type but when I try to compile it the compiler says it's returning an array so erm, I don't get what's wrong. I know Vec3's an array but I don't see how I'm ment to return it?

Code:
typedef float Vec3[3];

Vec3 CalculateNormal(Vec3 P1, Vec3 P2, Vec3 P3)
{
    Vec3 V1, V2, V, N;
    float l;
    V1[0] = P2[0]-P1[0];
    V1[1] = P2[1]-P1[1];
    V1[2] = P2[2]-P1[2];
    
    V2[0] = P3[0]-P1[0];
    V2[1] = P3[1]-P1[1];
    V2[2] = P3[2]-P1[2];

    V[0] = (V1[1]*V2[2])-(V1[2]*V1[1]);
    V[1] = (V1[2]*V2[0])-(V1[0]*V1[2]);
    V[2] = (V1[0]*V2[1])-(V1[1]*V1[0]);

    l = sqrt((V[0]*V[0])+(V[1]*V[1])+(V[2]*V[2]));

    N[0] = V[0]/l;
    N[1] = V[1]/l;
    N[2] = V[2]/l;

    return N;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 03-15-2008, 09:38 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
well, since your method doesn't have a return type, you shouldn't return any value..

Code:
    return N;

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 03-15-2008, 09:39 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
ummm.. nevermind... i see that you use Vec3...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 03-16-2008, 05:47 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Arrays are never copied, but passed by pointer, so the compiler is complaining that you're returning a pointer, but the return type is an array type. Ultimately its preventing you from doing something which would cause undefined behaviour, like a crash or a seg fault. Your function would be returning a pointer to some memory which is allocated in your function, and therefore it will be deallocated as soon as your function returns.

Depending whether you're using C++ or not, the preferred alternative is to use an STL container such as vector, which may be copied any way you like.

If you haven't got the STL containers available, then you should create your array externally from the function, and pass it in as an argument - allowing the function to modify an existing array, rather than attempting to return a whole new array from a function. There are other alternatives, such as creating the array dynamically (Bad solution - its open to all kinds of memory management nightmares), or wrapping the array in a struct (worth considering, if you can justify turning Vec3 into a struct type - struct objects may be copied in full, so can be used as return types for a function)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 06-17-2008, 03:46 PM
Eckos Eckos is offline
Novice
Join Date: Jun 2008
Posts: 2
iTrader: (0)
Eckos is on a distinguished road
If your using C++. Just create a class like this

class Vec3
{
union
{
float xyz;
struct
{
float x, y, z;
};
};

MATH_INLINE void glNormal3d(Vec3& p1, Vec3& p2)
{
Vec3 v = Vec3(x, y, z);
Vec3 v1 = p1 - v;
Vec3 v2 = p2 - p1;
Vec3 normal = Vec3(v1.cross(v2));
normal.normalise();
normal.printVec3();
glNormal3dv(normal.ptr());
}
};

EDIT: Sorry theres no way to delete the post. Just noticed its 3 months old.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Eckos : 06-17-2008 at 03:48 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 01:12 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50