ansi-c

string array conversion

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:54:47
问题 I have the following code: char *array1[3] = { "hello", "world", "there." }; struct locator_t { char **t; int len; } locator[2] = { { array1, 10 } }; It compiles OK with "gcc -Wall -ansi -pedantic". But with another toolchain (Rowley), it complains about warning: initialization from incompatible pointer type on the line where char **t is. Is this indeed illegal code or is it OK? Thanks for all the answer. I now know where my problem was. However, it raises a new question: string array

string array initialisation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:53:14
问题 This is a continuation of another question I have. Consider the following code: char *hi = "hello"; char *array1[3] = { hi, "world", "there." }; It doesn't compile to my surprise (apparently I don't know C syntax as well as I thought) and generates the following error: error: initializer element is not constant If I change the char* into char[] it compiles fine: char hi[] = "hello"; char *array1[3] = { hi, "world", "there." }; Can somebody explain to me why? 回答1: In the first example ( char

All struct identifiers are automatically forward declared

别来无恙 提交于 2019-12-10 12:34:01
问题 While answer warning: assignment from incompatible pointer type for linklist array, I noticed any undeclared identifier perceded with struct keyword are considered as forward declared identifiers. For instance the program below compiles well: /* Compile with "gcc -std=c99 -W -Wall -O2 -pedantic %" */ #include <stdio.h> struct foo { struct bar *next; /* Linked list */ }; int main(void) { struct bar *a = 0; struct baz *b = 0; struct foo c = {0}; printf("bar -> %p\n", (void *)a); printf("baz ->

Meta language to code generate packed structs for ANSI-C and C# Structs

青春壹個敷衍的年華 提交于 2019-12-09 23:14:38
问题 I'm trying to find a "meta language" that can be used to define a structure and get/set code for members. The catch is that the structure already exists in code, and this "meta language" would serve as bit-for-bit replacement of the original hand-coded structure to allow the headers describing the structures to be generated. The point is that the structures are used as part of a protocol between a C# application and an embedded device (not linux based, think smaller and more constrained like

C program to perform a pipe on three commands

梦想与她 提交于 2019-12-08 02:34:30
问题 I have to write a program that will perform the same operation that du | sort | head in the command line would do, but I'm stuck, and my program is not working. The output right now is 112 . and the program doesn't terminate. Please help, I don't know what to do! int main(void) { int fd[2]; int fd1[2]; int pid; if (pipe(fd) == -1) { perror("Pipe"); exit(1); } switch (fork()) { case -1: perror("Fork"); exit(2); case 0: dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); execl("/usr/bin/du"

C program to perform a pipe on three commands

…衆ロ難τιáo~ 提交于 2019-12-06 09:47:44
I have to write a program that will perform the same operation that du | sort | head in the command line would do, but I'm stuck, and my program is not working. The output right now is 112 . and the program doesn't terminate. Please help, I don't know what to do! int main(void) { int fd[2]; int fd1[2]; int pid; if (pipe(fd) == -1) { perror("Pipe"); exit(1); } switch (fork()) { case -1: perror("Fork"); exit(2); case 0: dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); execl("/usr/bin/du", "du", (char *) 0); exit(3); } if (pipe(fd1) == -1) { perror("Pipe"); exit(1); } switch (fork()) {

Should I use “-ansi” or explicit “-std=…” as compiler flags?

99封情书 提交于 2019-12-05 13:58:15
问题 I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly shown which standard is used. As I am specifically interested in compiling on Linux, Windows and MAC, I fear some compilers could not understand "-std=..." but "-ansi". So are there any pros and cons for using the one over the other? 回答1: If you

Should I use “-ansi” or explicit “-std=…” as compiler flags?

≡放荡痞女 提交于 2019-12-04 00:59:00
I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly shown which standard is used. As I am specifically interested in compiling on Linux, Windows and MAC, I fear some compilers could not understand "-std=..." but "-ansi". So are there any pros and cons for using the one over the other? If you want the compiler to enforce the 1989 ANSI C standard, or equivalently the 1990 ISO C standard (they describe

Multi-Dimensional Arrays in C: are they jagged?

醉酒当歌 提交于 2019-12-03 10:51:34
A simple question about the C programming language (ANSI-C): Are the multi-dimensional arrays in C jagged? I mean - are we talking about "array of arrays" (one array of pointers to other addresses in the memory) , or this is just "long one-dimensional array" (which is stored sequentially in the memory)? What that bothers me is that I'm kinda sure that: matrix[i][j] is equivalent to * ( * (matrix + i) + j) A multidimensional array in C is contiguous. The following: int m[4][5]; consists of 4 int[5] s laid out next to each other in memory. An array of pointers: int *m[4]; is jagged. Each pointer

ANSI C vs other C standards

余生长醉 提交于 2019-11-29 22:57:28
On several compilers I have used (all gcc but various versions) I get a C99 mode error for things like declaring int i inside the for loop expression instead of before it (if I do not use the std=c99 option). After reading here I understand that the gcc options -ansi , -std=c89 , and -std=iso9899:1990 all evaluate to the ANSI C standard, but I don't understand why/if I should pick the c89 standard versus a newer standard like c99 (which is the newest I assume). Also, I see multiple versions of the iso type standards for the C language, the first of which (from my understanding) is a direct