c99

Are all pointers derived from pointers to structure types the same?

我怕爱的太早我们不能终老 提交于 2019-11-30 11:22:59
问题 The Question The question of whether all pointers derived from pointers to structure types are the same, is not easy to answer. I find it to be a significant question for the following two primary reasons. A. The lack of a pointer to pointer to 'any' incomplete or object type, imposes a limitation on convenient function interfaces, such as: int allocate(ANY_TYPE **p, size_t s); int main(void) { int *p; int r = allocate(&p, sizeof *p); } [Complete code sample] The existing pointer to 'any'

Unsigned to signed conversion in C

那年仲夏 提交于 2019-11-30 09:21:01
问题 Is the following guaranteed to work or implementation defined? unsigned int a = 4294967294; signed int b = a; The value of b is -2 on gcc. From C99 (§6.3.1.3/3) Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. 回答1: The conversion of a value to signed int is implementation-defined (as you correctly mentioned because of 6.3.1.3p3) . On some systems for example it can be INT_MAX

C89 vs c99 GCC compiler

孤街浪徒 提交于 2019-11-30 09:05:53
Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two? #include <stdio.h> int main () { // Print string to screen. printf ("Hello World\n"); } gcc -o helloworld -std=c99 helloworld.c vs gcc -o helloworld -std=c89 helloworld.c Alok Singhal // comments are not a part of C89 but are OK in C99, falling off of main() without returning any value is equivalent to return 0; in C99, but not so in C89. From N1256 (pdf), 5.1.2.2.3p1: If the return type of the main function is a type compatible with int , a return

Is it legal to assign a restricted pointer to another pointer, and use the second pointer to modify the value?

旧街凉风 提交于 2019-11-30 09:05:17
问题 Does the following method respect the "restrict" contract? void fun(int* restrict foo) { int* bar = foo + 32; for (int i = 0; i < 32; ++i) *bar = 0; } My guess is no, but I need some clarification. 回答1: Yes, it sure respects the contract. 6.7.3 Type qualifiers 8 An object that is accessed through a restrict -qualified pointer has a special association with that pointer. This association, defined in 6.7.3.1 below, requires that all accesses to that object use, directly or indirectly, the value

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

夙愿已清 提交于 2019-11-30 08:38:35
Consider following example: #include <stdio.h> int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n", x); x = -b; printf("%ld\n", x); x = -c; printf("%ld\n", x); return 0; } To compile I am using GCC 4.4.7 (and it gave me no warnings): gcc -g -std=c99 -pedantic-errors -Wall -W check.c My result is: -15 -15 4294967281 The question is why both unsigned char and unsigned short values are "propagated" correctly to (signed) long , while unsigned int is not ? Is there any

Sequence points and side effects: Quiet change in C11?

孤人 提交于 2019-11-30 08:35:17
C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 72) Furthermore, the prior value shall be read only to determine the value to be stored. 73) with the footnotes 72) A floating-point status flag is not an object and can be set more than once within an expression. 73)

Can “sizeof(arr[0])” lead to undefined behavior?

瘦欲@ 提交于 2019-11-30 08:25:11
There is a well known pattern of figuring out array length: int arr[10]; size_t len = sizeof(arr) / sizeof(arr[0]); assert(len == 10); This pattern applies to static arrays and auto arrays of constant size. It also applies to variable length arrays in C99. I want to apply similar idea for figuring out dynamic array size in bytes: size_t known_len = 10; int *ptr = malloc(known_len * sizeof(int)); size_t size = known_len * sizeof(ptr[0]); assert(size == known_len * sizeof(int)); This is nicer than known_len * sizeof(int) because sizeof(ptr[0]) doesn't refer to actual array element type. Hence it

C99 printf formatters vs C++11 user-defined-literals

走远了吗. 提交于 2019-11-30 08:05:16
This code: #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> int main(int argc,char **argv) { uint64_t val=1234567890; printf("%"PRId64"\n",val); exit(0); } Works for C99 , C++03 , C++11 according to GCC 4.5 , but fails on C++11 according to GCC 4.7.1 . Adding a space before PRId64 lets GCC 4.7.1 compile it. Which one is correct? gcc 4.7.1 is correct. According to the standard, c++11 2.2 Phases of translation [lex.phases] 1 - The precedence among the syntax rules of translation is specified by the following phases. [...] 3. The source

Can GCC warn me about modifying the fields of a const struct in C99?

六眼飞鱼酱① 提交于 2019-11-30 05:38:36
I stumbled upon a small issue while trying to make const-correct code. I would have liked to write a function that takes a pointer to a const struct, to tell to the compiler "please tell me if I am modifying the struct, because I really do not want to". It suddenly came to my mind that the compiler will allow me to do this: struct A { char *ptrChar; }; void f(const struct A *ptrA) { ptrA->ptrChar[0] = 'A'; // NOT DESIRED!! } Which is understandable, because what actually is const is the pointer itself, but not the type it points to. I would like for the compiler to tell me that I'm doing

Function overloading in C using GCC - compiler warnings

依然范特西╮ 提交于 2019-11-30 04:24:29
问题 I am attempting to implement function overloading in C , and I am very close. I am using C99 so the _Generic keyword introduced in C11 is not available to me. I have developed some working code, but when I compile it I get a couple warnings. Working example: #include <stdio.h> #define print(x) \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int ), print_int(x) , \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char[]), print_string(x), \ (void)0)) void print_int