I type this code to get the home directory. I have later edited it to include all of the code:
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdarg.h>
#include "anotherfile.h"
typedef unsigned int uint;
void Interval(void) {
static uint S = 0;
static uint C = 0;
static uint M = 0;
static uint D = 0;
usleep(10e5/0x20);
printf("%d\n", C);
printf("%d\n", S);
printf("%d\n", M);
if(C == 0x20) {
if(S == 59) {
S=0;
M++;
}else{S++;}
C=0;
}else{C++;}
Interval();
}
int main(int argc, const char *argv[]) {
char *HomeDir;
if((HomeDir = getenv("HOME")) == NULL) {
HomeDir = getpwuid(getuid())->pw_dir;
if(HomeDir == NULL) {
printf("Failed to get Home Directory\n");
}else{printf("Retry Home Directory Found\n");}
}else{printf("Success getting Home Directory\n");}
Interval();
return 0;
}
It gives me the implicit declaration warning. It says something is wong with the getenv partHow can I fix it?
The function getenv is declared in stdlib.h according to this reference. So you need to add
#include <stdlib.h>
Related
How can I do global constant initialization like this? Is it possible at all? Or there is another way to do what I want? I mean I need global parameters gained from main() and they must be constants.
#include <stdio.h>
#include <stdlib.h>
const int var;
int main(int argc, char *argv[]) {
var = atoi(argv[1]);
/* ... */
return 0;
}
I need global parameters gained from main() and they must be constants.
No portable way to do directly as OP wants.
Code needs different access for reading and writing. Effectively hiding the access to the true data.
A close solution it to set and get data via functions defined in another file. Then no way to change data once set and only settable once.
main_var.h
int main_var_get(void);
void main_var_set(int v);
main_var.c
#include <stdlib.h>
#include "main_var.h"
static int var; // This could instead be a struct of many members.
// Or a pointer to a struct with many members.
static int var_init;
int main_var_get(void) {
if (!var_init) {
// Handle call of get before set, perhaps exit or return default value
exit(EXIT_FAILURE);
}
return var;
}
void main_var_set(int v) {
if (var_init) {
// Handle 2nd set, perhaps exit or ignore
exit(EXIT_FAILURE);
}
var = v;
var_init = 1;
}
main.c
#include <stdio.h>
#include "main_var.h"
int main(void) {
main_var_set(42);
...
printf("%d\n", main_var_get());
}
Another is to use a const int *. Access before setting is the same no-no as dereferencing NULL. Attempting to write *main_var_addr is UB like writing any const object.
main_var.h
extern const int *main_var_addr;
void main_var_set(int v);
main_var.c
#include <stdlib.h>
#include "main_var.h"
const int *main_var_addr = NULL;
static int var;
void main_var_set(int v) {
if (main_var_addr) {
// Handle 2nd set attempt, perhaps exit or ignore
exit(EXIT_FAILURE);
}
var = v;
main_var_addr = &var
}
main.c
#include <stdio.h>
#include "main_var.h"
int main(void) {
main_var_set(42);
...
printf("%d\n", *main_var_addr);
}
I don't think C allows you to initialize a constant variable elsewhere. However, you can just change var from const to static.
This is not possible, actually const doesn't mean the variable is constant in C, it's only mean that you are not allowed to change the value of the variable, but someone else could.
Global value are initialize before the main run, that mean you can't initialize at runtime, how this initialization is done is implementation behavior so there is no "pure C" way to do what you ask.
However, I don't see why you could not encapsulate your global:
my_var.h:
int init_my_var(int argc, char **argv);
int get_my_var(void);
my_var.c
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
static int var;
int init_my_var(int argc, char **argv) {
#ifndef NDEBUG
#include <stdbool.h>
#include <assert.h>
{
static bool first = true;
assert(first);
first = false;
}
#endif
if (argc < 2) {
return 1;
}
errno = 0;
long ret = strtol(argv[1], NULL, 10);
if (errno || (ret < INT_MIN || ret > INT_MAX)) {
return 2;
}
var = (int)ret;
return 0;
}
int get_my_var(void) {
return var;
}
main.c:
#include <stdio.h>
int main(void) {
printf("%d\n", get_my_var());
if (init_my_var(2, (char *[]){"", "42"})) {
return EXIT_FAILURE;
}
printf("%d\n", get_my_var());
if (init_my_var(2, (char *[]){"", "0"})) {
return EXIT_FAILURE;
}
}
This is the error that I am facing.
ERROR: warning: passing argument 1 of ‘__builtin_strlen’ makes pointer from integer without a cast [-Wint-conversion]
Question Link:Hackerrank Balanced Brackets
My Code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
char rev(char c){
char ch;
if(c=='[')
ch=']';
else if(c=='{')
ch='}';
else if(c=='(')
ch=')';
return ch;
}
int main(){
int t; //test cases
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
char* s = (char *)malloc(10240 * sizeof(char));
int l,ret;
scanf("%s",s);
l=strlen(s);
char *st;
st=(char *)malloc(l * sizeof(char));
int top=-1;
for(int i=0;i<l;i++){
char ch=s[i];
char chr=rev(ch);
ret=strcmp(st[top],chr);
if(ret!=0){
top++;
st[top]=ch;
}
else{
top=top-1;
}
}
if(top==-1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
I shall be really thankful if anybody can help me out.
Thanks in advance.
I am begginer in C and I have a problem with linking .c and .h files into main.c
I tried to link it and it looks like this in main.c :
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "hangman.h"
#include "hangman.c"
#include <string.h>
#include <time.h>
#include <ctype.h>
int main(void)
{
char secret[20];
srand(time(NULL));
getWord(secret);
hangman(secret);
return 0;
}
and like this in .c file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include "hangman.h"
int isWordGuessed(const char secret[], const char lettersGuessed[]){
int pom = 0;
int i,j;
for(i = 0; i < strlen(secret); i++){
for (j= 0; j < strlen(lettersGuessed); j++)
{
if(secret[i] == lettersGuessed[j])
pom = 1;
}
if(pom == 1){
pom = 0;
}
else{
return 0;
}
}
return 1;
}
and like this in .h file
#define WORDLIST_FILENAME "words.txt"
int isWordGuessed(const char secret[], const char lettersGuessed[]);
Program just throws an exception. It tells me:
hangman.o: In function isWordGuessed':
hangman.c:(.text+0x0): multiple definition ofisWordGuessed'
main.o:main.c:(.text+0x0): first defined here
#include "hangman.h"
#include "hangman.c"
Do not include .c source files in your program. The include directive will simply copy hangman.c in your main.c.
I have the following code:
#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <ostream>
#include <sstream>
#include <bitset>
#include <numeric>
#include<fstream>
using namespace std;
char str[] = "this.is.a.test";
char str2[] = "this.is.another.test";
typedef struct
{
size_t count;
char** strings;
} Tokens;
Tokens Tokenize(char* String, char Split)
{
Tokens t;
t.count = 1;
for (size_t i = 0; String[i] != 0; i++)
{
if (String[i] == Split)
t.count++;
}
t.strings =(char**) malloc(sizeof(char*)* t.count);
if (t.count > 0)
t.strings[0] = String;
for (size_t i = 0, j = 1; String[i] != 0; i++)
{
if (String[i] == Split)
{
t.strings[j] = &String[i + 1];
String[i] = 0;
j++;
}
}
return t;
}
int main(void)
{
Tokens t = Tokenize(str, '.');
printf("number of strings: %i\n---\n", t.count);
for (size_t i = 0; i < t.count; i++)
{
printf("%i: %s\n", i, t.strings[i]);
}
free(t.strings);
}
The problem is when I debug the code and especially that line t.strings[j] = &String[i + 1];
In a test case of this.is.a.test
At the first found dot . , it should points to this, but in the debugger it shows the following picture.
enter code here
What the debugger shows is correct at line 55. The assignment has been made, so t.strings[j] points after the dot.
Note that in Tokenize you allocate Tokens t; on the stack and later return this t. That is bad (very bad!). Because t is on the stack, it will be overwritten by the call to printf.
(And although most is C, formally it is C++ as in C you cannot declare a variable in the for initialization, as in for (size_t i = 0;)
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.