I'm sorry for the second post in as many hours, and both about structs.
The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.
What I can't seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}} ?
I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>
struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};
struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};
//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];
int main(void)
{
test3[0].testint=1;
int *testArray=mall oc(5*sizeof(int ));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=t estArray;
return 0;
}
Sep 23 '06
12 3997
Thank you again Harald. This works too! I have really got to sit down
and study this code now and convince myself I really do understand it.
Regards
Yusuf
"kondal" <ko******@gmail .comwrites:
[...]
If you are using another array to initialize your structures, why don't
you use memcpy.
Create a integer pointer to the structure and memcpy the entire
structure. If not you can use it to initialize individually.
What do you mean by "integer pointer"? Do you mean just "pointer"?
It's not necessary to use memcpy() to copy structures; you can just
use an assignment.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Yusuf" <as**********@h otmail.comwrite s:
[...]
t2.c:26: warning: initialization from incompatible pointer type
Code below.
Thank you again
Yusaf
typedef struct
{
int testint;
int *testintArray;
} teststruct3;
struct teststruct4a
{
char *testchar;
struct teststruct3 *teststruct;
};
struct teststruct4b
{
char *testchar;
struct teststruct3 *teststruct;
} test4b[1] =
{
{"testchar",(te ststruct3[])
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
}
}
};
int main(void)
{
return 0;
}
For the sake of consistency, don't use typedefs. You originally
declared your "teststruct 3" type as "struct teststruct3 { ... };";
there was no reason to change that to a typedef. The problem was
that, later on, you referred to the type as "teststruct 3". You just
need to refer to it as "struct teststruct3".
A typedef for a struct type merely declares another name for something
that already has one. Some people like being able to refer to the
type with a single identifier, but it's not at all necessary.
(If you're going to use typedefs, you should be consistent; having
"teststruct 3" as a typedef name and "teststruct 4b" as a struct tag is
just confusing.)
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Martin Pohlack |
last post by:
Hi,
I have a funtion which shall compute the amount for a later malloc. In
this function I need the sizes of some struct members without having an
instance or pointer of the struct.
As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:
#include <dirent.h>
|
by: Emanuele Blanco |
last post by:
Hi there,
I just compiled a program that uses linked lists (needed it as an homework
for my Programming course at University). It works flawlessly, even if I
notice a thing. Here's my linked list declaration:
struct node {
float item;
struct node *next;
};
|
by: Russell Shaw |
last post by:
Hi,
I have two structs in a header file, and they reference each other,
causing a compile error. Is there a standard way to deal with this?
typedef struct {
...
RtAction *actions;
} RtWidget;
|
by: ma0001yu |
last post by:
Hi, all. I feel confuse about below struct definition:
typedef struct TAG
{
comments....
};
What my confusion is: is typedef extra??why we not just use
|
by: Lane Straatman |
last post by:
I would like to write a 'struct'. I have a library that is all but
completely inappropriate for this task.
So I'm looking for C code that fills in the gaps between:
#undef whateverkeithsaysfor99compliance
#define whateverkeithsays for99compliance
int main(void
{
struct foo
| |
by: fcvcnet |
last post by:
Hi,
I read the book C++ Primer, Fourth Edition By Stanley B. Lippman,
Jos¨¦e Lajoie, Barbara E. Moo
"If we define a class using the class keyword, then any members
defined before the first access label are implicitly private; ifwe
usethe struct keyword, then those members are public. Whether we define
a class using the class keyword or the struct keyword affects only the
default initial access level."
Now I defined a struct with...
|
by: berrylthird |
last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example:
var myInstance:myClass = new myClass();
myInstance.member_0 = memberValue_0; // absolute notation
myInstance = memberValue_0; // relative notation
myInstance = memberValue_0; // nominal notation
You can initialize a struct in C/C++ like you would an array, as with the...
|
by: ManicQin |
last post by:
Hi all.
I'm trying to get the size of a variable in a struct by his relative
postion i.e.
///
#define offsetof(s,m) (size_t)&(((s *)0)->m)
struct ThePimp{
char rings;
char blings;
|
by: hugo.arregui |
last post by:
Hi!
I have two struts like that:
struct {
int num;
int num2;
struct b arrayOfB;
} a;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |