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
I am encountering a difficulty in trying to understand the usage of pointers in C programming. I do not understand why this doesn't compile:
void func(char**p);
void other_fun(void)
{
char arr[5][3];
func(arr);
}
The major issue is that arrays are not the same as pointers. Syntactically, you can use them in a very similar manner within a function, but they are not the same. As such, what you are passing in func(arr) is a variable of type char (*)[3], or a pointer to an array of 3 chars, and not a pointer to a pointer, which is what func expects.
Related
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 9 months ago.
Improve this question
I'm still very new to C, have only been learning for about a week at this point, but something I don't get is the use of pointers. I get how pointers work and I know how to use them, but I don't see why or when they should be used. Can someone please explain this to me?
It is enough to mention that arrays used in expressions with very rare exceptions are implicitly converted to pointers to their first elements.
For example if you will write the expression
arr[i]
where arr is the array name then even in this expression the array is converted implicitly to a pointer to its first element.
Or another simple example: all C string functions deal with pointers of the type char * because passed arrays again are implicitly converted to pointers to their first elements.
Think about how to write a general sort function for arrays of any types.
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 1 year ago.
Improve this question
can I use post-increment in a function return in C like this?
int meta_solve() {
//some codes
return metaData[head++]; //head is global variable
}
I asking this question because it showing the different results on windows and mac. thanks for your attention. have a great day!
Yes, that will work.
The return will not happen until the expression metaData[head++] is fully evaluated so the (global) variable head is incremented before the function returns.
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 1 year ago.
Improve this question
When I implement my code, my function does not work. However, I don't know the reason why. Can you tell me some problems and how to solve it?
Here's my code! And I'm using language C!
You are calling the function in wrong way:
try this:
findMin(n,arr);
findMax(n,arr);
Whenever you are calling function: in function just pass the array name;
Whenever you are calling a array in function just write its array name without [] bracket.
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 am analyzing a C code that I have been given and I came across this block:
for (k=0; k<m; k++)
{
//Perform some calculation and assign result to
//A[k].
if (A[k]!=A[k])
{
exception=1;
}
}
I have performed runs of the code where exception does turn out to be one, but I can't seem to understand how two array indices can contain different numbers! Is that something to do with machine precision? Thank you!
You might want to check whether the A[] array is allocated sufficient amount of memory: it should have 'm' elements allocated at least. If everything is OK, check the sizes of other arrays allocated in your program. The phenomenon that you've encountered looks like some memory allocation error.
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 8 years ago.
Improve this question
some_struct = some_function; //some_function returns a pointer to an instance of a struct
some_struct->num = 8; //num is an int
The second line creates a segmentation fault, when I try to use gdb with the p some_struct->num command, it says Cannot access memory at address 0x0
How do I set the value of some_struct->num without creating a segmentation fault?
Your function is returning a NULL pointer. Make sure it returns valid memory for a some_struct type.