36

How to print int* (int pointer) and unsigned int* in log using NSLog?

- (int) doSomethingWith:(unsigned int)Msg withWparam:(unsigned int*)wParam withParameter:(int *) lParam
{
    NSLog(@"MSg:%d wParam:%u lParam:%u",Msg,wParam,lParam);
//not working
    return 1;
}

Warning: Format specifies type 'unsigned int' but the argument has type 'unsigned int *'

Popeye
  • 11,839
  • 9
  • 58
  • 91
HDdeveloper
  • 4,396
  • 6
  • 40
  • 65
  • 7
    @Downvoter please also tell reason? – HDdeveloper Jan 18 '13 at 10:40
  • 3
    I up voted you since there are stupid trolls on here that down vote when they do not know the answer to something. But this question did help me out and for that you get my up vote. – JoeyL Oct 07 '13 at 00:39

2 Answers2

53

Use %d for int. And the parameters are pointers, so use * to access the values pointed to.

NSLog(@"MSg:%d wParam:%u lParam:%d",Msg,*wParam,*lParam);
Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • +1 Thanks @Joris, but its crashing if no value for wParam(means if wParam is nil). How to check that? // now using: NSLog(@"MSG:%d wParam:%u lParam:%d",Msg,*wParam, *lParam); – HDdeveloper Jan 18 '13 at 10:36
  • 1
    You'll have to check for that before logging: `if (!wParam || !lParam) { /* invalid arguments */ }` – Joris Kluivers Jan 18 '13 at 10:43
14

%@ is for objects. BOOL is not an object. You should use %d.
On the bases of data type %@ changes as follows

For Strings you use %@
For int  you use %i
For float you use %f
For double you use %lf
Vaibhav Sharma
  • 1,123
  • 10
  • 22