Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
I have tried to memset an integer pointer to zero, but its not assigning all the register to zero.

#include
#include
#define N 128
int main()
{
int *a;
int nSize = sizeof(int) * N;

a = (int*) malloc(nSize);

//*(a+sizeof(int)*397) = 397;
//*(a+sizeof(int)*396) = 396;
memset(a, 0, nSize);
for(int i=0; i<n;> printf("%d ", *(a+sizeof(int)*i));
}

 
pls help.
Posted 27-Dec-12 20:05pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

memset sets the number of bytes you specify to the value you have given. So it is working - you have allocated a chunk of memory that is nSize bytes long, then set all of them to the value zero.
 
The problem is with your loop, which frankly is a load of rubbish (but you know that, because it won't compile, will it?)
Try this:
for (i = 0; i < N; i++)
   {
   printf("%d ", *(a + i));
   }
Bear in mind that a is an int pointer - not a byte. So when you add a value to it, you are incrementing by the size of an integer already.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 CPallini 490
2 George Jonsson 297
3 OriginalGriff 250
4 Suvabrata Roy 209
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100