Created
September 18, 2015 20:28
-
-
Save arbalest/f3576f73cb6e5222400a to your computer and use it in GitHub Desktop.
Quick and simple demonstration about multiple ways of accessing arrays within a struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct _vertexStruct { | |
float vertex[2]; | |
float color[3]; | |
} vertexStruct; | |
int main(void) { | |
vertexStruct vertices[] = { | |
{ {1.0, 0.0}, {1.0, 1.0, 1.0} }, | |
{ {0.0, 1.0}, {0.0, 1.0, 0.0} } | |
}; | |
printf("The second color's red is %f\n", vertices[1].color[0]); | |
printf("The second color's green is %f\n", vertices[1].color[1]); | |
// These should each be the same | |
printf("The address of the vertices array is %p\n", vertices); | |
printf("The address of the first vertices element is %p\n", &vertices[0]); | |
printf("The address of the first vertex element is %p\n", &vertices[0].vertex); | |
printf("The address of the first vertex indexed element is %p\n", &vertices[0].vertex[0]); | |
// These should be the same | |
printf("The address of the first color element is %p\n", &vertices[0].color); | |
printf("The address of the first color indexed element is %p\n", &vertices[0].color[0]); | |
// And these should be the same | |
printf("The address of the second color element is %p\n", &vertices[1].color); | |
printf("The address of the second color indexed element is %p\n", &vertices[1].color[0]); | |
// your code goes here | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment