468,504 Members | 1,992 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

C structure reset , not C++

Hi,

I am hoping to get some help from you. Basically, I have a structure in

my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.
struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;
Thanks a lot..

Oct 7 '06 #1
8 9556


On Oct 7, 2:40 pm, devaraj.takhellam...@gmail.com wrote:
Hi,

I am hoping to get some help from you. Basically, I have a structure in

my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;

Thanks a lot..
Standard C can't do that directly. See the following link:
http://www.cs.wisc.edu/wpis/papers/B...0-990302-04.ps
Maybe that can give you some hints.
Gcc defines a feature which allows you to check your type at runtime.
See the following link:
http://www.imodulo.com/gnu/gcc/Typeof.html
However, it does NOT really do the checking as you wanted. So consider
to try another approach to get what you want, instead of checking the
type directly.

Oct 7 '06 #2
Somebody told me that we could use memset or something. Any ideas?
Cong Wang wrote:
On Oct 7, 2:40 pm, devaraj.takhellam...@gmail.com wrote:
Hi,

I am hoping to get some help from you. Basically, I have a structure in

my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;

Thanks a lot..

Standard C can't do that directly. See the following link:
http://www.cs.wisc.edu/wpis/papers/B...0-990302-04.ps
Maybe that can give you some hints.
Gcc defines a feature which allows you to check your type at runtime.
See the following link:
http://www.imodulo.com/gnu/gcc/Typeof.html
However, it does NOT really do the checking as you wanted. So consider
to try another approach to get what you want, instead of checking the
type directly.
Oct 7 '06 #3


On Oct 7, 4:17 pm, devaraj.takhellam...@gmail.com wrote:
Somebody told me that we could use memset or something. Any ideas?

Cong Wang wrote:
On Oct 7, 2:40 pm, devaraj.takhellam...@gmail.com wrote:
Hi,
I am hoping to get some help from you. Basically, I have a structure in
my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.
struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;
Thanks a lot..
Standard C can't do that directly. See the following link:
http://www.cs.wisc.edu/wpis/papers/B...0-990302-04.ps
Maybe that can give you some hints.
Gcc defines a feature which allows you to check your type at runtime.
See the following link:
http://www.imodulo.com/gnu/gcc/Typeof.html
However, it does NOT really do the checking as you wanted. So consider
to try another approach to get what you want, instead of checking the
type directly.
Of course, memset can help you to clear a structure. But it can NOT
help you to judge the type! You are very serious with the type of the
member that you want to clear, aren't you?

Oct 7 '06 #4
Devaraj posted:
Basically, I have a structure in my c program and the structure is used
to store the values of a table row by row and does some processing. At
the end of one row processing, I would like to clear the contents of
each members of the structure

By "clear", do you mean set to their default value (i.e. zero for
arithmetic types, null value for pointers)?

which has different data typebefore processing the next row. Can this be
done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;

I suppose you could do something like the following:

struct table_1 const global_blank_table;

int main(void)
{
struct table_1 obj;

/* Fill it will all sorts of values */

/* ... */

/* Now let's clear it: */

obj = global_blank_table;

return 0;
}

It's perfectly safe, however, to use memset on your structure, but only
because every member is an integer type. If it were to contain pointer
types or floating point types, then you're not guaranteed that setting it
to all-bits-zero will yield the null pointer value and arithmetic zero
respectively.

--

Frederick Gotham
Oct 7 '06 #5
Thanks.. what if i know that the structure always contains 5 chars , 2
long and some date data types..It will never change the datatypes as it
is constantly fetching the rows from a table and does some logic..after
processing the first row, it gets into the next row in a while loop.
here i would like to clear out the contents of the structures before
fetching the second record. What abt usung 9 memset's for ech
member..is that possible..please help!
Frederick Gotham wrote:
Devaraj posted:
Basically, I have a structure in my c program and the structure is used
to store the values of a table row by row and does some processing. At
the end of one row processing, I would like to clear the contents of
each members of the structure


By "clear", do you mean set to their default value (i.e. zero for
arithmetic types, null value for pointers)?

which has different data typebefore processing the next row. Can this be
done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;


I suppose you could do something like the following:

struct table_1 const global_blank_table;

int main(void)
{
struct table_1 obj;

/* Fill it will all sorts of values */

/* ... */

/* Now let's clear it: */

obj = global_blank_table;

return 0;
}

It's perfectly safe, however, to use memset on your structure, but only
because every member is an integer type. If it were to contain pointer
types or floating point types, then you're not guaranteed that setting it
to all-bits-zero will yield the null pointer value and arithmetic zero
respectively.

--

Frederick Gotham
Oct 7 '06 #6
de******************@gmail.com wrote:
Hi,

I am hoping to get some help from you. Basically, I have a structure in

my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.
struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;
Thanks a lot..
I'll assume 'to clear' to mean set to zero. You might investigate this.

#include <string.h>

struct table_1 tab1; /* defines tab1 */
memset(tab1, 0, sizeof tab1);
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 7 '06 #7
Devaraj posted:
Thanks.. what if i know that the structure always contains 5 chars , 2
long and some date data types..It will never change the datatypes as it
is constantly fetching the rows from a table and does some logic..after
processing the first row, it gets into the next row in a while loop.
here i would like to clear out the contents of the structures before
fetching the second record. What abt usung 9 memset's for ech
member..is that possible..please help!

Coincedently, there's a thread over on comp.lang.c++ very similar to
this...

I'm not sure exactly what you're trying to do. If you want to set an object
to all-bits-zero, then simply do:

memset(&obj,0,sizeof obj);

If you would like to set an object to its default value, then... hmm...
maybe the following would be a start. If your compiler supports "inline",
then:

/* FILE BEGIN: clearer.h */

#define DEFINE_CLEARER(Type) \
inline void Clear(Type *const p) \
{ \
static Type const blank_obj = {0}; \
\
*p = blank_oj; \
}

/* FILE END: clearer.h */
Then just include that header in your source file, and pre-define the
clearer function:

DEFINE_CLEARER(MyType)

int main()
{
MyType obj;

Clear(&obj);
}

This won't work for arrays for two reasons:

(1) The function parameter will come out as "int[5] *const p" rather
than "int (*const p)[5]".
(2) You can't assign to an array (although we could overcome this with
memcpy).

(Not to wave the C++ flag, but if you could write even one of the source
files in C++, there'd be a very simple solution.)

--

Frederick Gotham
Oct 7 '06 #8
de******************@gmail.com wrote:
Hi,

I am hoping to get some help from you. Basically, I have a structure
in

my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

One way is to have a default initialized variable of the struct that
you use to "clear" the other by assignment:
#include <stdio.h>

struct st
{
int a;
int b;
};

int main(void)
{
const struct st reset = {0};
struct st worker;

worker.a = 1;
worker.b = 2;

printf("a = %d b = %d\n\n", worker.a, worker.b);

worker = reset;

printf("a = %d b = %d\n\n", worker.a, worker.b);

return 0;
}

BASH.EXE-2.01$ ./reset
a = 1 b = 2

a = 0 b = 0


Brian
Oct 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by kazack | last post: by
15 posts views Thread by Sourcerer | last post: by
2 posts views Thread by Bob Jenkins | last post: by
24 posts views Thread by Roman Mashak | last post: by
2 posts views Thread by Roger Withnell | last post: by
6 posts views Thread by zulander | last post: by
4 posts views Thread by gordon | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.