Q:

integer in C Get Random value

i want write program in C via bluez API
I have used this site for tutorial :
and this is my code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(int argc, char **argv)
{
    int dev_id = hci_get_route(NULL);
    int res_scan=NULL;
    int count;
    inquiry_info *device_info=NULL;
    res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
    printf("%i\n",res_scan);
    for(count = 0;count < res_scan;count++)
    {
        char *name;
        printf("count Before : %i\n",count);
        ba2str(&(device_info+count)->bdaddr,&name);
        printf("count After : %i\n",count);

        printf("%s\n",&name);
    }

}

and out console : 
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86

why count value after ba2str(&(device_info+count)->bdaddr,&name); get random value ?
in that source i linked this issue wont occur !? 

A:

instead of 
char *name;
...
printf("%s\n",&name);

use
char name[248] = { 0 };
...
printf("%s\n",name);

A:

You need allocate memory before pass the variable as reference, and the best option is to do that out of the loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(int argc, char **argv)
{
    int dev_id = hci_get_route(NULL);
    int res_scan=NULL;
    int count;
    char *name = (char *) malloc(248*sizeof(char));
    inquiry_info *device_info=NULL;
    res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
    printf("%i\n",res_scan);
    for(count = 0;count < res_scan;count++)
    {
        printf("count Before : %i\n",count);
        ba2str(&(device_info+count)->bdaddr,name);
        printf("count After : %i\n",count);

        printf("%s\n",name);
    }

 free(name);

}

doing that your code will be faster because you will allocate memory only one time.

