memset o/p not correct
  Home FAQ Contact Sign in
comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c Profile…
 Up
memset o/p not correct         


Author: aarklon
Date: Apr 19, 2008 14:28

Hi,

the following is actually a part of the pattern matching program which
i tried ,memset is not setting the entire integer array with
-1

#include
#include
#include

int main(void)
{

int maxpat[80];
memset(maxpat,-1,80);

for(i=0;i<80;i++)
printf("%%d\t",maxpat[i]);

return EXIT_SUCCESS;
}

but i am getting o/p as
Show full article (1.14Kb)
46 Comments
Re: memset o/p not correct         


Author: aarklon
Date: Apr 19, 2008 14:31

sorry I forgot to declare i , but still the o/p is same

#include
#include
#include

int main(void)
{

int maxpat[80],i;
memset(maxpat,-1,80);

for(i=0;i<80;i++)
printf("%%d\t",maxpat[i]);

return EXIT_SUCCESS;
}
no comments
Re: memset o/p not correct         


Author: aarklon
Date: Apr 19, 2008 14:32

On Apr 20, 2:28 am, aark...@gmail.com wrote:
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
>
>  #include
>  #include
>  #include
>
>  int main(void)
>  {
>
>    int maxpat[80];
>    memset(maxpat,-1,80);
>
>    for(i=0;i<80;i++)
>    printf("%%d\t",maxpat[i]);
> ...
Show full article (1.38Kb)
no comments
Re: memset o/p not correct         


Author: Ian Collins
Date: Apr 19, 2008 14:38

aarklon@gmail.com wrote:
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
>
> #include
> #include
> #include
>
> int main(void)
> {
>
> int maxpat[80];
> memset(maxpat,-1,80);
>
You are only setting the first 80 bytes of the the array, not 80 ints.
Show full article (0.42Kb)
no comments
Re: memset o/p not correct         


Author: Bartc
Date: Apr 19, 2008 14:45

gmail.com> wrote in message
news:c7633ee9-954f-4ad8-a0c0-646fbe8e8e81@b5g2000pri.googlegroups.com...
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
>
> #include
> #include
> #include
>
> int main(void)
> {
>
> int maxpat[80];
> memset(maxpat,-1,80);

You're setting 80 bytes. The array is bigger than that (80*sizeof(int)).
Try:
Show full article (0.54Kb)
no comments
Re: memset o/p not correct         


Author: Flash Gordon
Date: Apr 19, 2008 14:36

aarklon@gmail.com wrote, On 19/04/08 22:31:
> sorry I forgot to declare i , but still the o/p is same
>
> #include
> #include
> #include
>
> int main(void)
> {
> int maxpat[80],i;
> memset(maxpat,-1,80);



Check the definition of memset in your reference book, it sets a
specified number of *bytes*. This also means that having corrected the
error (sizeof *maxpat is your friend) it won't be portable to
implementations using 1s complement or sign-magnitude if you can find
such an implementation.
--
Flash Gordon
no comments
Re: memset o/p not correct         


Author: Default User
Date: Apr 19, 2008 16:43

aarklon@gmail.com wrote:
> Hi,
>
> the following is actually a part of the pattern matching program which
> i tried ,memset is not setting the entire integer array with
> -1
> int maxpat[80];
> memset(maxpat,-1,80);

As the others have pointed out, it sets each byte to the value. There's
almost no way to do what you want with memset(), unless you happen to
be on a machine that had one-byte ints.

To set each int in the array to -1, you need a loop. Better yet,
explain WHY you want to do this. There may be a better construct.

Brian
no comments
Re: memset o/p not correct         


Author: Richard Tobin
Date: Apr 19, 2008 17:22

In article <66vedbF2m6h5lU1@mid.individual.net>,
Default User yahoo.com> wrote:
>> int maxpat[80];
>> memset(maxpat,-1,80);
>As the others have pointed out, it sets each byte to the value. There's
>almost no way to do what you want with memset(), unless you happen to
>be on a machine that had one-byte ints.

Unless you really on the machines being 2s-complement, which is true
of all general-purpose computers today.

-- Richard
--
:wq
no comments
Re: memset o/p not correct         


Author: Richard
Date: Apr 19, 2008 20:29

"Default User" yahoo.com> writes:
> aarklon@gmail.com wrote:
>
>> Hi,
>>
>> the following is actually a part of the pattern matching program which
>> i tried ,memset is not setting the entire integer array with
>> -1
>
>> int maxpat[80];
>> memset(maxpat,-1,80);
>
> As the others have pointed out, it sets each byte to the value. There's
> almost no way to do what you want with memset(), unless you happen to
> be on a machine that had one-byte ints.
>
> To set each int in the array to -1, you need a loop. Better yet,
> explain WHY you want to do this. There may be a better construct.
Show full article (0.78Kb)
no comments
Re: memset o/p not correct         


Author: CBFalconer
Date: Apr 19, 2008 21:06

Richard Tobin wrote:
> Default User yahoo.com> wrote:
>
>>> int maxpat[80];
>>> memset(maxpat,-1,80);
>
>> As the others have pointed out, it sets each byte to the value.
>> There's almost no way to do what you want with memset(), unless
>> you happen to be on a machine that had one-byte ints.
>
> Unless you really on the machines being 2s-complement, which is
> true of all general-purpose computers today.

What's the point of the foolishness, when:

#define MPSIZE 80
#define VALUE -1
int maxpat[MPSIZE], i;

for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
Show full article (1.03Kb)
no comments
1 2 3 4 5