C strange if statement syntax [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I saw this if statement and I am not sure exactly how that works, what is being compared with what? Is there a lack of && or ||? The parens are confusing me.
if ((list->func)((list->head)->dataPointer, newOb) < 0) {

what is being compared with what? The parens are confusing me.
The result of the part between if( and ) is being compared with 0, like in every if statement.
Is there a lack of && or ||?
I don't see any && or ||
The parens are confusing me.
What (list->func)((list->head)->dataPointer, newOb) does is (not necessarily in this order):
Evaluate list->func
Evaluate (list->head)->dataPointer
Call list->func passing the two arguments (list->head)->dataPointer and newOb.
The result of this is then compared to 0 because it's what's between the if( and ).

Okay, so the first set of parentheses are indicating that list is a pointer to an object which has a member called func. func is a function. So (list->func) is a function call. The function apparently takes two arguments. The first argument that is being passed in is (list->head)->dataPointer. (list->head) indicates that the object that the pointer called list is pointing at has a member called head. head is also a pointer, and it points at an object with a member called dataPointer. The second argument that is being passed to the function (list->func) is newOb. The function (list->func) is apparently returning some sort of number value, probably an int.
The code could be rewritten as:
if( list->func( list->head->dataPointer, newOb ) < 0 )
if that helps you see what is going on.

Related

Visual Studio complains about few arguments to VirtualAlloc despite correct arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 15 days ago.
Improve this question
I've been trying to learn my way through the winapi in C, and I'm trying to use the VirtualAlloc() function to allocate memory. The function clearly takes 4 arguments and I'm supplying 4 arguments as per the windows API documentation.
Function body:
VirtualAlloc(
lpAddress,
dwSize,
flAllocationType,
flProtect
);
How I'm supplying the function:
//values I inserted here have the correct types, again the problem is that it complains about few arguments
void* memory = VirtualAlloc(
NULL,
DRAWING_AREA_MEMORY_SIZE,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE
);
I tried using only one allocation type instead of piping, just in case that caused the problem for some reason, but to no avail
Error Copied from the compiler:
Error (active) E0165 too few arguments in function call
I can't tell what could be the problem. Funny enough, no matter how many other arguments I throw in, it stays too few and never changes to too many. What is going on here?
Problem was an extra parenthesis in the DRAWING_AREA_MEMORY_SIZE macro. Thanks to #ChrisKushnir and #IInspectable for leading me to the solution

Why won't legal C code with 4 dashes compile in GCC? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
This is legal C code.
INT m = -3;
INT t = ----m;
decrement(--) positive m (-m) and Negate (-)
(-)(--) -m
but fails to compile with error: lvalue required as decrement operand
Does anyone know why this doesnt compile?
You cannot compile -- (-m) nor --(--m) as in both cases the argument to -- is not something that can be written to. There is no place to write back the value of -- unless it's immediate argument is a variable or a dereferenced pointer. - doesn't work at all as that is now a temporary.
The output of a pre/post increment/decrement operator is again not something that can be written to (this is immediately clear on post, but it's also true on pre).
Note that C is not a backtracking language*; so it's not going to find the only legal parse of -(-(-(-m))) on its own. It did find it after I put spaces in - - - - m as expected.
*Having used one, you don't want it. The results are surprising.
----m is not -, --, -, m.
----m is --, --, m because the compiler always finds the longest token.
The compiler is telling you that --, --, m is an illegal construct.

Argument value in C immutable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I have the following code:
void func(uint8 *var) {
uint8 tempvar;
if (var)
var = &tempvar;
*var = 0;
}
I call the function using:
func(NULL);
The code gives a segmentation fault at the line "*var = 0;" because var still points to the memory address 0x0. I don't understand why my assignment to a temporary variable did not work!
Because you omitted the !. You are testing whether the variable exists, but you should test whether it doesn't exist: if (!var) ...
To expand a bit further... var is of type uint8 *, thus var itself is a pointer. By writing if (var) you are testing if that pointer is not NULL. In pseudo-code, your code says:
if (the var pointer already exists)
assign a new pointer to it (make it point to somewhere else)
But if it doesn't exist (if it points to NULL), you leave it alone. You can verify this using a debugger or a simple print statement. Thus you end up at the assignment with a null-pointer, and crash your program.

Application of null pointer [closed]

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 8 years ago.
Improve this question
I was giving interview in a company and they asked me "What are applications of null pointers in C"? I told them what null pointer is and how it leads to segmentation fault. However, they were not happy with the answer. I have never heard anything like this before. If anyone has any idea, please share.
Thank you
they were asking when you need to use NULL pointers, not when they lead to problems.
The classic answer is that they are used when there is nothing to point to. For example the next pointer in a list when its the end of a list -> there is no next item.
and so in your code you go
if(nextItem != NULL)
{
// do stuff with nextItem
}
else
{
// the end
}
As mooseboy points out this is a sentinel value i.e, a special value that your code recognizes as having a deciding value.
NULL is the perfect pointer sentinel since NULL is assumed to never be a valid value for a pointer (for example you could not use 0x000042 since 42 might be a real address)
This question is kind of vague, but one thing they might have been unhappy about is that from a pure language perspective, dereferencing NULL is not guranteed to cause a segfault. It causes unspecified behavior, i.e. the language doesn't say what will happen. On most platforms, yes, null reference causes a segfault, but an implementation of C could do something else and still conform to the standard.

int value changing when passing it into a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm passing in an int to a function in my lame program. It's passing in a number to convert it to a binary representation as an int array.
typedef int bool;
bool* conv2bin(int num)
{
blah blah blah return binary as bool array
}
I pass in 78 and if I printf() immediately after it's passed in, I get 781237412753-124?
I'm new to C (coming from C++) so please tell me if I'm doing something really dumb?
This seems like it should be really easy but it isn't...?
EDIT:
Have I done goofed:
printf("%d", num);
EDIT 2:
It has to be something with the int because at the end of the function, it checks to see if we subtracted numbers sufficiently to get to num==0 but it says we're not at 0. It's doing really weird things. It also says that the binary is 0000000001001111, and it should be 0000000001001110.
Edit 3:
Wow I suck. Thank you Floris! It's been a long day.
Guessing hereā€¦
Your printout starts with the correct two digits: 78.
But if you do not include a \n at the end of your formatting string, then the next thing you print will be concatenated. As will the next thing, and the next.
I suspect your problem will disappear when you change your print statement to
printf("%d\n", num);

Resources