﻿<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=Dev%3ASource%2FData_Structures%2FC_Structures_and_Pointers</id>
	<title>Dev:Source/Data Structures/C Structures and Pointers - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=Dev%3ASource%2FData_Structures%2FC_Structures_and_Pointers"/>
	<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/C_Structures_and_Pointers&amp;action=history"/>
	<updated>2026-05-11T14:24:07Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/C_Structures_and_Pointers&amp;diff=55342&amp;oldid=prev</id>
		<title>Yamyam: 1版 をインポートしました</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/C_Structures_and_Pointers&amp;diff=55342&amp;oldid=prev"/>
		<updated>2018-06-28T17:51:53Z</updated>

		<summary type="html">&lt;p&gt;1版 をインポートしました&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;2018年6月28日 (木) 17:51時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ja&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(相違点なし)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Yamyam</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/C_Structures_and_Pointers&amp;diff=55341&amp;oldid=prev</id>
		<title>wiki&gt;Adc: Minor code fixes.</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/C_Structures_and_Pointers&amp;diff=55341&amp;oldid=prev"/>
		<updated>2012-04-27T21:03:51Z</updated>

		<summary type="html">&lt;p&gt;Minor code fixes.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= C Data Structures and Pointers=&lt;br /&gt;
&lt;br /&gt;
_&amp;quot;.... you need to learn about data structures. These are things like those lists that keep keep showing up in Blender.&amp;quot;_ --stiv&lt;br /&gt;
&lt;br /&gt;
=== Defining Structures===&lt;br /&gt;
Structures are typically collections of variables and such that we want to&lt;br /&gt;
keep together.  A simple example would be found in source/blender/makesdna/&lt;br /&gt;
DNA_meshdata_types.h.  Scrolling down to line 67 we see the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct MCol { &lt;br /&gt;
     char a, r, g, b;&lt;br /&gt;
} MCol;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We have declared a structure type called MCol.  Inside this struct we have&lt;br /&gt;
variables or structure members.  In this case there are four variables of type&lt;br /&gt;
char, one each for alpha, red, green, and blue.  The [[DeveloperWiki/MCol|MCol]] after &amp;quot;}&amp;quot; means we&lt;br /&gt;
just created an instance of [[DeveloperWiki/MCol|MCol]] called MCol.  This last [[DeveloperWiki/MCol|MCol]] is a variable&lt;br /&gt;
which every time we use it we can get its alpha, red, green, and blue structure&lt;br /&gt;
variables.&lt;br /&gt;
&lt;br /&gt;
One thing of note is the typedef statement.  Without it every time we wanted to&lt;br /&gt;
create a new structure variable we would have to do this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct MCol MYCol;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Which would create a structure variable named [[DeveloperWiki/MYCol|MYCol]] of type MCol.  To simplify&lt;br /&gt;
we use typedef and save some typing like so:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
MCol MYCol;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This is a bit of a simple explanation of typedef and there are other uses for it.&lt;br /&gt;
&lt;br /&gt;
=== Assigning Structure Members===&lt;br /&gt;
&lt;br /&gt;
So now we have this structure and it had structure members.  How do we get the&lt;br /&gt;
data of these members in and out of the structure?  Well, there are a couple of&lt;br /&gt;
ways.  The first way is to assign the structure members when declaring the&lt;br /&gt;
structure like so:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct MCol { &lt;br /&gt;
     char a, r, g, b;&lt;br /&gt;
} MCol = { 255, 0, 128, 255 };&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This would assign a=255, r=0, g=128, and b=255 to the variables in the struct.&lt;br /&gt;
&lt;br /&gt;
Option two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct MCol { &lt;br /&gt;
     char a, r, g, b;&lt;br /&gt;
} MCol;&lt;br /&gt;
&lt;br /&gt;
MCol MCol = { 255, 0, 128, 255 };&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice this time we assign the member variables and structure variable separate from&lt;br /&gt;
the structure declaration.&lt;br /&gt;
&lt;br /&gt;
And finally after declaring a structure we can assign values to members by using&lt;br /&gt;
this syntax:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
MCol.a = 255;&lt;br /&gt;
MCol.r = 0;&lt;br /&gt;
MCol.g = 128;&lt;br /&gt;
MCol.b = 255;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Structure Code Example===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/* Code listing for BabyBlend */&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
     /* declare structure */    &lt;br /&gt;
     typedef struct MVert {&lt;br /&gt;
          float co[3];          /* array for vertex coordinates */&lt;br /&gt;
          short no[3];          /* array for vertex normals */&lt;br /&gt;
          char flag, mat_nr;    /* variables for a flag and material index */&lt;br /&gt;
     } MVert;  &lt;br /&gt;
&lt;br /&gt;
     /* create variable of type MVert */ &lt;br /&gt;
     MVert vert; &lt;br /&gt;
     char key_press = '\0'; &lt;br /&gt;
 &lt;br /&gt;
     printf(&amp;quot;\nWelcome to BabyBlender\n\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;To create a vertex press 'V'\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;Key: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%c&amp;quot;, &amp;amp;key_press);&lt;br /&gt;
     switch (key_press) {&lt;br /&gt;
         case 'V': &lt;br /&gt;
         case 'v': &lt;br /&gt;
              /* assign values to the structure's array */&lt;br /&gt;
              printf(&amp;quot;Enter X coordinate: &amp;quot;);&lt;br /&gt;
              scanf(&amp;quot;%f&amp;quot;, &amp;amp;vert.co[0]); &lt;br /&gt;
              printf(&amp;quot;Enter Y coordinate: &amp;quot;); &lt;br /&gt;
              scanf(&amp;quot;%f&amp;quot;, &amp;amp;vert.co[1]); &lt;br /&gt;
              printf(&amp;quot;Enter Z coordinate: &amp;quot;); &lt;br /&gt;
              scanf(&amp;quot;%f&amp;quot;, &amp;amp;vert.co[2]);&lt;br /&gt;
&lt;br /&gt;
              /* print the values from the structure's array */&lt;br /&gt;
              printf(&amp;quot;\nYour vertex is is located at\n&amp;quot;);&lt;br /&gt;
              printf(&amp;quot;X: %f\n&amp;quot;, vert.co[0]);&lt;br /&gt;
              printf(&amp;quot;Y: %f\n&amp;quot;, vert.co[1]); &lt;br /&gt;
              printf(&amp;quot;Z: %f\n&amp;quot;, vert.co[2]); &lt;br /&gt;
              break;  &lt;br /&gt;
         default: printf(&amp;quot;That is not an option\n&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So structures are not that scary after all.&lt;br /&gt;
&lt;br /&gt;
=== Pointers and Variables===&lt;br /&gt;
&lt;br /&gt;
_&amp;quot;I'd recommend that anyone who's planning to dig through the source acquaint themselves intimately with C's use of pointers, including the ways that you access elements of pointers to structures; as well as building and incrementing through linked lists; and function pointers.&amp;quot;_ --harkyman&lt;br /&gt;
&lt;br /&gt;
A pointer is a variable that holds an address in memory.  &lt;br /&gt;
&lt;br /&gt;
A variable can hold data of different types and sizes.  Lets say that I&lt;br /&gt;
wanted to store the alpha, red, green, and blue values of a vetrice in memory&lt;br /&gt;
using integers.  Integers take up two bytes so the following would use 8 bytes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int a = 255;&lt;br /&gt;
int r, g, b;&lt;br /&gt;
r = 0;&lt;br /&gt;
g = 128;&lt;br /&gt;
b = 250;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Declaring Pointers===&lt;br /&gt;
When creating a pointer you declare it to be a specific type.  This type&lt;br /&gt;
helps tell how much memory is needed.  To declare a pointer variable declare the&lt;br /&gt;
type followed by the variable name with an asterisk prefix like so:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int *alpha;&lt;br /&gt;
int *red, *green, *blue;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are four pointers which hold the address of our variables.  At each&lt;br /&gt;
address we can store an integer.  This is where is gets good.  We want to store&lt;br /&gt;
the address of each variable to the pointers.  We use the ampersand as a prefix&lt;br /&gt;
to the variable name when assigning it to a pointer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
alpha = &amp;amp;a;&lt;br /&gt;
red = &amp;amp;r;&lt;br /&gt;
green = &amp;amp;g;&lt;br /&gt;
blue = &amp;amp;b;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Pointers===&lt;br /&gt;
Pointers are used in a number of ways.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
     &amp;amp;alpha is used for the address of the pointer.&lt;br /&gt;
     alpha is used for the address held by pointer.&lt;br /&gt;
     *alpha is used for the value held at the address stored in the pointer.&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Nice stuff.  Just remember there is a difference between variables and pointers.&lt;br /&gt;
When adding one to a variable we add to the value stored in the variable.  When&lt;br /&gt;
adding one to the pointer, we add to the address of the pointer.  So if our color&lt;br /&gt;
pointers are all in order in memory, by adding one to the pointer alpha I would get&lt;br /&gt;
the address of red.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
     alpha +=1;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If I add four to alpha I would get the address of the next alpha.  This is&lt;br /&gt;
because a pointer of type integer can only hold the address of an integer.  In&lt;br /&gt;
addition to this the pointer also knows that it is 2 bytes long. To add one to the variable b &lt;br /&gt;
using a pointer:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
*blue += 1;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Now blue is 251.  This is pretty powerful stuff.&lt;br /&gt;
&lt;br /&gt;
=== Pointer Code Example===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/* code listing for BabyBlend2 */&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
     /* declare some variables of type int*/ &lt;br /&gt;
     int a, r, g, b;&lt;br /&gt;
     /* declare pointers to type int */ &lt;br /&gt;
     int *alpha, *red, *green, *blue;&lt;br /&gt;
     /* assign the addresses of the variables to the pointers */ &lt;br /&gt;
     alpha = &amp;amp;a; red = &amp;amp;r; green = &amp;amp;g; blue = &amp;amp;b;  &lt;br /&gt;
     /* assign values to the variables using pointers */ &lt;br /&gt;
     *alpha = 255;&lt;br /&gt;
     *red = 0;&lt;br /&gt;
     *green = 128;&lt;br /&gt;
     *blue = 250;  &lt;br /&gt;
     &lt;br /&gt;
     /* print alpha data */ &lt;br /&gt;
     printf(&amp;quot;\nAddress of pointer alpha %p\n&amp;quot;, &amp;amp;alpha); &lt;br /&gt;
     printf(&amp;quot;Address held by pointer alpha %p\n&amp;quot;, alpha); &lt;br /&gt;
     printf(&amp;quot;Value at address held by alpha %d\n&amp;quot;, *alpha); &lt;br /&gt;
     &lt;br /&gt;
     /* print red data */ &lt;br /&gt;
     printf(&amp;quot;\nAddress of pointer red %p\n&amp;quot;, &amp;amp;red);&lt;br /&gt;
     printf(&amp;quot;Address held by pointer red %p\n&amp;quot;, red);&lt;br /&gt;
     printf(&amp;quot;Value at address held by red %d\n&amp;quot;, *red);&lt;br /&gt;
&lt;br /&gt;
     *alpha -= 1;&lt;br /&gt;
     printf(&amp;quot;\n*alpha -= 1\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;Address of pointer alpha %p\n&amp;quot;, &amp;amp;alpha); &lt;br /&gt;
     printf(&amp;quot;Address held by pointer alpha %p\n&amp;quot;, alpha);&lt;br /&gt;
     printf(&amp;quot;Value at address held by alpha %d\n&amp;quot;, *alpha);&lt;br /&gt;
     printf(&amp;quot;\nNotice value at address held by alpha is one less\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
     alpha += 1;&lt;br /&gt;
     printf(&amp;quot;\nalpha += 1\n&amp;quot;);&lt;br /&gt;
     printf(&amp;quot;Address of pointer alpha %p\n&amp;quot;, &amp;amp;alpha);&lt;br /&gt;
     printf(&amp;quot;Address held by pointer alpha %p\n&amp;quot;, alpha);&lt;br /&gt;
     printf(&amp;quot;Value at address held by alpha %d\n&amp;quot;, *alpha);&lt;br /&gt;
     printf(&amp;quot;\nNotice alpha now points to address held by red\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pointers to Structures===&lt;br /&gt;
Let's create a structure called vert and then create a pointer and&lt;br /&gt;
finally point to the structure.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
     MVert vert;            /* creates structure variable &amp;quot;vert&amp;quot; */&lt;br /&gt;
     MVert *vert_point;     /* creates pointer to structure of type MVert */&lt;br /&gt;
     vert_point = &amp;amp;vert;    /* assign address of variable vert to pointer */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Accessing Structure Data using Pointers===&lt;br /&gt;
We access the structure variables with the pointer using this syntax.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
     (*vert_point).mat_nr = 1;               /* assigns 1 to mat_nr of structure */&lt;br /&gt;
     printf(&amp;quot;X co %f&amp;quot;, (*vert_point).co[0]); /* print x coordinate */&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
We can simplify the readability of the code using a different syntax.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
     vert_point-&amp;gt;mat_nr = 1;              /* assigns 1 to mat_nr of structure */&lt;br /&gt;
     printf(&amp;quot;X co %f&amp;quot;, vert_point-&amp;gt;co[0]); /* print x coordinate */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pointer to Structures Code Example===&lt;br /&gt;
In this example we access the structure variables with a pointer to the structure.&lt;br /&gt;
Compare this to the code in part 1.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/* Pointer to Structure Code Listing */&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
     /* declare structure */&lt;br /&gt;
     typedef struct MVert { &lt;br /&gt;
          float   co[3];      /* array for vertex coordinates */&lt;br /&gt;
          short   no[3];      /* array for vertex normals */&lt;br /&gt;
          char flag, mat_nr;  /* variables for a flag and material index */&lt;br /&gt;
     } MVert;&lt;br /&gt;
 &lt;br /&gt;
     MVert vert;        /* create variable of type MVert */&lt;br /&gt;
     MVert *vert_point; /* create pointer to type MVert */ &lt;br /&gt;
     char key_press = '\0';&lt;br /&gt;
 &lt;br /&gt;
     /* assign structure address to pointer */ &lt;br /&gt;
     vert_point = &amp;amp;vert;  &lt;br /&gt;
     &lt;br /&gt;
     printf(&amp;quot;\nWelcome to BabyBlender\n\n&amp;quot;); &lt;br /&gt;
     printf(&amp;quot;To create a vertex press 'V'\n&amp;quot;); &lt;br /&gt;
     printf(&amp;quot;Key: &amp;quot;); &lt;br /&gt;
     scanf(&amp;quot;%c&amp;quot;, &amp;amp;key_press);&lt;br /&gt;
     switch (key_press) {&lt;br /&gt;
          case 'V': &lt;br /&gt;
          case 'v': &lt;br /&gt;
               /* assign values to the structure's array */&lt;br /&gt;
               printf(&amp;quot;Enter X coordinate: &amp;quot;);&lt;br /&gt;
               scanf(&amp;quot;%f&amp;quot;, &amp;amp;(*vert_point).co[0]); &lt;br /&gt;
               printf(&amp;quot;Enter Y coordinate: &amp;quot;); &lt;br /&gt;
               scanf(&amp;quot;%f&amp;quot;, &amp;amp;(*vert_point).co[1]); &lt;br /&gt;
               printf(&amp;quot;Enter Z coordinate: &amp;quot;); &lt;br /&gt;
               scanf(&amp;quot;%f&amp;quot;, &amp;amp;(*vert_point).co[2]); &lt;br /&gt;
&lt;br /&gt;
               /* print the values from the structure's array */&lt;br /&gt;
               printf(&amp;quot;\nYour vertex is located at\n&amp;quot;);&lt;br /&gt;
               printf(&amp;quot;X: %f\n&amp;quot;, vert_point-&amp;gt;co[0]); &lt;br /&gt;
               printf(&amp;quot;Y: %f\n&amp;quot;, vert_point-&amp;gt;co[1]); &lt;br /&gt;
               printf(&amp;quot;Z: %f\n&amp;quot;, vert_point-&amp;gt;co[2]); &lt;br /&gt;
          break;  &lt;br /&gt;
          default: printf(&amp;quot;That is not an option\n&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- [[DeveloperWiki/DeveloperWikiStephenHughes|DeveloperWikiStephenHughes]] - 27 Apr 2005&lt;br /&gt;
&lt;br /&gt;
[[Category:Script]]&lt;/div&gt;</summary>
		<author><name>wiki&gt;Adc</name></author>
		
	</entry>
</feed>