integer in C Get Random value - c

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 !?

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

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.

Related

Randomize letters in string in C

I have for example "asd" and I want it to be randomized to DAS, DSA, SAD, you know. How can I code this? Tried a few solutions but It didnt really work.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main()
{
printf("type in the word\n");
char haslo[128];
scanf("%s", haslo);
char set[128];
char hasloa[128];
strcpy(set, haslo);
unsigned int Ind = 0;
srand(time(NULL) + rand());
int len = strlen(set);
while(Ind < len)
{
hasloa[Ind++] = set[rand()%62];
}
hasloa[len] = '\0';
printf("%s", hasloa);
return 0;
}
Change 62 inside the while loop to "len"

adding an int to a Multi-dimensional char Array

I am trying to add an int to a Multi-dimensional char Array. After reading the link below I would think I can use sprintf. If I can't use sprintf what is another way I can do this?
http://www.cplusplus.com/reference/cstdio/sprintf/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
//{"TYPE", "ID", "SCOPE", "VALUE"}
char *symbol_table_variables[503][4];
int scope = 0;
int lower_bound_of_big_boy_counter = 0;
sprintf (symbol_table_variables[lower_bound_of_big_boy_counter][2], "%d", scope);
printf("symbol_table_variables[lower_bound_of_big_boy_counter][2] %s \n",
symbol_table_variables[lower_bound_of_big_boy_counter][2]);
return 0;
}
An update.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
//{"TYPE", "ID", "SCOPE", "VALUE"}
char *symbol_table_variables[503][4] = {0};
int scope = 5;
int lower_bound_of_big_boy_counter = 0;
char scope_char[80] = {0};
sprintf (scope_char, "%d", scope);
printf("scope_char %s \n", scope_char);
symbol_table_variables[lower_bound_of_big_boy_counter][2] =
malloc(strlen(scope_char)+1);
strcpy(symbol_table_variables[lower_bound_of_big_boy_counter][2],
scope_char);
memset(scope_char, 0, 80);
//sprintf (symbol_table_variables[lower_bound_of_big_boy_counter][2], "%d", scope);
printf("symbol_table_variables[lower_bound_of_big_boy_counter][2] is %s \n",
symbol_table_variables[lower_bound_of_big_boy_counter][2]);
return 0;
}
symbol_table_variables[lower_bound_of_big_boy_counter][2] has no memory allocated to it you you are invoking undefined behavior.
One solution would be to allocate some memory
symbol_table_variables[lower_bound_of_big_boy_counter][2] = malloc(32);
printf (symbol_table_variables[lower_bound_of_big_boy_counter][2], "%d", scope);
That isn't great because you don't really know how much memory you need.
I'd be questioning the need for a 2D array of strings...

how to change color of xeyes using a path in c?

all this in Linux not windows
hello i want to know how i can change the color of xeyes like we can do in terminal like
xeyes -fg blue
now i want to to do this in c program using path
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <malloc.h>
//#inlcude <windows.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[0], fullPathName);
myArgv[1] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
if i simply call /usr/bin/X11/xeyes it just show eyes
now i am trying to add command like /usr/bin/X11/xeyes-fg but its not working
any suggestion?
You can add onto the argument vector, like this:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
int n = 0;
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[n++], fullPathName);
myArgv[n++] = "-fg";
myArgv[n++] = "blue";
myArgv[n] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
Here is a picture of the result:
Offhand, I would have expected strace to show the file rgb.txt being opened, but do not see this using -f option (assume it happens in the server). The "blue" does show up in a trace, but only in the exec call, e.g.,
execve("/usr/bin/X11/xeyes", ["/usr/bin/X11/xeyes", "-fg", "blue"], [/* 62 vars */]) = 0

notify-send in C code

How can I call notify-send from C code with a message stored in my string ?
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("mount something somewhere");
system("notify-send message");
return 0;
}
Just send the string as a parameter to system().
For example:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char command[100], msg[100];
strcpy(command,"notify-send ");
strcpy(msg,"\"Hello World\"");
strcat(command,msg);
system(command);
return 0;
}
#include <stdio.h>
int main(void)
{
system("notify-send Test \"Hello World\"");
return 0;
}

character array to floating point conversion

I am trying to convert the output buffer(character array)
of the code below to floating point format for further calculations.
Can anybody tell me how to do it.
#include "usbtmc.h"
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <pthread.h>
int main()
{
int myfile;
char buffer[4000];
int actual;
myfile=open("/dev/usbtmc1",O_RDWR);
if(myfile>0)
{
system("echo MEAS:VOLT:AC?>/dev/usbtmc1");
actual=read(myfile,buffer,4000);
buffer[actual] = 0;
printf("Response = \n %s\n",buffer);
close(myfile);
}
return 0;
}
The sample output for this code is
Response =
+1.29273072E-04
You may have two ways:
using double atof(const char* str)
float f;
f = (float)atof(buffer);
printf("%f",f); // here you can use f
using int sscanf( const char * s, const char * format, ...)
float f;
sscanf(buffer,"%f",&f);
printf("%f",f); // here you can use f

Resources