Linked Questions

-1 votes
2 answers
592 views

Why casting unsigned to signed directly in C gives correct result? [duplicate]

In C, signed integer and unsigned integer are stored differently in memory. C also convert signed integer and unsigned integer implicitly when the types are clear at runtime. However, when I try the ...
doge99's user avatar
  • 188
96 votes
8 answers
54k views

Is it possible to initialize a C pointer to NULL?

I had been writing things like char *x=NULL; on the assumption that char *x=2; would create a char pointer to address 2. But, in The GNU C Programming Tutorial it says that int *my_int_ptr = ...
fagricipni's user avatar
32 votes
2 answers
2k views

Indexing an `unsigned long` variable and printing the result

Yesterday, someone showed me this code: #include <stdio.h> int main(void) { unsigned long foo = 506097522914230528; for (int i = 0; i < sizeof(unsigned long); ++i) printf(&...
mediocrevegetable1's user avatar
10 votes
8 answers
4k views

What happens if I cast a function pointer, changing the number of parameters

I'm just beginning to wrap my head around function pointers in C. To understand how casting of function pointers works, I wrote the following program. It basically creates a function pointer to a ...
sleske's user avatar
  • 82.7k
3 votes
3 answers
592 views

Might casting void* into unsigned long int cause undefined behaviour even where sizeof(void*)==sizeof(unsigned long int)

size_t size_int = sizeof(unsigned long int); size_t size_ptr = sizeof(void*); printf("sizeof(unsigned long int): %zu\n", size_int); printf("sizeof(void*): %zu\n", size_ptr); if(...
ahk's user avatar
  • 43
-3 votes
2 answers
2k views

Don't understand a piece of code like "....*(BYTE *)p=0xff;...."

The C code is like this: #include <stdio.h> typedef unsigned char BYTE; int main(void) { unsigned int num, *p; p=&num; num=0; *(BYTE *)p=0xff; } But I do not understand ...
Omg I killed Kennny's user avatar
3 votes
1 answer
384 views

Is the alignment requirement for incomplete `struct X` and `struct Y` the same?

An answer to "C: When is casting between pointer types not undefined behavior?" indicates that casting forth and back via a pointer with no stricter alignment requirement is safe. The alignment is ...
skyking's user avatar
  • 14k
3 votes
2 answers
273 views

When undefined behavior can be considered well-known and accepted?

We know what undefined behavior is and we (more or less) know the reasons (performance, cross-platform compatibility) of most of them. Assuming a given platform, say Windows 32 bit, can we consider an ...
Adriano Repetti's user avatar
1 vote
2 answers
97 views

Assigning variable address to pointer. Dereferencing pointer causes segmentation fault. How?

As you can see I'm just taking a variable address and taking it back and forth through other variables and pointers. Through printf I can see that address1, value2 and address 2 all hold the same ...
Roberto Maisto's user avatar
1 vote
1 answer
247 views

Reinterpreting memory/pointers

Just a quick question concerning the rust programming language. Assume you had the following in C: uint8_t *someblockofdata; /* has certain length of 4 */ uint32_t *anotherway = (uint32_t*) ...
Leon's user avatar
  • 37
1 vote
2 answers
172 views

Implementation specific behavior when casting a pointer to a data type

From my understanding, according to C standard, casting an int pointer to int is unadvised for portable code. A simple example would be doing such a cast on a 64-bit architecture where pointers are 64 ...
keyermoond's user avatar
2 votes
2 answers
116 views

Is there a failsafe way to determine the alignment/trailing bits of a pointer in C?

In C, a pointer is aligned to a certain if a certain number of trailing bits are zeros. This requires extracting the bits of a pointer, albeit only bounded number of trailing bits. The obvious way is ...
CPlus's user avatar
  • 4,154
2 votes
1 answer
143 views

C Guarantees Re: Pointers to Void and Character Types, Typecasting

My best-effort reading of the C specification (C99, primarily) makes me think that it is valid to cast (or implicitly convert, where void *'s implicit conversion behavior applies), between any of ...
mtraceur's user avatar
  • 3,569
0 votes
2 answers
59 views

What does this line mean in C99?

static int* p= (int*)(&foo); I just know p points to a memory in the code segment. But I don't know what exactly happens in this line. I thought maybe it's a pointer to a function but the format ...
TheLogicGuy's user avatar
0 votes
1 answer
48 views

Const array in C: fixing assignment from incompatible pointer type [-Werror]

I am not able to access const array sfp_addr and assign it to another const array variable. Here is my code (it has been edited for brevity): #define SFP_NUM 2 const int sfp_addr[SFP_NUM] = { ...
Sedmaister's user avatar