C Sprintf Format Error [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to format the given string and printf it. Bu it doesnt work. It gives error Any idea?
char* query_buffer;
sprintf(query_buffer,"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x;%u.%u.%u.%u;%d\n",
node_config->mac_address.addr[0], node_config->mac_address.addr[1], node_config->mac_address.addr[2], node_config->mac_address.addr[3],
node_config->mac_address.addr[4], node_config->mac_address.addr[5], node_config->mac_address.addr[6], node_config->mac_address.addr[7],
ip64_addr->u8[0], ip64_addr->u8[1], ip64_addr->u8[2], ip64_addr->u8[3],
node_config->coap_port);
printf("%s\n",query_buffer);
If I try below printf it works. I couldnt understand what is different between doing these two.
printf("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x;%u.%u.%u.%u;%d\n",
node_config->mac_address.addr[0], node_config->mac_address.addr[1], node_config->mac_address.addr[2], node_config->mac_address.addr[3],
node_config->mac_address.addr[4], node_config->mac_address.addr[5], node_config->mac_address.addr[6], node_config->mac_address.addr[7],
ip64_addr->u8[0], ip64_addr->u8[1], ip64_addr->u8[2], ip64_addr->u8[3],
node_config->coap_port);

The line char* query_buffer; declares a pointer to a char but it the memory it points to might not be declared. So you can get a segmentation fault when you call sprintf to access that memory. Try declaring query_buffer like char *query_buffer = (char*)malloc(256);. That will create a pointer and declare 256 bytes at where it points to.

Related

Segmentation fault (core dumped) with strcpy [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'v encountered "Segmentation fault (core dumped)" when compile my C program in *nix. I've narrowed the mistake to this line (without this line my program can run):
strcpy(con[count], "1234");
Before that, I declared con as:
char *con[30];
And count is always smaller than 30.
What's wrong with this line? How should I change it?
char *con[30];
declares an array of 30 pointers to strings. This is not what you need. It fails because you then try to copy to the first string, but did not allocate the first string (only a pointer to it)
You need
char con[30];
and then
strcpy(con, "1234");
Or (as Lee Danial points out) you might have wanted an array , in which case you need
char *con[30];
then
con[count] = strdup("1234")
or
con[count] = "1234"
The first one allocates a string and copies it for you (a combination of malloc and strcpy). The second one just points at the supplied literal, it doesn't make a copy. Hard to say which is 'best' for you.
PS strdup is equivalent to
x = malloc(strlen(str) + 1);
strcpy(x, str);
return x;

C passing string to function issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm having an issue passing a char* to a function in C.
I have a method dns_magic, where one of the parameters is a char *addr. In addition, I have a char temp[20], which contains a string (typically something like irc.rizon.net). Right before I call dns_magic(temp), I do a printf("Temp: %s", temp) and it prints as expected. In the first line of dns_magic, I call printf("Addr: %s", addr), and it prints nothing (as if the string is null).
Here's the relevant part of the code:
printf("Temp: %s", temp);
res = dns_magic(temp, conf->port, &hints);
and
struct addrinfo *dns_magic(char *addr, int port_i, struct addrinfo hints)
{
printf("Addr: %s", addr);
Could someone help tell me what I'm doing wrong? I've tried changing dns_magic to take a char[20] and disabling compiler optimization, but neither seems to have fixed the issue.
Any help is appreciated.
Turn on compiler warnings and get rid of them.
I'd suggest that your 3rd parameter is messing things up. You declare a whole struct but pass in a pointer to one. The stack is messed up and the function can't properly find the parameters passed.
You should make the last parameter a struct addrinfo *hints with appropriate const modifiers.

Array of Struct pointers, get segfault in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#define MAX_SPACES 10
#define MAX_SIMPLE_EVENTS 5000
#define MAX_USER_EVENTS 1000
struct {
EventSpace* p_spaces[MAX_SPACES];
SimpleEvent* p_simple_events[MAX_SIMPLE_EVENTS];
UserEvent* p_user_events[MAX_USER_EVENTS];
}* G_manager;
static void add_space(EventSpace* space){
static uint16_t index = 0;
(*G_manager).p_spaces[index] = space;
}
After running in gdb got:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400559 in add_space (space=0x7fffffffdf30)
How to impove it?
At least in the code you're showing, you haven't actually allocated the memory for the structure containing the arrays; you've only created a pointer. So when you dereference that pointer meaning to write into an instance of the structure, you hit a random memory address and got the relatively tame result of your program crashing.
You could change G_manager from a pointer-to-struct to an instance of the struct itself; or you can malloc a buffer big enough for the structure and assign that buffer to G_manager

I want to convert a char (popped from the stack) to integer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
char i[]=pop();
char j[]=pop();
b=atoi(i);
a=atoi(j);
I wanted to pop an char type element from stack and convert it to int type. But it says
invalid initializer.
What is the problem?
If you want a char variable, use a char variable, don't use a char array.
Change
char i[] = pop();
to
char i = pop();
and likewise.
That said, atoi() won't be relevant there. If you want the result to be of type int, simply use an int variable.

function pointer C programming [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm not sure what the following does and i'm hoping someone can clarify the purpose of having the asterisk in front of the functions name:
char *Foo(char *ptr) {
return NULL;
}
I understand that you can pass by value the memory location of something in the function argument call and *ptr would be the pointer to it. I understand you can create a pointer function that can be used to point to other functions like a regular pointer points to variable memory location but in this case this is not a function pointer that we can point to other functions, or is it? This seems like a real function.
Foo is a function.
It has input: ptr of type char*
It has output of type char*
char* means "pointer to char"
it returns NULL.
That is the most plain explanation I can think of.
its misleading you, the * by the name isn't related to the name
it means the same as char* Foo(char* ptr)
which means a function which takes a char* and returns a char*

Resources