-
Nov 20th, 2022, 10:22 PM
#1
Thread Starter
Frenzied Member
How do I address C variables that use an typedef array in an array?
For example, if I have a type like this "typedef int TwoInts[2];" and then I have an array of this type like this "TwoInts MyVar[10]", how do I address the individual elements of that array?
Do I do MyVar[y][x], where y is an entry in the MyVar array (the entry being of type TwoInts), and then x selects which one of the 2 int fields within this array entry? Or would y select the int field, while x would select the entry in the array?
-
Nov 21st, 2022, 05:43 AM
#2
Re: How do I address C variables that use an typedef array in an array?
Do you mean something like:
Code:
#include <stdio.h>
typedef int TwoInts[2];
int main() {
TwoInts MyVar[10] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
printf("%i %i\n", MyVar[1][0], MyVar[1][1]);
printf("%i %i\n", MyVar[3][0], MyVar[3][1]);
}
which displays:
[3] selects the element in MyVar and then [0] or [1] selects the element of that as per TwoInts.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Nov 21st, 2022, 06:03 AM
#3
Re: How do I address C variables that use an typedef array in an array?
Originally Posted by Ben321
For example, if I have a type like this "typedef int TwoInts[2];" and then I have an array of this type like this "TwoInts MyVar[10]", how do I address the individual elements of that array?
Do I do MyVar[y][x], where y is an entry in the MyVar array (the entry being of type TwoInts), and then x selects which one of the 2 int fields within this array entry? Or would y select the int field, while x would select the entry in the array?
Speaking in VB6 this would be:
Dim Myvar(0 To 9, 0 To 1) As Long
So y would be first dimension, x the second
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 21st, 2022, 12:25 PM
#4
Re: How do I address C variables that use an typedef array in an array?
In VB6 dimensions are reversed i.e. y have to be the second one in order to keep the memory layout like in C/C++
-
Nov 21st, 2022, 12:29 PM
#5
Re: How do I address C variables that use an typedef array in an array?
Originally Posted by wqweto;[URL="tel:5586748"
5586748[/URL]]In VB6 dimensions are reversed i.e. y have to be the second one in order to keep the memory layout like in C/C++
Damn! That‘s one of those „50:50“ things i can never remember.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 22nd, 2022, 04:29 AM
#6
Re: How do I address C variables that use an typedef array in an array?
Given the C++ code above, then effectively you have:
Code:
TwoInts temp = MyVar[3];
int myInt = temp[1];
c/c++ has arrays of 1 dimension. Although you can have an array of type array of type array.... which allows multi-dimensions to be used. The types of each 'array' can be different.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Nov 22nd, 2022, 07:56 AM
#7
Re: How do I address C variables that use an typedef array in an array?
Originally Posted by Zvoni
Damn! That‘s one of those „50:50“ things i can never remember.
In VB6 it's the natural x,y while in C/C++ you have to be able to get a pointer to y-th row with MyVar[y] so it's reversed to MyVar[y][x]
You can check which dimension is which in VB's layout like this
Code:
Dim Myvar(0 To 9, 0 To 1) As Long
Debug.Print VarPtr(Myvar(1, 0)) - VarPtr(Myvar(0, 0))
'--> 4
Debug.Print VarPtr(Myvar(0, 1)) - VarPtr(Myvar(0, 0))
'--> 40
Obviously the second dimension "offsets" by 40 bytes so it must be "by row"
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|