In C, returning a float when sending pointer-to-struct values to a function changes the values of the struct
Date : March 29 2020, 07:55 AM
this will help I am trying to get some practice using structs and pointer-to-structs in the C language. I ran into an issue that has stumped me. I have set up a function that takes in 3 values from a pointer-to-struct and returns a float. However, when calling this function, it modifies the values of the original struct, eventually leading to an access violation and some nasty character regurgitation. After stepping through the code, the values are fine until I return the value of the function. // Return a pointer to our struct.
return fillInventory;
}
|
Modify dereferenced struct pointer changes most struct values, but not slices
Date : March 29 2020, 07:55 AM
seems to work fine Since b.board is a slice type, it is a reference type ( https://blog.golang.org/go-slices-usage-and-internals) and behaves like a pointer. So any changes made to possible.board will show up in b. You can try making a copy of b.board like so: func (b *Board) Move(orig, dest int) error {
// validation
...
// Update
possible := *b // A 'shallow copy'?
boardCopy := make([]byte, len(b.board))
copy(boardCopy, b.board)
possible.board = boardCopy
possible.updateBoard(orig, dest, val, isEmpassant, isCastle)
// ...
|
dereferencing pointer to incomplete type - assigning values to struct using pointer to a function
Tag : c , By : user169463
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You defined struct inv inside of your main function. As a result, it's not visible outside of main. This means that the struct inv mentioned in the declaration of the funciton values is a different struct, and one that hasn't been fully defined yet. That's why you're getting the "incomplete type" error. You need to move the definition outside of the function. #include<stdio.h>
struct inv {
int a;
float *p;
};
void values(struct inv *t, int , float);
void main()
{
struct inv ptr;
int z = 10;
float x = 67.67;
values(&ptr, z, x);
printf("%d\n%.2f\n", ptr.a, *ptr.p);
}
void values(struct inv *t, int w , float b) {
t -> a = w;
t -> p = &b;
}
|
Cast C struct double pointer to a Swift struct unsafe pointer
Tag : c , By : Enrique Anaya
Date : March 29 2020, 07:55 AM
it fixes the issue Let's think about that in terms of what's happening in C. When I do a cast of pointers in C, the data that represents the pointer type will now be treated as a pointer of a different type. The value of the pointer doesn't change, just how you treat it. Swift doesn't like to do this kind of thing and doesn't encourage you to do it because while cheap, it's a fundamentally unsafe thing to do and can lead to corrupting data, jumping into space, bus errors, etc. let a:UnsafeMutablePointer<SomeType> = fetchASomeTypePointer()
let b = unsafeBitCast(a, to: UnsafeMutablePointer<SomeOtherType>.self)
|
Cannot assign values to a struct pointer, after pointer was successfully assigned
Date : March 29 2020, 07:55 AM
This might help you Problem found: Cannot read whole address space (A20 on) in QEMU, works correctly with bit lower addresses. The C code above is correct, now the problem remains with QEMU/GRUB.
|