C structure reset , not C++

  • Thread starter devaraj.takhellambam
  • Start date
D

devaraj.takhellambam

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..
 
C

Cong Wang

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/BL-0113590-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.
 
D

devaraj.takhellambam

Somebody told me that we could use memset or something. Any ideas?
Cong said:
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/BL-0113590-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.
 
C

Cong Wang

Somebody told me that we could use memset or something. Any ideas?

Cong said:
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/BL-0113590-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?
 
F

Frederick Gotham

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.
 
D

devaraj.takhellambam

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 said:
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.
 
J

Joe Wright

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);
 
F

Frederick Gotham

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.)
 
D

Default User

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top