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.
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 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.
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 have this two arrays, codeblocks doesn't give me any build error but whenever I run it, it gives "Segmention fault" and it shuts the program down I've debugged it and found out I can't change values from names1 this way but changing names2 this way works just fine, is there a way to make this work? If yes how do I make troca work for names1?
void troca(char* frase){
unsigned i=0;
while(*(frase+i)!='\0') {
if(*(frase+i)=='O') {
*(frase+i)='0';
}
i++;
}
}
int main(){
char *names1[]={"JOAO","MANUEL","ROBERTO","ZE"};
char names2[][51]={"JOAO","MANUEL","ROBERTO","ZE"};
unsigned i;
for(i=0;i<4;i++) {
troca(names2[i]);
}
return 0;
}
The difference is:
names1 is declared simply as an array of string pointers without other defined characteristics. Using string literals here will put the string literals into a section in your executable file which is read-only, because the compiler can this way re-use them. For example, when you use char* a = "abc"; char* b = "abc"; then most likely a and b will have equal memory addresses as values. This means you can't modify them, so you get a "Segmentation fault" (another name for the same error is "Access Violation").
names2 is declared as an array of arrays of chars. Assigning a string literal there will copy the data of the strings into the array, and since there is no const thing in play in your code, the array has to be mutable, so in turn your strings stored in the char arrays are mutable as well.
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.
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
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int a[5][10][2];
int *p;
p = (int(*)[10][2])p;//Gives error!
return EXIT_SUCCESS;
}
I want to type cast p to type so that it can act as a pointer to the given 3-d array?Is there a way to do so.I am applyindg the idea that the type of a variable is everything except variable name.
Why are you trying to "typecast" anything? Why would you expect a value "typecasted" to (int(*)[10][2]) to be compatible with an int * pointer? And why does your original code assigns p to p, completely ignoring a?
This is what you can do
int a[5][10][2];
int (*p)[10][2] = a;
Now p is a pointer that can be used to access a, i.e. p[i][j][k] is equivalent to a[i][j][k]. No typecasting necessary.
If you write p = (int(*)[10][2])a; it won't give you any errors, may be a warning. You are thinking that p will be converted to pointer to a 3-D array, which is wrong. Try this statement after assigning a to p.
printf("addresses %u %u",p,p+1);
According to you, output should be something similar to this(lets say) "addresses 9990000 99940000", because you are thinking p is pointing to 3-D array. However you will get similar to "addresses 9990000 9990004", proving that p is a pointer to an integer.
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*