12081 - Reduced ID Numbers

All about problems in Volume 120. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Wed Nov 16, 2005 12:31 pm

thanks Krzysztof Duleba, but i cannot understood the line
Krzysztof Duleba wrote:you just have to know what the second argument is of char type.
can you clarify in much easier way. why initializing an array with 0 is working, but not other values? can an array of structure be initialized directly with memset?
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Wed Nov 16, 2005 2:53 pm

ayon wrote:thanks Krzysztof Duleba, but i cannot understood the line
Krzysztof Duleba wrote:you just have to know what the second argument is of char type.
can you clarify in much easier way. why initializing an array with 0 is working, but not other values? can an array of structure be initialized directly with memset?
the syntax is
memset( P, C, S );
where:
- P is a pointer into memory
- C is a variable of type char
- S is an integer variable (size)
memset takes the S bytes of memory starting at P and fills them with the char C.

As most of the variables use more than one byte (e.g., an int uses 4 bytes of memory), it is (in general) impossible to use memset for initialization of arrays.

There are some special cases where this is possible. For example, if all 4 bytes of an int are zero, the value of that int is zero. Thus if you use memset with C=0 on an array of ints, it will set all the ints to zero.

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Wed Nov 16, 2005 4:59 pm

at last, i understand everything

Code: Select all

#include <stdio.h>
#include <string.h>

void main()
{
   int a;
   memset(&a, 1, sizeof(a));
   printf("%d\n", a);   
}
this code prints 16843009, because the 4 bytes are allocated like below:
00000001 00000001 00000001 00000001 = 16843009(decimal)

thanks to everyone
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko » Sat Dec 10, 2005 12:54 pm

misof wrote:There are some special cases where this is possible. For example, if all 4 bytes of an int are zero, the value of that int is zero. Thus if you use memset with C=0 on an array of ints, it will set all the ints to zero.
Very usefull is also the case if all 4 bytes of an int are -1 (11111111 binary), the value of that int is -1. Thus

Code: Select all

int P[100];
memset(a,-1,4*100);
fills P with -1s.

Post Reply

Return to “Volume 120 (12000-12099)”