| View previous topic :: View next topic |
| Author |
Message |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jun 06, 2012 8:42 pm Post subject: any C programmer here? |
|
|
| I need some help D:
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jun 06, 2012 8:51 pm Post subject: |
|
|
| me pick me
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jun 06, 2012 9:02 pm Post subject: |
|
|
so, I'm doing this
| Code: |
typedef struct {
int x;
int y;
int z;
} Vector3;
typedef struct {
int x;
int y;
}Vector2;
typedef struct{
Vector3 parent;
Vector3 child;
}Bone;
typedef struct {
Vector3 *positions;
Vector3 *normals;
Vector2 *uvs;
Bone *bones;
}cModel;
|
using pointers on positions normals uvs and bones because I found that using that method I could use
| Code: |
cModel model1;
int size;
model.positions = malloc(size* sizeof *model.positions ); |
to make it a flexible array ?
and then use
model.positions[45912]
is it correct?
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jun 06, 2012 9:09 pm Post subject: |
|
|
yes you can do this. it's more common/idiomatic in C to use an array of size 1 actually:
http://blogs.msdn.com/b/oldnewthing/archive/2004/08/26/220873.aspx
size 0 arrays are also common:
http://stackoverflow.com/questions/295027/array-of-zero-length
however, note that 0 length array is not part of the standard (at least not c89 or c99). they are however a gnu extension. so for example, compile with -std=gnu99. if you are using c99 and your struct is going to be scoped to a function or less, you can avoid the malloc all together by using VLAs. for example:
| Code: | #include <stdlib.h>
#include <stdio.h>
struct lala_s {
int * lala;
};
void func1(int x)
{
struct lala_s s;
int arr[x];
s.lala = arr;
for(int i = 0; i < x; i++) {
s.lala[i] = i;
}
for(int i = 0; i < x; i++) {
printf("%d\n", s.lala[i]);
}
}
int main(void)
{
func1(10);
return EXIT_SUCCESS;
}
|
compile with:
| Code: | | gcc -std=c99 -Wall -pedantic lala.c -o lala |
Last edited by Slugsnack on Wed Jun 06, 2012 9:54 pm; edited 3 times in total |
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jun 06, 2012 9:20 pm Post subject: |
|
|
I'm not really sure of what C I'm using, it is the one used by Blackberry native sdk
I tried
Vector3 position[0];
and it built with no errors,
thanks :3
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jun 06, 2012 9:32 pm Post subject: |
|
|
just because it compiles without errors does not mean that it guarantees it will work. for example, gcc is not strictly conformant with the c99 standard. hence if you try to compile a source with a 0 size array it will give a warning instead of an error. you would only spot the warning if you compiled with -pedantic.
the problem in this case is that if you are not sure whether your compiler is standard conformant, you could well be relying on undefined behaviour. and by definition that is... undefined. it could do anything. restart your phone. display random porno pictures when your mother picks up the phone, etc.
at the very least check that your compiler is supporting 0 size array through some extension (this is what gnu does)
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jun 06, 2012 9:41 pm Post subject: |
|
|
Shrooms -> on a contest on blackberry playbook apps
I'm making 3D games
slugnack-> using pointers ten would be a better option ?
like what is the difference between using an array 0 or a pointer?
I believe difference between using an array 0 and an array 1 is just a little of optimization not storing a data space but with a pointer I'm not sure.
I still don't really understand pointers fully.
|
|
| Back to top |
|
 |
Channel GannoK pffrt
Reputation: 130
Joined: 12 Apr 2008 Posts: 608
|
Posted: Wed Jun 06, 2012 9:44 pm Post subject: |
|
|
Slugsnack being a bro
+1
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jun 06, 2012 9:45 pm Post subject: |
|
|
i just told you. the difference is that one is well-defined by the standard and the other is undefined behaviour (or at least is less portable if used via an extension). either you don't care about portability. in which case check for an extension or use array of size 1. otherwise just use a pointer...
there are minor optimisations but they shouldn't really concern you at this stage
|
|
| Back to top |
|
 |
Barnold Swolezzenegger Cheater
Reputation: 20
Joined: 12 Mar 2011 Posts: 45
|
Posted: Wed Jun 06, 2012 9:51 pm Post subject: |
|
|
| GannoK wrote: | Slugsnack being a bro
+1 |
You act like he's always a massive faggot. When you guys aren't being immature about everything, he's usually not bad.. You guys just always go into faggot tryhard mode and argue with him.
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jun 06, 2012 9:51 pm Post subject: |
|
|
ok ok
thanks :3
|
|
| Back to top |
|
 |
Channel GannoK pffrt
Reputation: 130
Joined: 12 Apr 2008 Posts: 608
|
Posted: Wed Jun 06, 2012 9:56 pm Post subject: |
|
|
| Apotheoun wrote: | | GannoK wrote: | Slugsnack being a bro
+1 |
You act like he's always a massive faggot. When you guys aren't being immature about everything, he's usually not bad.. You guys just always go into faggot tryhard mode and argue with him. |
I have never had a problem with slugsnack.......
_________________
|
|
| Back to top |
|
 |
Barnold Swolezzenegger Cheater
Reputation: 20
Joined: 12 Mar 2011 Posts: 45
|
Posted: Wed Jun 06, 2012 9:57 pm Post subject: |
|
|
| I was generalizing the forum.
|
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Thu Jun 07, 2012 7:25 am Post subject: |
|
|
| Apotheoun wrote: | | I was generalizing the forum. |
Based on a minority.
|
|
| Back to top |
|
 |
Butters Grandmaster Cheater Supreme
Reputation: 71
Joined: 17 Mar 2007 Posts: 1373
|
Posted: Thu Jun 07, 2012 8:15 am Post subject: |
|
|
| Aniblaze wrote: | | Apotheoun wrote: | | I was generalizing the forum. |
Based on a minority. | Which consists of about 5 people.
|
|
| Back to top |
|
 |
|