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.
Related
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I come from a Java background and I started reading K&R, but the progress is extremely slow, because I know most of it, but still have to read everything again. So, I was thinking that I could maybe ask here some things about the C programming language to learn things a lot faster.
What I want to know is
What happens when I pass a struct variable with a &-prefix as an argument to a function? The code sample that I am trying to understand is:
struct somestruct st;
somefunction(&st);
1.1. What kind of signature does somefunction need to have and what exactly is passed?
1.2. A pointer to the struct variable would be *st instead, right?
What does it mean when a function has as a parameter sometype ** variable_name? The code that I want to understand is:
int main(int argc, char **argv)
The whole code that I want to understand is here: https://stackoverflow.com/a/35355069/3668527
Please no explanations of the code. I know what it does. I just need to know what those strange new C operators & and ** mean.
Edit: Oh, and please tell me how these operators are called!
& get pointer to that variable.
The function signature should be: void somefunction(struct somestruct *st), i.e. it will accept pointer to that structure.
strct * means pointer to strct, strct ** means pointer to pointer to strct etc.
What happens when I pass a struct variable with a &-prefix as an argument to a
function? The code sample that I am trying to understand is:
The & operator returns the adress of an object.
What kind of signature does somefunction need to have and what exactly is passed?
void somefunction(struct somestruct *pointer);
You can put in "const" in a few places to tell the compiler that you don't want to allow the method to do any changes.
1.2 A pointer to the struct variable would be *st instead, right?
Depends in which context "*st" is used, if you just want to create a pointer do it that way:
somestruct *pointer = NULL; // or init it somehow
Edit: Oh, and please tell me how these operators are called! Thanks!
They are called adress operators.
What does it mean when a function has as a parameter sometype ** variable_name
That means that the parameter is a pointer pointing to another pointer.
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 7 years ago.
Improve this question
There is code using c:
const char *bits[] = {"0000","0001","0010"};
When I call printf-function it works fine:
prinft("%s",bits[1]); // this prints correct value
But when I try call subroutine function it stops program:
print(*bits);
void print(const char *bits)
prinft("%s",bits[1]); // system stops working
How can I print a correct value?
When you call print(*bits) you give bits[0] as the argument, which is char*. Then you tell printf to output a string (which is char* in C) and you give it bits[1] which is actually of type char, specifically the second char in the bits. printf will treat this value as char* and this will naturally cause a problem.
At first, I am a Chinese Student.So maybe my English writting is not good.
I think there are some mistakes in your code.
you should understand pointer(example: int *ptr or int ptr[]) and the pointer points to a pointer(example: int **ptr or int *ptr[]).In your codes, bits is a pointer points to a pointer, because you used * and [] meanwhile.
So, if you want to transfer bits to the print() Function, you should use
void print(const char **bits){
printf("%s\n", bits[0]);
}
or you can write print() as below:
void print(const char *bits){
printf("%s\n", bits);
}
//in main()
print(bits[0]);
it depends what you what print() to do.
ps:This is my first answer on Stack Overflow. if you find some Grammar mistake with English, please tell me.Thx.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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
Improve this question
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I can cast int to char but I need cast int to char*.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
char ch_number_of_rev[4];
version = "0.";
itoa(number_of_revision,ch_number_of_rev,10);
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
but I need cast int to char*
Casting only changes the type - it does not change the value within the variable. If you need to convert an int to array of chars (i.e. a string) then use sprintf or snprintf:
char* buffer = ... allocate a buffer ...
int value = 970;
sprintf(buffer, "%d", value);
Converting int to string in c
Also, you have not allocated any memory for version - use malloc and allocate some memory.
strcat here won't work because you haven't allocated any space to store the result in. Your version is probably in read-only memory anyway, so you'd get a segfault, otherwise you'll get memory corruption. So make sure to allocate enough space for it, e.g. by using
char version[10] = "0.";
You may want to read up on pointers first, though.
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*