adding an int to a Multi-dimensional char Array - c

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...

Related

Why can't I access my pointer of char through my function?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
void *process(char **nbE)
{
char buffer[8] = "test";
*nbE = &buffer[0];
printf("%s\n", *nbE);
}
int main(int argc, char **argv)
{
char *str;
process(&str);
printf("%s\n", str);
}
I'm trying to get the value of *nbE in main() by making it points to the address of first char in my array.
But it returns something not encoded, why?
What would be a way for me to do this way?
Note: I know I can do it simpler, I have a more complex code and this is a mini example
Basically I have something interesting in my array and want to pass it to my main function through a char* variable
char buffer[8] = "test";
creates a string that is local to the function, it is destroyed once you return from that function. Do this
static char buffer[8] = "test";
or
char * buffer = strdup("test");
you have to release the string when you have finsihed with it in the second case

Output different value after function call?

I'm confusing about the different sizeof() return value after function check() calls. And buf exactly the same as buffer based on the printf() for each char. Any reply is awesome! Thx.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void check(char *buf)
{
printf("%d \n", sizeof(buf)); // **output 8**
}
int main(int argc, char *argv[])
{
char buffer[] = "\x64\x49\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33";
printf("%d\n", sizeof(buffer)); // **output 33**
check(buffer);
}
In function check buffer is a pointer to the char and its size is 8
In the main furnctions buffers is the 33 elements char array and its size is 33
To get the length of the C string use strlen function.
Generally there is no way of getting the size of the array referenced by the pointer. You need to pass the size as a anothother parameter.
In your example:
void check(char *buf, size_t size)
{
printf("%zu \n", size);
}
int main(int argc, char *argv[])
{
char buffer[] = "\x64\x49\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33\x64\x49\x00\x00\x00\x55\x33\x33";
printf("%d\n", sizeof(buffer)); // **output 33**
check(buffer, sizeof(buffer));
}

Storing user-inputted strings into a string array in c?

Fairly new to C, I am trying to read a file of multiple words using bash indirection, and put the words into a string array. The end of the file is marked with a -1.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[400000];
init(words);
int i = 0;
do{
printf("%s",words[i]);
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int i = 0;
do{
fgets(words[i],1024,stdin);
i++;
}while(!strcmp(words[i],"-1"));
}
This gives me a segmentation fault, if any other information is needed I'm more than happy to provide it.
If I guessed correctly, '400000' means the max lines the user can input. But the default size of stack on Windows OS is 1M, sizeof(void*) * 400000 = 1,600,000...
The other thing is that you have not allocated memory for every line.
So, I try to correct your code like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 4000 // '400000' is really too big!
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[MAX_LINE];
memset(words, 0 , sizeof(words));
init(words);
int i = 0;
do{
printf("%s",words[i]);
delete words[i];
words[i] = nullptr;
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int maxLen = 1024;
int i = 0;
do{
words[i] = new char[maxLen];
memset(words[i], 0, maxLen);
fgets(words[i], maxLen, stdin);
i++;
}while(!strcmp(words[i],"-1") && i < MAX_LINE);
}

Declaration char* not working with strcat()

This code compiles without errors, but upon opening the app, it says:
file.exe has stopped working
#include <string.h>
#include <stdio.h>
int main() {
char *a = 'Hello';
char *b = 'World';
strcat(a,b);
puts(a);
}
Where did I go wrong?
You need to allocate sufficient space and use double quote instead of single quote. You could use array.
#include <string.h>
#include <stdio.h>
int main() {
char a[20] = "Hello";
char b[10] = "World";
strcat(a,b);
puts(a);
}
Constant strings are not modifiable. This is a proper way to declare, initialize and modify a string buffer in C:
#include <string.h>
#include <stdio.h>
int main() {
char a[20];
char *b = "World";
strcpy(a,"Hello");
strcat(a,b);
puts(a);
return(0);
}
You can't do strcat on pointer of characters. You only can do strcat on array of characters .... Sorry for my precedent answer, look at the code below :
#include <string.h>
#include <stdio.h>
int main() {
char a [20];
char b[20];
strcpy(a,"Hello");
strcpy(b,"World");
strcat(a,b);
puts(a);
return(0);
}

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

Resources