Click here to Skip to main content
11,641,067 members (65,800 online)
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 Mika Wendelius 400
1 OriginalGriff 205
2 DamithSL 203
3 CPallini 185
4 Afzaal Ahmad Zeeshan 129
0 Mika Wendelius 400
1 OriginalGriff 205
2 DamithSL 203
3 CPallini 185
4 Afzaal Ahmad Zeeshan 129


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
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