I know that memset can be used to assign values to a character array in the following way:
char str[100];
memset(str,'*',10);
now can i use memset to assign values to other data types such as double. If so how can I achieve this.
memset
Moderator: Board moderators
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
You can do the same with arrays:
However, do take note that since int and double are not 1 byte, you cannot do things like memset(a, 1, sizeof(a)) and expect a[0..99] to be 1.
Code: Select all
double d[100];
int a[100];
memset(d, 0, sizeof(d));
memset(a, 0, sizeof(a));
-
- New poster
- Posts: 31
- Joined: Sun Feb 23, 2003 9:18 pm
- Location: Waterloo, Ontario, Canada
The C++ STL function "fill(iterator, iterator, value)" may be useful if you need to perform more complicated initializations.
[cpp]
#include <algorithm>
using namespace std;
double a[100];
vector<string> b;
void foo() {
fill(a, a+100, 3.1415);
fill(b.begin(), b.end(), "hello");
}
[/cpp]
I have not benchmarked memset vs. fill on UVA, but I think memset is faster for zeroing things.
[cpp]
#include <algorithm>
using namespace std;
double a[100];
vector<string> b;
void foo() {
fill(a, a+100, 3.1415);
fill(b.begin(), b.end(), "hello");
}
[/cpp]
I have not benchmarked memset vs. fill on UVA, but I think memset is faster for zeroing things.