Re: [Dev-C++] Stress with structs and pointers.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-06-12 10:16:18
|
David McKen wrote:
> Ok, I need some clarification on a couple topics invloving pointers and
> pointers.
>
> 1) when a struct is created. All that can be assumed is that it contains
> trash right? What is a portable way to initalize everything to 0? mset?
That is the simplest, if you have a LARGE structure.
#include <mem.h> /* or string.h, I'm not sure */
struct mystruct {
int a,b,c;
char d,e,f;
long g,h,i,j,k,l,m,n;
/* ... */
};
int main()
{
struct mystruct ms;
memset(&ms,0,sizeof(struct mystruct));
return 0;
}
The above code is pure C. In C++, you don't need the "struct" word, when
using mystruct. So, in C++, the first 2 line in main():
mystruct ms;
memset(&ms,0,sizeof(mystruct));
memset() is also portable, and it will always work, even if you alter
the definition of your structure (add new elements). This way, you don't
have to use a LONG list to set everything to 0.
--
Greetings,
Balázs
|